demarches-normaliennes/app/models/commentaire.rb

44 lines
1,023 B
Ruby
Raw Normal View History

2015-08-10 11:05:06 +02:00
class Commentaire < ActiveRecord::Base
belongs_to :dossier
2016-11-14 18:00:26 +01:00
belongs_to :champ
belongs_to :piece_justificative
after_create :notify
2016-12-26 11:08:53 +01:00
def header
"#{email}, " + I18n.l(created_at.localtime, format: '%d %b %Y %H:%M')
end
2016-12-26 11:08:53 +01:00
private
def notify
dossier_user_email = dossier.user.email
invited_users_emails = dossier.invites_user.pluck(:email).to_a
case email
when I18n.t("dynamics.contact_email")
# The commentaire is a copy of an automated notification email
# we sent to a user, so do nothing
when dossier_user_email, *invited_users_emails
# A user or an inved user posted a commentaire,
# we need to notify the gestionnaires
notify_gestionnaires
else
# A gestionnaire posted a commentaire,
# we need to notify the user
notify_user
end
2016-12-26 11:08:53 +01:00
end
def notify_gestionnaires
NotificationService.new('commentaire', self.dossier.id).notify
end
def notify_user
NotificationMailer.new_answer(dossier).deliver_now!
end
2015-08-10 11:05:06 +02:00
end