From 70f637f088806e4879b96aaf8a5ab60f40e8c78b Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Thu, 11 May 2017 18:07:37 +0200 Subject: [PATCH] [Fix #198] Add a Commentaire when we send a Notification email to a user --- app/mailers/notification_mailer.rb | 16 +++++++++++++--- spec/mailers/notification_mailer_spec.rb | 13 +++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 7bca836bc..9bf486f17 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -1,13 +1,15 @@ class NotificationMailer < ApplicationMailer default to: Proc.new { @user.email } + after_action :create_commentaire_for_notification, only: :send_notification + def send_notification dossier, mail_template vars_mailer(dossier) - obj = mail_template.object_for_dossier dossier - body = mail_template.body_for_dossier dossier + @obj = mail_template.object_for_dossier dossier + @body = mail_template.body_for_dossier dossier - mail(subject: obj) { |format| format.html { body } } + mail(subject: @obj) { |format| format.html { @body } } end def new_answer dossier @@ -16,6 +18,14 @@ class NotificationMailer < ApplicationMailer private + def create_commentaire_for_notification + Commentaire.create( + dossier: @dossier, + email: "contact@tps.apientreprise.fr", + body: ["[#{@obj}]", @body].join("

") + ) + end + def vars_mailer dossier @dossier = dossier @user = dossier.user diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb index 4c8f26fc1..e9a66e4a7 100644 --- a/spec/mailers/notification_mailer_spec.rb +++ b/spec/mailers/notification_mailer_spec.rb @@ -5,10 +5,23 @@ RSpec.describe NotificationMailer, type: :mailer do let(:user) { create(:user) } let(:dossier) { create(:dossier, user: user) } let(:email) { instance_double('email', object_for_dossier: 'object', body_for_dossier: 'body') } + let (:notifications_count_before) { Notification.count } subject { described_class.send_notification(dossier, email) } it { expect(subject.subject).to eq(email.object_for_dossier) } it { expect(subject.body).to eq(email.body_for_dossier) } + + it "creates a commentaire, which is not notified" do + described_class.send_notification(dossier, email).deliver_now + + commentaire = Commentaire.last + notifications_count_after = Notification.count + + expect(commentaire.dossier).to eq(dossier) + expect(commentaire.email).to eq("contact@tps.apientreprise.fr") + expect(commentaire.body).to eq("[object]

body") + expect(notifications_count_before).to eq(notifications_count_after) + end end describe ".new_answer" do