2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-06 13:44:29 +01:00
|
|
|
class AssignTo < ApplicationRecord
|
2020-07-20 16:13:51 +02:00
|
|
|
belongs_to :instructeur, optional: false
|
|
|
|
belongs_to :groupe_instructeur, optional: false
|
2017-10-02 17:03:30 +02:00
|
|
|
has_one :procedure_presentation, dependent: :destroy
|
2019-08-26 16:17:25 +02:00
|
|
|
has_one :procedure, through: :groupe_instructeur
|
2017-10-02 17:03:30 +02:00
|
|
|
|
2020-02-27 11:09:19 +01:00
|
|
|
scope :with_email_notifications, -> { where(daily_email_notifications_enabled: true) }
|
2020-02-18 17:18:25 +01:00
|
|
|
|
2018-10-23 11:39:54 +02:00
|
|
|
def procedure_presentation_or_default_and_errors
|
2018-10-23 11:48:25 +02:00
|
|
|
errors = reset_procedure_presentation_if_invalid
|
2024-10-11 15:29:52 +02:00
|
|
|
|
2022-10-21 17:24:16 +02:00
|
|
|
if self.procedure_presentation.nil?
|
2024-10-11 15:29:52 +02:00
|
|
|
self.procedure_presentation = create_procedure_presentation!
|
2022-10-21 17:24:16 +02:00
|
|
|
end
|
2024-10-11 15:29:52 +02:00
|
|
|
|
2022-10-21 17:24:16 +02:00
|
|
|
[self.procedure_presentation, errors]
|
2018-10-23 11:48:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def reset_procedure_presentation_if_invalid
|
2024-10-11 15:29:52 +02:00
|
|
|
errors = begin
|
|
|
|
procedure_presentation.errors if procedure_presentation&.invalid?
|
|
|
|
rescue ActiveRecord::RecordNotFound => e
|
2024-10-31 03:27:05 +01:00
|
|
|
errors = ActiveModel::Errors.new(self)
|
|
|
|
errors.add(:procedure_presentation, e.message)
|
|
|
|
errors
|
2024-10-11 15:29:52 +02:00
|
|
|
end
|
2018-10-23 11:48:25 +02:00
|
|
|
|
2024-10-11 15:29:52 +02:00
|
|
|
if errors.present?
|
2021-01-28 14:49:22 +01:00
|
|
|
Sentry.capture_message(
|
2018-10-23 12:18:46 +02:00
|
|
|
"Destroying invalid ProcedurePresentation",
|
2024-11-06 10:52:23 +01:00
|
|
|
extra: { procedure_presentation_id: procedure_presentation.id, errors: errors.full_messages }
|
2018-10-23 12:18:46 +02:00
|
|
|
)
|
2018-10-23 11:48:25 +02:00
|
|
|
self.procedure_presentation = nil
|
|
|
|
end
|
2024-10-11 15:29:52 +02:00
|
|
|
|
|
|
|
errors
|
2017-10-02 17:03:30 +02:00
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
end
|