From 86b2cb211ef98ab65545a0db6316e6f94e5fa047 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 29 May 2018 11:44:44 +0200 Subject: [PATCH 1/2] Do not attempt to archive already archived procedures --- app/models/procedure.rb | 5 +---- app/models/procedure_path.rb | 15 ++++++++++++--- .../admin/procedures_controller_spec.rb | 18 +++++++++++++++++- .../users/dossiers_controller_spec.rb | 6 ------ 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index dbf8ede3d..a5f06c2a1 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -79,10 +79,7 @@ class Procedure < ApplicationRecord procedure_path = ProcedurePath.find_by(path: path) if procedure_path.present? - if procedure_path.procedure != self - procedure_path.procedure.archive! - procedure_path.update(procedure: self) - end + procedure_path.publish!(self) else ProcedurePath.create(procedure: self, administrateur: administrateur, path: path) end diff --git a/app/models/procedure_path.rb b/app/models/procedure_path.rb index c1cccb775..f34ddf396 100644 --- a/app/models/procedure_path.rb +++ b/app/models/procedure_path.rb @@ -11,15 +11,24 @@ class ProcedurePath < ApplicationRecord where(procedure: procedure).or(where(test_procedure: procedure)).last end - def hide!(procedure) - if self.procedure == procedure + def hide!(new_procedure) + if procedure == new_procedure update(procedure: nil) end - if self.test_procedure == procedure + if test_procedure == new_procedure update(test_procedure: nil) end if procedure.nil? && test_procedure.nil? destroy end end + + def publish!(new_procedure) + if procedure != new_procedure + if procedure&.publiee? + procedure.archive! + end + update(procedure: new_procedure) + end + end end diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index 89444b56b..cd6947cee 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -365,6 +365,22 @@ describe Admin::ProceduresController, type: :controller do end end + context 'procedure path exists and has archived procedure' do + let(:procedure_path) { procedure2.path } + let(:procedure2) { create(:procedure, :archived, administrateur: admin) } + + it 'publish the given procedure' do + expect(procedure.publiee?).to be_truthy + expect(procedure.path).to eq(procedure_path) + expect(response.status).to eq 302 + expect(flash[:notice]).to have_content 'Procédure publiée' + end + + it 'archive previous procedure' do + expect(procedure2.archivee?).to be_truthy + end + end + context 'procedure path exists and is not owned by current administrator' do let(:procedure_path) { procedure3.path } @@ -605,7 +621,7 @@ describe Admin::ProceduresController, type: :controller do end it { expect(procedure.hidden_at).not_to be_nil } - it { expect(procedure.procedure_path.procedure).to be_nil } + it { expect(procedure.procedure_path).to be_nil } end context "when procedure has no path" do diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index 99770bf05..d70e19c30 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -164,12 +164,6 @@ describe Users::DossiersController, type: :controller do it { expect(subject.status).to eq 200 } end - context 'when procedure is hidden' do - let(:procedure) { create(:procedure, :hidden) } - - it { expect(subject).to redirect_to(root_path) } - end - context 'when procedure path dose not exist' do let(:path) { 'hello' } From 1e3bb352cd69e9534b085d36b8f649e1c57e08a4 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Mon, 21 May 2018 15:44:49 +0200 Subject: [PATCH 2/2] 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