demarches-normaliennes/lib/tasks/2018_05_21_cerfa_to_pj.rake

27 lines
938 B
Ruby
Raw Normal View History

2018-05-21 15:44:49 +02:00
namespace :'2018_05_21_cerfa_to_pj' do
task set: :environment do
dossiers = Cerfa.includes(dossier: [:procedure]).all.reject(&:empty?).map(&:dossier).compact.uniq
dossiers.group_by(&:procedure).each do |procedure, dossiers|
2018-05-30 15:58:54 +02:00
if !procedure.types_de_champ.find_by(libelle: 'CERFA')
2018-05-21 15:44:49 +02:00
procedure.administrateur.enable_feature(:champ_pj)
2018-05-30 15:58:54 +02:00
type_de_champ = procedure.types_de_champ.create(
2018-05-21 15:44:49 +02:00
type_champ: 'piece_justificative',
libelle: 'CERFA'
)
dossiers.each do |dossier|
cerfa = dossier.cerfa.last
champ = type_de_champ.champ.create(dossier: dossier)
response = Typhoeus.get(cerfa.content_url, timeout: 5)
if response.success?
champ.piece_justificative_file.attach(
io: StringIO.new(response.body),
filename: cerfa.content.filename
)
end
end
end
end
end
end