Move CommentairesController#notify_user_user_with_mail to Commentaire

This commit is contained in:
gregoirenovel 2017-05-12 13:40:32 +02:00
parent cae3e1f420
commit 9c3cde2b2e
3 changed files with 6 additions and 33 deletions

View file

@ -35,15 +35,12 @@ class CommentairesController < ApplicationController
end
@commentaire.body = params['texte_commentaire']
saved = false
unless @commentaire.body.blank? && @commentaire.piece_justificative.nil?
saved = @commentaire.save unless flash.alert
@commentaire.save unless flash.alert
else
flash.alert = "Veuillez rédiger un message ou ajouter une pièce jointe."
end
notify_user_with_mail(@commentaire) if saved
if is_gestionnaire?
unless current_gestionnaire.follow? @commentaire.dossier
current_gestionnaire.toggle_follow_dossier @commentaire.dossier
@ -63,10 +60,4 @@ class CommentairesController < ApplicationController
def is_gestionnaire?
false
end
private
def notify_user_with_mail(commentaire)
NotificationMailer.new_answer(commentaire.dossier).deliver_now! unless current_user.try(:email) == commentaire.dossier.user.email
end
end

View file

@ -5,6 +5,7 @@ class Commentaire < ActiveRecord::Base
belongs_to :piece_justificative
after_save :notify_gestionnaires
after_save :notify_user_with_mail
def header
"#{email}, " + I18n.l(created_at.localtime, format: '%d %b %Y %H:%M')
@ -17,4 +18,8 @@ class Commentaire < ActiveRecord::Base
NotificationService.new('commentaire', self.dossier.id).notify
end
end
def notify_user_with_mail
NotificationMailer.new_answer(dossier).deliver_now! unless (current_user.try(:email) == dossier.user.email || email == 'contact@tps.apientreprise.fr')
end
end

View file

@ -45,27 +45,4 @@ describe Users::Dossiers::CommentairesController, type: :controller do
end
end
describe '#notify_user_with_mail' do
let(:commentaire){create(:commentaire)}
context 'when usager is writing a commentaire on dossier' do
before { sign_in commentaire.dossier.user }
it {
expect(NotificationMailer).to_not receive(:new_answer)
subject.send(:notify_user_with_mail, commentaire)
}
end
context 'when anybody else but usager is writing a commentaire' do
before { sign_in create(:user, email: 'administrateur@test.fr') }
it {
expect(NotificationMailer).to receive(:new_answer).and_return(NotificationMailer)
expect(NotificationMailer).to receive(:deliver_now!)
subject.send(:notify_user_with_mail, commentaire)
}
end
end
end