From 1e3bb352cd69e9534b085d36b8f649e1c57e08a4 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Mon, 21 May 2018 15:44:49 +0200 Subject: [PATCH] Convert CERFA to PJ --- lib/tasks/2018_05_21_cerfa_to_pj.rake | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 lib/tasks/2018_05_21_cerfa_to_pj.rake diff --git a/lib/tasks/2018_05_21_cerfa_to_pj.rake b/lib/tasks/2018_05_21_cerfa_to_pj.rake new file mode 100644 index 000000000..2dcabd4c9 --- /dev/null +++ b/lib/tasks/2018_05_21_cerfa_to_pj.rake @@ -0,0 +1,26 @@ +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| + if !procedure.type_de_champs.find_by(libelle: 'CERFA') + procedure.administrateur.enable_feature(:champ_pj) + type_de_champ = procedure.type_de_champs.create( + 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