Assign users and gestionnaires to commentaires

fix #2052
This commit is contained in:
Paul Chavard 2018-11-29 15:00:26 +01:00
parent b927ab4568
commit a08f170dbd
11 changed files with 39 additions and 25 deletions

View file

@ -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é"

View file

@ -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)

View file

@ -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é à linstructeur en charge de votre dossier."

View file

@ -62,11 +62,12 @@ class SupportController < ApplicationController
end
def create_commentaire
dossier.commentaires.create(
email: email,
params = {
file: params[:file],
body: "[#{params[:subject]}]<br><br>#{params[:text]}"
)
}
commentaire = CommentaireService.build_with_email(email, dossier, params)
commentaire.save!
end
def tags

View file

@ -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("<br><br>")
)
params = { body: ["[#{subject}]", body].join("<br><br>") }
commentaire = CommentaireService.build_with_email(CONTACT_EMAIL, dossier, params)
commentaire.save!
end
end

View file

@ -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

View file

@ -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 '<p></p>',
# 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

View file

@ -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

View file

@ -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("<p>avant\n<br />apres</p>")
expect(assigns(:commentaire).body).to eq("avant\napres")
end
end
end

View file

@ -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("<p>avant\n<br />apres</p>")
expect(assigns(:commentaire).body).to eq("avant\napres")
end
end
end

View file

@ -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 '<p>Contenu du message.</p>'
expect(commentaire.body).to eq 'Contenu du message.'
expect(commentaire.file).to be_blank
expect(commentaire).to be_valid
end