[Fix #2604] Handle ProcedurePresentations that have gone invalid
This commit is contained in:
parent
7baa239095
commit
14fd60bee7
3 changed files with 45 additions and 1 deletions
|
@ -4,6 +4,21 @@ class AssignTo < ApplicationRecord
|
|||
has_one :procedure_presentation, dependent: :destroy
|
||||
|
||||
def procedure_presentation_or_default_and_errors
|
||||
[procedure_presentation || build_procedure_presentation, nil]
|
||||
errors = reset_procedure_presentation_if_invalid
|
||||
[procedure_presentation || build_procedure_presentation, errors]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def reset_procedure_presentation_if_invalid
|
||||
if procedure_presentation&.invalid?
|
||||
# This is a last defense against invalid `ProcedurePresentation`s persistently
|
||||
# hindering instructeurs. Whenever this gets triggered, it means that there is
|
||||
# a bug somewhere else that we need to fix.
|
||||
|
||||
errors = procedure_presentation.errors
|
||||
self.procedure_presentation = nil
|
||||
errors
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -9,6 +9,7 @@ describe AssignTo, type: :model do
|
|||
|
||||
context "without a procedure_presentation" do
|
||||
it { expect(procedure_presentation_or_default).not_to be_persisted }
|
||||
it { expect(procedure_presentation_or_default).to be_valid }
|
||||
it { expect(errors).to be_nil }
|
||||
end
|
||||
|
||||
|
@ -16,7 +17,24 @@ describe AssignTo, type: :model do
|
|||
let!(:procedure_presentation) { ProcedurePresentation.create(assign_to: assign_to) }
|
||||
|
||||
it { expect(procedure_presentation_or_default).to eq(procedure_presentation) }
|
||||
it { expect(procedure_presentation_or_default).to be_valid }
|
||||
it { expect(errors).to be_nil }
|
||||
end
|
||||
|
||||
context "with an invalid procedure_presentation" do
|
||||
let!(:procedure_presentation) do
|
||||
pp = ProcedurePresentation.new(assign_to: assign_to, displayed_fields: [{ 'table' => 'invalid', 'column' => 'random' }])
|
||||
pp.save(validate: false)
|
||||
pp
|
||||
end
|
||||
|
||||
it { expect(procedure_presentation_or_default).not_to be_persisted }
|
||||
it { expect(procedure_presentation_or_default).to be_valid }
|
||||
it { expect(errors).to be_present }
|
||||
it do
|
||||
procedure_presentation_or_default
|
||||
expect(assign_to.procedure_presentation).not_to be(procedure_presentation)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -210,6 +210,17 @@ describe Gestionnaire, type: :model do
|
|||
it { expect(errors).to be_nil }
|
||||
end
|
||||
|
||||
context 'with invalid presentation' do
|
||||
let(:procedure_id) { procedure.id }
|
||||
before do
|
||||
pp = ProcedurePresentation.create(assign_to: procedure_assign, displayed_fields: [{ 'table' => 'invalid', 'column' => 'random' }])
|
||||
pp.save(:validate => false)
|
||||
end
|
||||
|
||||
it { expect(procedure_presentation).not_to be_persisted }
|
||||
it { expect(errors).to be_present }
|
||||
end
|
||||
|
||||
context 'with default presentation' do
|
||||
let(:procedure_id) { procedure_2.id }
|
||||
|
||||
|
|
Loading…
Reference in a new issue