2017-04-25 12:09:11 +02:00
|
|
|
class Avis < ApplicationRecord
|
|
|
|
belongs_to :dossier
|
|
|
|
belongs_to :gestionnaire
|
2017-04-25 17:02:54 +02:00
|
|
|
|
2017-05-02 15:37:06 +02:00
|
|
|
after_create :notify_gestionnaire
|
|
|
|
|
2017-04-27 12:17:50 +02:00
|
|
|
scope :with_answer, -> { where.not(answer: nil) }
|
|
|
|
scope :without_answer, -> { where(answer: nil) }
|
2017-05-24 18:45:31 +02:00
|
|
|
scope :for_dossier, ->(dossier_id) { where(dossier_id: dossier_id) }
|
2017-05-02 13:54:57 +02:00
|
|
|
scope :by_latest, -> { order(updated_at: :desc) }
|
2017-04-27 12:17:50 +02:00
|
|
|
|
2017-04-25 17:02:54 +02:00
|
|
|
def email_to_display
|
|
|
|
gestionnaire.try(:email) || email
|
|
|
|
end
|
2017-05-02 15:37:06 +02:00
|
|
|
|
|
|
|
def notify_gestionnaire
|
|
|
|
AvisMailer.you_are_invited_on_dossier(self).deliver_now
|
|
|
|
end
|
2017-05-02 16:13:09 +02:00
|
|
|
|
|
|
|
def self.link_avis_to_gestionnaire(gestionnaire)
|
|
|
|
Avis.where(email: gestionnaire.email).update_all(email: nil, gestionnaire_id: gestionnaire.id)
|
|
|
|
end
|
2017-05-12 10:22:18 +02:00
|
|
|
|
|
|
|
def self.avis_exists_and_email_belongs_to_avis?(avis_id, email)
|
|
|
|
avis = Avis.find_by(id: avis_id)
|
|
|
|
avis.present? && avis.email == email
|
|
|
|
end
|
2017-04-25 12:09:11 +02:00
|
|
|
end
|