diff --git a/app/controllers/new_gestionnaire/procedures_controller.rb b/app/controllers/new_gestionnaire/procedures_controller.rb index 71ba18093..4c8ac5d56 100644 --- a/app/controllers/new_gestionnaire/procedures_controller.rb +++ b/app/controllers/new_gestionnaire/procedures_controller.rb @@ -212,7 +212,11 @@ module NewGestionnaire end def get_procedure_presentation - current_gestionnaire.procedure_presentation_for_procedure_id(params[:procedure_id]) + procedure_presentation, errors = current_gestionnaire.procedure_presentation_and_errors_for_procedure_id(params[:procedure_id]) + if errors.present? + flash[:alert] = "Votre affichage a dû être réinitialisé en raison du problème suivant : " + errors.full_messages.join(', ') + end + procedure_presentation end def displayed_fields_values diff --git a/app/models/assign_to.rb b/app/models/assign_to.rb index af232d267..1e21528b4 100644 --- a/app/models/assign_to.rb +++ b/app/models/assign_to.rb @@ -3,7 +3,7 @@ class AssignTo < ApplicationRecord belongs_to :gestionnaire has_one :procedure_presentation, dependent: :destroy - def procedure_presentation_or_default - procedure_presentation || build_procedure_presentation + def procedure_presentation_or_default_and_errors + [procedure_presentation || build_procedure_presentation, nil] end end diff --git a/app/models/gestionnaire.rb b/app/models/gestionnaire.rb index 675cafebb..ae9030646 100644 --- a/app/models/gestionnaire.rb +++ b/app/models/gestionnaire.rb @@ -81,8 +81,8 @@ class Gestionnaire < ApplicationRecord end end - def procedure_presentation_for_procedure_id(procedure_id) - assign_to.find_by(procedure_id: procedure_id).procedure_presentation_or_default + def procedure_presentation_and_errors_for_procedure_id(procedure_id) + assign_to.find_by(procedure_id: procedure_id).procedure_presentation_or_default_and_errors end def notifications_for_dossier(dossier) diff --git a/spec/models/assign_to_spec.rb b/spec/models/assign_to_spec.rb index 9b520c188..fbd3e59d0 100644 --- a/spec/models/assign_to_spec.rb +++ b/spec/models/assign_to_spec.rb @@ -1,18 +1,22 @@ describe AssignTo, type: :model do - describe '#procedure_presentation_or_default' do + describe '#procedure_presentation_or_default_and_errors' do let(:procedure) { create(:procedure) } let(:assign_to) { AssignTo.create(procedure: procedure) } - let(:procedure_presentation_or_default) { assign_to.procedure_presentation_or_default } + let(:procedure_presentation_and_errors) { assign_to.procedure_presentation_or_default_and_errors } + let(:procedure_presentation_or_default) { procedure_presentation_and_errors.first } + let(:errors) { procedure_presentation_and_errors.second } context "without a procedure_presentation" do it { expect(procedure_presentation_or_default).not_to be_persisted } + it { expect(errors).to be_nil } end context "with a procedure_presentation" do let!(:procedure_presentation) { ProcedurePresentation.create(assign_to: assign_to) } it { expect(procedure_presentation_or_default).to eq(procedure_presentation) } + it { expect(errors).to be_nil } end end end diff --git a/spec/models/gestionnaire_spec.rb b/spec/models/gestionnaire_spec.rb index fc9ab72bc..df119bc25 100644 --- a/spec/models/gestionnaire_spec.rb +++ b/spec/models/gestionnaire_spec.rb @@ -197,20 +197,24 @@ describe Gestionnaire, type: :model do end end - describe "procedure_presentation_for_procedure_id" do - let(:procedure_presentation) { gestionnaire.procedure_presentation_for_procedure_id(procedure_id) } + describe "procedure_presentation_and_errors_for_procedure_id" do + let(:procedure_presentation_and_errors) { gestionnaire.procedure_presentation_and_errors_for_procedure_id(procedure_id) } + let(:procedure_presentation) { procedure_presentation_and_errors.first } + let(:errors) { procedure_presentation_and_errors.second } context 'with explicit presentation' do let(:procedure_id) { procedure.id } let!(:pp) { ProcedurePresentation.create(assign_to: procedure_assign) } it { expect(procedure_presentation).to eq(pp) } + it { expect(errors).to be_nil } end context 'with default presentation' do let(:procedure_id) { procedure_2.id } it { expect(procedure_presentation).not_to be_persisted } + it { expect(errors).to be_nil } end end