diff --git a/app/controllers/administrateurs/services_controller.rb b/app/controllers/administrateurs/services_controller.rb index 08835eb96..91ad55375 100644 --- a/app/controllers/administrateurs/services_controller.rb +++ b/app/controllers/administrateurs/services_controller.rb @@ -64,6 +64,7 @@ module Administrateurs flash[:alert] = message redirect_to admin_services_path(procedure_id: params[:procedure_id]) else + service_to_destroy.procedures.with_discarded.discarded.update(service: nil) service_to_destroy.destroy redirect_to admin_services_path(procedure_id: params[:procedure_id]), notice: "#{service_to_destroy.nom} est supprimé" diff --git a/config/environments/test.rb b/config/environments/test.rb index cfe9eb30b..9d411b7fd 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -55,6 +55,9 @@ Rails.application.configure do # Tell Active Support which deprecation messages to disallow. config.active_support.disallowed_deprecation_warnings = [] + # Highlight code that triggered database queries in logs. + config.active_record.verbose_query_logs = true + # Raises error for missing translations. # config.i18n.raise_on_missing_translations = true diff --git a/spec/controllers/administrateurs/services_controller_spec.rb b/spec/controllers/administrateurs/services_controller_spec.rb index 9945edd0c..e4070f9ed 100644 --- a/spec/controllers/administrateurs/services_controller_spec.rb +++ b/spec/controllers/administrateurs/services_controller_spec.rb @@ -135,5 +135,19 @@ describe Administrateurs::ServicesController, type: :controller do it { expect(flash.notice).to be_nil } it { expect(response).to redirect_to(admin_services_path(procedure_id: 12)) } end + + context "when a service has some related discarded procedures" do + let!(:procedure) { create(:procedure, :discarded, service: service) } + + before do + sign_in(admin.user) + delete :destroy, params: { id: service.id, procedure_id: procedure.id } + end + + it { expect { service.reload }.to raise_error(ActiveRecord::RecordNotFound) } + it { expect(flash.alert).to be_nil } + it { expect(flash.notice).to eq("#{service.nom} est supprimé") } + it { expect(procedure.reload.service_id).to be_nil } + end end end