2017-04-25 12:09:11 +02:00
|
|
|
class Avis < ApplicationRecord
|
2018-02-28 15:46:27 +01:00
|
|
|
include EmailSanitizableConcern
|
|
|
|
|
2017-10-24 18:12:25 +02:00
|
|
|
belongs_to :dossier, touch: true
|
2017-04-25 12:09:11 +02:00
|
|
|
belongs_to :gestionnaire
|
2017-05-23 13:28:17 +02:00
|
|
|
belongs_to :claimant, class_name: 'Gestionnaire'
|
2017-04-25 17:02:54 +02:00
|
|
|
|
2018-02-27 16:58:22 +01:00
|
|
|
validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true
|
|
|
|
|
2018-02-28 15:46:27 +01:00
|
|
|
before_validation -> { sanitize_email(:email) }
|
2017-07-20 16:30:11 +02:00
|
|
|
before_create :try_to_assign_gestionnaire
|
2017-05-02 15:37:06 +02:00
|
|
|
after_create :notify_gestionnaire
|
|
|
|
|
2017-08-24 16:26:57 +02:00
|
|
|
default_scope { joins(:dossier) }
|
2017-04-27 12:17:50 +02:00
|
|
|
scope :with_answer, -> { where.not(answer: nil) }
|
|
|
|
scope :without_answer, -> { where(answer: nil) }
|
2018-01-15 21:53:30 +01: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-10-05 16:10:00 +02:00
|
|
|
scope :updated_since?, -> (date) { where('avis.updated_at > ?', date) }
|
2017-04-27 12:17:50 +02:00
|
|
|
|
2017-04-25 17:02:54 +02:00
|
|
|
def email_to_display
|
2018-05-30 18:45:46 +02:00
|
|
|
gestionnaire&.email || email
|
2017-04-25 17:02:54 +02:00
|
|
|
end
|
2017-05-02 15:37:06 +02:00
|
|
|
|
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
|
|
|
|
2017-05-09 18:11:58 +02:00
|
|
|
def self.avis_exists_and_email_belongs_to_avis?(avis_id, email)
|
2018-05-30 18:45:46 +02:00
|
|
|
Avis.find_by(id: avis_id)&.email == email
|
2017-05-12 10:22:18 +02:00
|
|
|
end
|
2017-07-20 11:54:41 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def notify_gestionnaire
|
2018-05-25 18:05:28 +02:00
|
|
|
AvisMailer.avis_invitation(self).deliver_later
|
2017-07-20 11:54:41 +02:00
|
|
|
end
|
2017-07-20 16:30:11 +02:00
|
|
|
|
|
|
|
def try_to_assign_gestionnaire
|
|
|
|
gestionnaire = Gestionnaire.find_by(email: email)
|
|
|
|
if gestionnaire
|
|
|
|
self.gestionnaire = gestionnaire
|
|
|
|
self.email = nil
|
|
|
|
end
|
|
|
|
end
|
2017-04-25 12:09:11 +02:00
|
|
|
end
|