2015-08-10 11:05:06 +02:00
|
|
|
class Commentaire < ActiveRecord::Base
|
2017-10-24 18:12:25 +02:00
|
|
|
belongs_to :dossier, touch: true
|
2016-11-14 18:00:26 +01:00
|
|
|
belongs_to :champ
|
2016-04-20 16:51:57 +02:00
|
|
|
belongs_to :piece_justificative
|
2016-11-25 11:08:58 +01:00
|
|
|
|
2017-10-30 16:07:02 +01:00
|
|
|
mount_uploader :file, CommentaireFileUploader
|
2017-11-08 16:38:07 +01:00
|
|
|
validates :file, file_size: { maximum: 20.megabytes, message: "La taille du fichier doit être inférieure à 20 Mo" }
|
2017-11-08 16:37:04 +01:00
|
|
|
validate :is_virus_free?
|
2017-11-07 20:40:00 +01:00
|
|
|
validates_presence_of :body, message: "Votre message ne peut être vide"
|
2017-10-30 16:07:02 +01:00
|
|
|
|
2017-10-05 13:43:37 +02:00
|
|
|
default_scope { order(created_at: :asc) }
|
2017-10-05 16:10:00 +02:00
|
|
|
scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) }
|
2017-07-20 14:17:12 +02:00
|
|
|
|
2017-05-12 13:32:48 +02:00
|
|
|
after_create :notify
|
2016-12-26 11:08:53 +01:00
|
|
|
|
2016-11-25 11:08:58 +01:00
|
|
|
def header
|
2017-04-18 17:33:24 +02:00
|
|
|
"#{email}, " + I18n.l(created_at.localtime, format: '%d %b %Y %H:%M')
|
2016-11-25 11:08:58 +01:00
|
|
|
end
|
2016-12-26 11:08:53 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-05-12 13:32:48 +02:00
|
|
|
def notify
|
|
|
|
dossier_user_email = dossier.user.email
|
|
|
|
invited_users_emails = dossier.invites_user.pluck(:email).to_a
|
|
|
|
|
|
|
|
case email
|
2017-05-12 14:35:57 +02:00
|
|
|
when I18n.t("dynamics.contact_email")
|
2017-05-12 13:32:48 +02:00
|
|
|
# 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
|
2016-12-26 14:38:00 +01:00
|
|
|
end
|
2016-12-26 11:08:53 +01:00
|
|
|
end
|
2017-05-12 13:40:32 +02:00
|
|
|
|
2017-05-12 13:32:48 +02:00
|
|
|
def notify_gestionnaires
|
|
|
|
NotificationService.new('commentaire', self.dossier.id).notify
|
|
|
|
end
|
|
|
|
|
2017-05-10 10:56:30 +02:00
|
|
|
def notify_user
|
2017-05-12 13:32:48 +02:00
|
|
|
NotificationMailer.new_answer(dossier).deliver_now!
|
2017-05-12 13:40:32 +02:00
|
|
|
end
|
2017-11-08 16:37:04 +01:00
|
|
|
|
|
|
|
def is_virus_free?
|
|
|
|
if file.present? && file_changed? && !ClamavService.safe_file?(file.path)
|
|
|
|
errors.add(:file, "Virus détecté dans le fichier joint, merci de changer de fichier")
|
|
|
|
end
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|