parent
b927ab4568
commit
a08f170dbd
11 changed files with 39 additions and 25 deletions
|
@ -50,7 +50,7 @@ module NewGestionnaire
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_commentaire
|
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
|
if @commentaire.save
|
||||||
flash.notice = "Message envoyé"
|
flash.notice = "Message envoyé"
|
||||||
|
|
|
@ -106,7 +106,7 @@ module NewGestionnaire
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_commentaire
|
def create_commentaire
|
||||||
@commentaire = CommentaireService.create(current_gestionnaire, dossier, commentaire_params)
|
@commentaire = CommentaireService.build(current_gestionnaire, dossier, commentaire_params)
|
||||||
|
|
||||||
if @commentaire.save
|
if @commentaire.save
|
||||||
current_gestionnaire.follow(dossier)
|
current_gestionnaire.follow(dossier)
|
||||||
|
|
|
@ -172,7 +172,7 @@ module NewUser
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_commentaire
|
def create_commentaire
|
||||||
@commentaire = CommentaireService.create(current_user, dossier, commentaire_params)
|
@commentaire = CommentaireService.build(current_user, dossier, commentaire_params)
|
||||||
|
|
||||||
if @commentaire.save
|
if @commentaire.save
|
||||||
flash.notice = "Votre message a bien été envoyé à l’instructeur en charge de votre dossier."
|
flash.notice = "Votre message a bien été envoyé à l’instructeur en charge de votre dossier."
|
||||||
|
|
|
@ -62,11 +62,12 @@ class SupportController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_commentaire
|
def create_commentaire
|
||||||
dossier.commentaires.create(
|
params = {
|
||||||
email: email,
|
|
||||||
file: params[:file],
|
file: params[:file],
|
||||||
body: "[#{params[:subject]}]<br><br>#{params[:text]}"
|
body: "[#{params[:subject]}]<br><br>#{params[:text]}"
|
||||||
)
|
}
|
||||||
|
commentaire = CommentaireService.build_with_email(email, dossier, params)
|
||||||
|
commentaire.save!
|
||||||
end
|
end
|
||||||
|
|
||||||
def tags
|
def tags
|
||||||
|
|
|
@ -46,10 +46,8 @@ class NotificationMailer < ApplicationMailer
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_commentaire_for_notification(dossier, subject, body)
|
def create_commentaire_for_notification(dossier, subject, body)
|
||||||
Commentaire.create(
|
params = { body: ["[#{subject}]", body].join("<br><br>") }
|
||||||
dossier: dossier,
|
commentaire = CommentaireService.build_with_email(CONTACT_EMAIL, dossier, params)
|
||||||
email: CONTACT_EMAIL,
|
commentaire.save!
|
||||||
body: ["[#{subject}]", body].join("<br><br>")
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,6 +2,9 @@ class Commentaire < ApplicationRecord
|
||||||
belongs_to :dossier, touch: true
|
belongs_to :dossier, touch: true
|
||||||
belongs_to :piece_justificative
|
belongs_to :piece_justificative
|
||||||
|
|
||||||
|
belongs_to :user
|
||||||
|
belongs_to :gestionnaire
|
||||||
|
|
||||||
mount_uploader :file, CommentaireFileUploader
|
mount_uploader :file, CommentaireFileUploader
|
||||||
validates :file, file_size: { maximum: 20.megabytes, message: "La taille du fichier doit être inférieure à 20 Mo" }
|
validates :file, file_size: { maximum: 20.megabytes, message: "La taille du fichier doit être inférieure à 20 Mo" }
|
||||||
validate :is_virus_free?
|
validate :is_virus_free?
|
||||||
|
@ -16,6 +19,16 @@ class Commentaire < ApplicationRecord
|
||||||
super.reject { |c| c.name == "champ" }
|
super.reject { |c| c.name == "champ" }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def email
|
||||||
|
if user
|
||||||
|
user.email
|
||||||
|
elsif gestionnaire
|
||||||
|
gestionnaire.email
|
||||||
|
else
|
||||||
|
read_attribute(:email)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def header
|
def header
|
||||||
"#{sender}, #{I18n.l(created_at, format: '%d %b %Y %H:%M')}"
|
"#{sender}, #{I18n.l(created_at, format: '%d %b %Y %H:%M')}"
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,16 +1,18 @@
|
||||||
class CommentaireService
|
class CommentaireService
|
||||||
class << self
|
class << self
|
||||||
def create(sender, dossier, params)
|
def build(sender, dossier, params)
|
||||||
attributes = params.merge(email: sender.email, dossier: dossier)
|
case sender
|
||||||
|
when User
|
||||||
# If the user submits a empty message, simple_format will replace '' by '<p></p>',
|
params[:user] = sender
|
||||||
# and thus bypass the not-empty constraint on commentaire's body.
|
when Gestionnaire
|
||||||
#
|
params[:gestionnaire] = sender
|
||||||
# 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])
|
|
||||||
end
|
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)
|
Commentaire.new(attributes)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
%span.guest Invité
|
%span.guest Invité
|
||||||
%span.date{ class: highlight_if_unseen_class(messagerie_seen_at, commentaire.created_at) }
|
%span.date{ class: highlight_if_unseen_class(messagerie_seen_at, commentaire.created_at) }
|
||||||
= commentaire_date(commentaire)
|
= commentaire_date(commentaire)
|
||||||
.rich-text= sanitize(commentaire.body)
|
.rich-text= sanitize(simple_format(commentaire.body))
|
||||||
|
|
||||||
- if commentaire.piece_justificative
|
- if commentaire.piece_justificative
|
||||||
.attachment-link
|
.attachment-link
|
||||||
|
|
|
@ -301,7 +301,7 @@ describe NewGestionnaire::DossiersController, type: :controller do
|
||||||
|
|
||||||
expect(response).to render_template :messagerie
|
expect(response).to render_template :messagerie
|
||||||
expect(flash.alert).to be_present
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -787,7 +787,7 @@ describe NewUser::DossiersController, type: :controller do
|
||||||
|
|
||||||
expect(response).to render_template :messagerie
|
expect(response).to render_template :messagerie
|
||||||
expect(flash.alert).to be_present
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@ describe CommentaireService do
|
||||||
let(:file) { nil }
|
let(:file) { nil }
|
||||||
let(:scan_result) { true }
|
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
|
before do
|
||||||
allow(ClamavService).to receive(:safe_file?).and_return(scan_result)
|
allow(ClamavService).to receive(:safe_file?).and_return(scan_result)
|
||||||
|
@ -17,7 +17,7 @@ describe CommentaireService do
|
||||||
it 'creates a new valid commentaire' do
|
it 'creates a new valid commentaire' do
|
||||||
expect(commentaire.email).to eq sender.email
|
expect(commentaire.email).to eq sender.email
|
||||||
expect(commentaire.dossier).to eq dossier
|
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.file).to be_blank
|
||||||
expect(commentaire).to be_valid
|
expect(commentaire).to be_valid
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue