From 3f0f22d8494b971bd3281e06554323d45221148c Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 23 Jan 2019 15:11:16 +0000 Subject: [PATCH 1/4] procedure: clarify controller spec --- .../admin/procedures_controller_spec.rb | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index bad0c5be6..ecdb510aa 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -97,24 +97,18 @@ describe Admin::ProceduresController, type: :controller do subject { delete :destroy, params: { id: procedure.id } } - context 'when procedure is draft' do + context 'when the procedure is a draft' do let!(:procedure) { procedure_draft } - describe 'tech params' do - before do - subject - end - - it { expect(subject.status).to eq 302 } - it { expect(flash[:notice]).to be_present } + it 'destroys the procedure' do + expect { subject }.to change { Procedure.count }.by(-1) end - it 'destroy procedure is call' do - expect_any_instance_of(Procedure).to receive(:destroy) + it 'redirects to the procedure drafts page' do subject + expect(response).to redirect_to admin_procedures_draft_path + expect(flash[:notice]).to be_present end - - it { expect { subject }.to change { Procedure.count }.by(-1) } end context 'when procedure is published' do From fb75461c78fc7d4eeb83add483dc371fc3041db8 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 23 Jan 2019 16:13:14 +0100 Subject: [PATCH 2/4] procedure: delete dossiers on destroy --- .../admin/procedures_controller.rb | 1 + .../admin/procedures_controller_spec.rb | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index a05960b6e..6b9a347a8 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -54,6 +54,7 @@ class Admin::ProceduresController < AdminController return render json: {}, status: 401 end + procedure.reset! procedure.destroy flash.notice = 'Démarche supprimée' diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index ecdb510aa..64725cc03 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -91,9 +91,9 @@ describe Admin::ProceduresController, type: :controller do end describe 'DELETE #destroy' do - let(:procedure_draft) { create :procedure, administrateur: admin, published_at: nil, archived_at: nil } - let(:procedure_published) { create :procedure, administrateur: admin, aasm_state: :publiee, published_at: Time.zone.now, archived_at: nil } - let(:procedure_archived) { create :procedure, administrateur: admin, aasm_state: :archivee, published_at: nil, archived_at: Time.zone.now } + let(:procedure_draft) { create :procedure_with_dossiers, administrateur: admin, published_at: nil, archived_at: nil } + let(:procedure_published) { create :procedure_with_dossiers, administrateur: admin, aasm_state: :publiee, published_at: Time.zone.now, archived_at: nil } + let(:procedure_archived) { create :procedure_with_dossiers, administrateur: admin, aasm_state: :archivee, published_at: nil, archived_at: Time.zone.now } subject { delete :destroy, params: { id: procedure.id } } @@ -104,6 +104,11 @@ describe Admin::ProceduresController, type: :controller do expect { subject }.to change { Procedure.count }.by(-1) end + it 'deletes associated dossiers' do + subject + expect(Dossier.find_by(procedure_id: procedure.id)).to be_blank + end + it 'redirects to the procedure drafts page' do subject expect(response).to redirect_to admin_procedures_draft_path @@ -112,14 +117,18 @@ describe Admin::ProceduresController, type: :controller do end context 'when procedure is published' do - let(:procedure) { procedure_published } + let!(:procedure) { procedure_published } + it { expect { subject }.not_to change { Procedure.count } } + it { expect { subject }.not_to change { Dossier.count } } it { expect(subject.status).to eq 401 } end context 'when procedure is archived' do - let(:procedure) { procedure_published } + let!(:procedure) { procedure_archived } + it { expect { subject }.not_to change { Procedure.count } } + it { expect { subject }.not_to change { Dossier.count } } it { expect(subject.status).to eq 401 } end From db8289a19da25df4d1b4b7495de2a3ece665d055 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 23 Jan 2019 16:13:38 +0100 Subject: [PATCH 3/4] procedure: ensure a procedure can't be deleted while it has dossiers --- app/models/procedure.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 276a90183..9b0eeb631 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -6,7 +6,7 @@ class Procedure < ApplicationRecord has_many :types_de_piece_justificative, -> { ordered }, dependent: :destroy has_many :types_de_champ, -> { root.public_only.ordered }, dependent: :destroy has_many :types_de_champ_private, -> { root.private_only.ordered }, class_name: 'TypeDeChamp', dependent: :destroy - has_many :dossiers + has_many :dossiers, dependent: :restrict_with_exception has_many :deleted_dossiers, dependent: :destroy has_one :module_api_carto, dependent: :destroy From bb1cc1eef8a085eae4c2addeb39dcf2e35666a3a Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 23 Jan 2019 15:13:57 +0000 Subject: [PATCH 4/4] tasks: delete dossiers without procedures Fix #3279 --- ...829_delete_dossiers_without_procedure.rake | 25 +++++++++++ ...te_dossiers_without_procedure.rake_spec.rb | 42 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 lib/tasks/deployment/20190117154829_delete_dossiers_without_procedure.rake create mode 100644 spec/lib/tasks/deployment/20190117154829_delete_dossiers_without_procedure.rake_spec.rb diff --git a/lib/tasks/deployment/20190117154829_delete_dossiers_without_procedure.rake b/lib/tasks/deployment/20190117154829_delete_dossiers_without_procedure.rake new file mode 100644 index 000000000..2e7db554d --- /dev/null +++ b/lib/tasks/deployment/20190117154829_delete_dossiers_without_procedure.rake @@ -0,0 +1,25 @@ +require Rails.root.join("lib", "tasks", "task_helper") + +namespace :after_party do + desc 'Deployment task: delete_dossiers_without_procedure' + task delete_dossiers_without_procedure: :environment do + rake_puts "Running deploy task 'delete_dossiers_without_procedure'" + + dossiers_without_procedure = Dossier.left_outer_joins(:procedure).where(procedures: { id: nil }) + total = dossiers_without_procedure.count + expected_dossiers_count = 60 + + if total > expected_dossiers_count + raise "Error: #{expected_dossiers_count} dossiers expected, but found #{total}. Aborting." + end + + dossiers_without_procedure.each do |dossier| + rake_puts "Destroy dossier #{dossier.id}" + dossier.destroy! + end + + rake_puts "#{total} dossiers without procedure were destroyed." + + AfterParty::TaskRecord.create version: '20190117154829' + end # task :delete_dossiers_without_procedure +end # namespace :after_party diff --git a/spec/lib/tasks/deployment/20190117154829_delete_dossiers_without_procedure.rake_spec.rb b/spec/lib/tasks/deployment/20190117154829_delete_dossiers_without_procedure.rake_spec.rb new file mode 100644 index 000000000..89cdf9b52 --- /dev/null +++ b/spec/lib/tasks/deployment/20190117154829_delete_dossiers_without_procedure.rake_spec.rb @@ -0,0 +1,42 @@ +describe '20190117154829_delete_dossiers_without_procedure.rake' do + let(:rake_task) { Rake::Task['after_party:delete_dossiers_without_procedure'] } + + subject do + rake_task.invoke + end + + after do + rake_task.reenable + end + + context 'when the procedure of some dossiers has been deleted' do + let!(:procedure1) { create(:procedure_with_dossiers, dossiers_count: 2) } + let!(:procedure2) { create(:procedure_with_dossiers, :published, dossiers_count: 2) } + let!(:procedure3) { create(:procedure_with_dossiers, :published, dossiers_count: 2) } + let!(:procedure4) { create(:procedure_with_dossiers, :archived, dossiers_count: 2) } + + let(:procedure_2_dossier_ids) { procedure2.dossiers.pluck(:id) } + + before do + procedure_2_dossier_ids + procedure2.delete + expect(procedure_2_dossier_ids.count).to eq(2) + expect(Dossier.find_by(id: procedure_2_dossier_ids.first).procedure).to be nil + expect(Dossier.find_by(id: procedure_2_dossier_ids.second).procedure).to be nil + end + + it 'destroy dossiers without an existing procedure' do + subject + expect(Dossier.unscoped.find_by(id: procedure_2_dossier_ids.first)).to be nil + expect(Dossier.unscoped.find_by(id: procedure_2_dossier_ids.last)).to be nil + end + + it 'doesn’t destroy other dossiers' do + subject + expect(Dossier.all.count).to eq(6) + expect(procedure1.reload.dossiers.count).to eq(2) + expect(procedure3.reload.dossiers.count).to eq(2) + expect(procedure4.reload.dossiers.count).to eq(2) + end + end +end