demarches-normaliennes/app/models/assign_to.rb

52 lines
2.2 KiB
Ruby
Raw Normal View History

2020-08-06 16:35:45 +02:00
# == Schema Information
#
# Table name: assign_tos
#
2022-08-30 21:41:36 +02:00
# id :integer not null, primary key
# daily_email_notifications_enabled :boolean default(FALSE), not null
# instant_email_dossier_notifications_enabled :boolean default(FALSE), not null
# instant_email_message_notifications_enabled :boolean default(FALSE), not null
# instant_expert_avis_email_notifications_enabled :boolean default(FALSE)
2022-08-30 21:41:36 +02:00
# manager :boolean default(FALSE)
# weekly_email_notifications_enabled :boolean default(TRUE), not null
# created_at :datetime
# updated_at :datetime
# groupe_instructeur_id :bigint
# instructeur_id :integer
2020-08-06 16:35:45 +02:00
#
2018-03-06 13:44:29 +01:00
class AssignTo < ApplicationRecord
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
has_one :procedure, through: :groupe_instructeur
2017-10-02 17:03:30 +02:00
scope :with_email_notifications, -> { where(daily_email_notifications_enabled: true) }
def procedure_presentation_or_default_and_errors
errors = reset_procedure_presentation_if_invalid
if self.procedure_presentation.nil?
self.procedure_presentation = build_procedure_presentation
self.procedure_presentation.save if procedure_presentation.valid? && !procedure_presentation.persisted?
end
[self.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
2021-01-28 14:49:22 +01:00
Sentry.capture_message(
"Destroying invalid ProcedurePresentation",
extra: { procedure_presentation: procedure_presentation.as_json }
)
self.procedure_presentation = nil
errors
end
2017-10-02 17:03:30 +02:00
end
2017-04-04 15:27:04 +02:00
end