[Fix #2604] Handle ProcedurePresentations that have gone invalid

This commit is contained in:
Frederic Merizen 2018-10-23 11:48:25 +02:00
parent 7baa239095
commit 14fd60bee7
3 changed files with 45 additions and 1 deletions

View file

@ -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