diff --git a/app/controllers/new_gestionnaire/avis_controller.rb b/app/controllers/new_gestionnaire/avis_controller.rb index e8267f944..7cb207e7a 100644 --- a/app/controllers/new_gestionnaire/avis_controller.rb +++ b/app/controllers/new_gestionnaire/avis_controller.rb @@ -50,7 +50,7 @@ module NewGestionnaire end def create_commentaire - @commentaire = Commentaire.new(commentaire_params.merge(email: current_gestionnaire.email, dossier: avis.dossier)) + @commentaire = CommentaireService.build(current_gestionnaire, avis.dossier, commentaire_params) if @commentaire.save flash.notice = "Message envoyé" diff --git a/app/controllers/new_gestionnaire/dossiers_controller.rb b/app/controllers/new_gestionnaire/dossiers_controller.rb index 473d66d08..2ae4e52da 100644 --- a/app/controllers/new_gestionnaire/dossiers_controller.rb +++ b/app/controllers/new_gestionnaire/dossiers_controller.rb @@ -106,7 +106,7 @@ module NewGestionnaire end def create_commentaire - @commentaire = CommentaireService.create(current_gestionnaire, dossier, commentaire_params) + @commentaire = CommentaireService.build(current_gestionnaire, dossier, commentaire_params) if @commentaire.save current_gestionnaire.follow(dossier) diff --git a/app/controllers/new_user/dossiers_controller.rb b/app/controllers/new_user/dossiers_controller.rb index 57e8e6117..9faa079a4 100644 --- a/app/controllers/new_user/dossiers_controller.rb +++ b/app/controllers/new_user/dossiers_controller.rb @@ -172,7 +172,7 @@ module NewUser end def create_commentaire - @commentaire = CommentaireService.create(current_user, dossier, commentaire_params) + @commentaire = CommentaireService.build(current_user, dossier, commentaire_params) if @commentaire.save flash.notice = "Votre message a bien été envoyé à l’instructeur en charge de votre dossier." diff --git a/app/controllers/support_controller.rb b/app/controllers/support_controller.rb index 45f4c074d..330100091 100644 --- a/app/controllers/support_controller.rb +++ b/app/controllers/support_controller.rb @@ -62,11 +62,12 @@ class SupportController < ApplicationController end def create_commentaire - dossier.commentaires.create( - email: email, + params = { file: params[:file], body: "[#{params[:subject]}]

#{params[:text]}" - ) + } + commentaire = CommentaireService.build_with_email(email, dossier, params) + commentaire.save! end def tags diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 503bd4bf0..8b855662d 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -46,10 +46,8 @@ class NotificationMailer < ApplicationMailer end def create_commentaire_for_notification(dossier, subject, body) - Commentaire.create( - dossier: dossier, - email: CONTACT_EMAIL, - body: ["[#{subject}]", body].join("

") - ) + params = { body: ["[#{subject}]", body].join("

") } + commentaire = CommentaireService.build_with_email(CONTACT_EMAIL, dossier, params) + commentaire.save! end end diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index cfe068626..5a49b6e84 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -2,6 +2,9 @@ class Commentaire < ApplicationRecord belongs_to :dossier, touch: true belongs_to :piece_justificative + belongs_to :user + belongs_to :gestionnaire + mount_uploader :file, CommentaireFileUploader validates :file, file_size: { maximum: 20.megabytes, message: "La taille du fichier doit être inférieure à 20 Mo" } validate :is_virus_free? @@ -16,6 +19,16 @@ class Commentaire < ApplicationRecord super.reject { |c| c.name == "champ" } end + def email + if user + user.email + elsif gestionnaire + gestionnaire.email + else + read_attribute(:email) + end + end + def header "#{sender}, #{I18n.l(created_at, format: '%d %b %Y %H:%M')}" end diff --git a/app/services/commentaire_service.rb b/app/services/commentaire_service.rb index c70ddf815..fa3b9a576 100644 --- a/app/services/commentaire_service.rb +++ b/app/services/commentaire_service.rb @@ -1,16 +1,18 @@ class CommentaireService class << self - def create(sender, dossier, params) - attributes = params.merge(email: sender.email, dossier: dossier) - - # If the user submits a empty message, simple_format will replace '' by '

', - # and thus bypass the not-empty constraint on commentaire's body. - # - # To avoid this, format the message only if a body is present in the first place. - if attributes[:body].present? - attributes[:body] = ActionController::Base.helpers.simple_format(attributes[:body]) + def build(sender, dossier, params) + case sender + when User + params[:user] = sender + when Gestionnaire + params[:gestionnaire] = sender end + build_with_email(sender.email, dossier, params) + end + + def build_with_email(email, dossier, params) + attributes = params.merge(email: email, dossier: dossier) Commentaire.new(attributes) end end diff --git a/app/views/shared/dossiers/messages/_message.html.haml b/app/views/shared/dossiers/messages/_message.html.haml index a1b78bc9d..39ac76ef3 100644 --- a/app/views/shared/dossiers/messages/_message.html.haml +++ b/app/views/shared/dossiers/messages/_message.html.haml @@ -8,7 +8,7 @@ %span.guest Invité %span.date{ class: highlight_if_unseen_class(messagerie_seen_at, commentaire.created_at) } = commentaire_date(commentaire) - .rich-text= sanitize(commentaire.body) + .rich-text= sanitize(simple_format(commentaire.body)) - if commentaire.piece_justificative .attachment-link diff --git a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb index ac3dc3361..6ebdc7a0e 100644 --- a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb @@ -301,7 +301,7 @@ describe NewGestionnaire::DossiersController, type: :controller do expect(response).to render_template :messagerie expect(flash.alert).to be_present - expect(assigns(:commentaire).body).to eq("

avant\n
apres

") + expect(assigns(:commentaire).body).to eq("avant\napres") end end end diff --git a/spec/controllers/new_user/dossiers_controller_spec.rb b/spec/controllers/new_user/dossiers_controller_spec.rb index 6bc393ea2..27726b35f 100644 --- a/spec/controllers/new_user/dossiers_controller_spec.rb +++ b/spec/controllers/new_user/dossiers_controller_spec.rb @@ -787,7 +787,7 @@ describe NewUser::DossiersController, type: :controller do expect(response).to render_template :messagerie expect(flash.alert).to be_present - expect(assigns(:commentaire).body).to eq("

avant\n
apres

") + expect(assigns(:commentaire).body).to eq("avant\napres") end end end diff --git a/spec/services/commentaire_service_spec.rb b/spec/services/commentaire_service_spec.rb index 55e5f3115..d781ca8ca 100644 --- a/spec/services/commentaire_service_spec.rb +++ b/spec/services/commentaire_service_spec.rb @@ -8,7 +8,7 @@ describe CommentaireService do let(:file) { nil } let(:scan_result) { true } - subject(:commentaire) { CommentaireService.create(sender, dossier, { body: body, file: file }) } + subject(:commentaire) { CommentaireService.build(sender, dossier, { body: body, file: file }) } before do allow(ClamavService).to receive(:safe_file?).and_return(scan_result) @@ -17,7 +17,7 @@ describe CommentaireService do it 'creates a new valid commentaire' do expect(commentaire.email).to eq sender.email expect(commentaire.dossier).to eq dossier - expect(commentaire.body).to eq '

Contenu du message.

' + expect(commentaire.body).to eq 'Contenu du message.' expect(commentaire.file).to be_blank expect(commentaire).to be_valid end