mailers: use the standard view mechanism to render notifications

Instead of rendering a view as a template, consider the view as a normal
view.
This commit is contained in:
Pierre de La Morinerie 2019-07-22 13:49:11 +00:00
parent 5bbcd600fb
commit 858b093314
7 changed files with 36 additions and 12 deletions

View file

@ -8,8 +8,9 @@ module NewAdministrateur
@service = procedure.service @service = procedure.service
mail_template = find_mail_template_by_slug(params[:id]) mail_template = find_mail_template_by_slug(params[:id])
@rendered_template = sanitize(mail_template.body)
render(html: sanitize(mail_template.body), layout: 'mailers/notification') render(template: 'notification_mailer/send_notification', layout: 'mailers/notifications_layout')
end end
private private

View file

@ -13,7 +13,7 @@ class DossierMailer < ApplicationMailer
subject = "Retrouvez votre brouillon pour la démarche « #{dossier.procedure.libelle} »" subject = "Retrouvez votre brouillon pour la démarche « #{dossier.procedure.libelle} »"
mail(to: dossier.user.email, subject: subject) do |format| mail(to: dossier.user.email, subject: subject) do |format|
format.html { render layout: 'mailers/notification' } format.html { render layout: 'mailers/notifications_layout' }
end end
end end
@ -25,7 +25,7 @@ class DossierMailer < ApplicationMailer
subject = "Nouveau message pour votre dossier nº #{dossier.id} (#{dossier.procedure.libelle})" subject = "Nouveau message pour votre dossier nº #{dossier.id} (#{dossier.procedure.libelle})"
mail(to: dossier.user.email, subject: subject) do |format| mail(to: dossier.user.email, subject: subject) do |format|
format.html { render layout: 'mailers/notification' } format.html { render layout: 'mailers/notifications_layout' }
end end
end end

View file

@ -6,8 +6,12 @@
# The subject and body of a Notification can be customized by each demarche. # The subject and body of a Notification can be customized by each demarche.
# #
class NotificationMailer < ApplicationMailer class NotificationMailer < ApplicationMailer
include ActionView::Helpers::SanitizeHelper
helper ServiceHelper helper ServiceHelper
layout 'mailers/notifications_layout'
def send_dossier_received(dossier) def send_dossier_received(dossier)
send_notification(dossier, dossier.procedure.received_mail_template) send_notification(dossier, dossier.procedure.received_mail_template)
end end
@ -41,12 +45,9 @@ class NotificationMailer < ApplicationMailer
@dossier = dossier @dossier = dossier
@service = dossier.procedure.service @service = dossier.procedure.service
@logo_url = attach_logo(dossier.procedure) @logo_url = attach_logo(dossier.procedure)
@rendered_template = sanitize(body)
mail(subject: subject, to: email) do |format| mail(subject: subject, to: email, template_name: 'send_notification')
# rubocop:disable Rails/OutputSafety
format.html { render(html: body.html_safe, layout: 'mailers/notification') }
# rubocop:enable Rails/OutputSafety
end
end end
def create_commentaire_for_notification(dossier, subject, body) def create_commentaire_for_notification(dossier, subject, body)

View file

@ -1,3 +1,6 @@
- content_for :procedure_logo do
= render 'layouts/mailers/logo', url: @logo_url
%p %p
Bonjour, Bonjour,
@ -10,4 +13,7 @@
= round_button('Lire le message', messagerie_dossier_url(@dossier)) = round_button('Lire le message', messagerie_dossier_url(@dossier))
= render partial: "layouts/mailers/signature", locals: { service: @service } = render 'layouts/mailers/signature', service: @service
- content_for :footer do
= render 'layouts/mailers/service_footer', service: @service, dossier: @dossier

View file

@ -1,3 +1,6 @@
- content_for :procedure_logo do
= render 'layouts/mailers/logo', url: @logo_url
%p %p
Bonjour, Bonjour,
@ -10,4 +13,7 @@
à ladresse suivante : à ladresse suivante :
= link_to dossier_url(@dossier), dossier_url(@dossier), target: '_blank', rel: 'noopener' = link_to dossier_url(@dossier), dossier_url(@dossier), target: '_blank', rel: 'noopener'
= render partial: "layouts/mailers/signature" = render 'layouts/mailers/signature'
- content_for :footer do
= render 'layouts/mailers/service_footer', service: @service, dossier: @dossier

View file

@ -1,7 +1,7 @@
- content_for :procedure_logo do - content_for :procedure_logo do
= render 'layouts/mailers/logo', url: @logo_url = render 'layouts/mailers/logo', url: @logo_url
= @rendered_template
- content_for :footer do - content_for :footer do
= render 'layouts/mailers/service_footer', service: @service, dossier: @dossier = render 'layouts/mailers/service_footer', service: @service, dossier: @dossier
= render template: 'layouts/mailers/notifications_layout'

View file

@ -3,6 +3,12 @@ require "rails_helper"
RSpec.describe DossierMailer, type: :mailer do RSpec.describe DossierMailer, type: :mailer do
let(:to_email) { 'gestionnaire@exemple.gouv.fr' } let(:to_email) { 'gestionnaire@exemple.gouv.fr' }
shared_examples 'a dossier notification' do
it 'includes the contact informations in the footer' do
expect(subject.body).to include('ne pas répondre')
end
end
describe '.notify_new_draft' do describe '.notify_new_draft' do
let(:dossier) { create(:dossier, procedure: build(:simple_procedure)) } let(:dossier) { create(:dossier, procedure: build(:simple_procedure)) }
@ -12,6 +18,8 @@ RSpec.describe DossierMailer, type: :mailer do
it { expect(subject.subject).to include(dossier.procedure.libelle) } it { expect(subject.subject).to include(dossier.procedure.libelle) }
it { expect(subject.body).to include(dossier.procedure.libelle) } it { expect(subject.body).to include(dossier.procedure.libelle) }
it { expect(subject.body).to include(dossier_url(dossier)) } it { expect(subject.body).to include(dossier_url(dossier)) }
it_behaves_like 'a dossier notification'
end end
describe '.notify_new_answer' do describe '.notify_new_answer' do
@ -22,6 +30,8 @@ RSpec.describe DossierMailer, type: :mailer do
it { expect(subject.subject).to include("Nouveau message") } it { expect(subject.subject).to include("Nouveau message") }
it { expect(subject.subject).to include(dossier.id.to_s) } it { expect(subject.subject).to include(dossier.id.to_s) }
it { expect(subject.body).to include(messagerie_dossier_url(dossier)) } it { expect(subject.body).to include(messagerie_dossier_url(dossier)) }
it_behaves_like 'a dossier notification'
end end
describe '.notify_deletion_to_user' do describe '.notify_deletion_to_user' do