Merge pull request #3118 from tchak/messages-user_id

Add user_id and gestionnaire_id to commentaires
This commit is contained in:
Paul Chavard 2018-12-05 14:10:09 +01:00 committed by GitHub
commit 2e29825415
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 53 additions and 26 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

@ -0,0 +1,9 @@
class AddUserIdToCommentaires < ActiveRecord::Migration[5.2]
def change
add_column :commentaires, :user_id, :bigint
add_column :commentaires, :gestionnaire_id, :bigint
add_index :commentaires, :user_id
add_index :commentaires, :gestionnaire_id
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_11_23_195208) do
ActiveRecord::Schema.define(version: 2018_12_04_125101) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -189,7 +189,11 @@ ActiveRecord::Schema.define(version: 2018_11_23_195208) do
t.datetime "updated_at", null: false
t.integer "piece_justificative_id"
t.string "file"
t.bigint "user_id"
t.bigint "gestionnaire_id"
t.index ["dossier_id"], name: "index_commentaires_on_dossier_id"
t.index ["gestionnaire_id"], name: "index_commentaires_on_gestionnaire_id"
t.index ["user_id"], name: "index_commentaires_on_user_id"
end
create_table "delayed_jobs", id: :serial, force: :cascade do |t|

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