Merge pull request #370 from sgmap/clean-emails

Clean emails
This commit is contained in:
gregoirenovel 2017-06-08 12:23:08 +02:00 committed by GitHub
commit 0be32def5c
16 changed files with 101 additions and 122 deletions

View file

@ -2,29 +2,33 @@ class Admin::MailTemplatesController < AdminController
before_action :retrieve_procedure before_action :retrieve_procedure
def index def index
@mails = mails @mail_templates = mail_templates
end end
def edit def edit
@mail_template = find_the_right_mail params[:id] @mail_template = find_mail_template_by_slug(params[:id])
@mail_template_name = params[:id]
end end
def update def update
mail_template = find_the_right_mail params[:id] mail_template = find_mail_template_by_slug(params[:id])
mail_template.update_attributes(update_params) mail_template.update_attributes(update_params)
redirect_to admin_procedure_mail_templates_path redirect_to admin_procedure_mail_templates_path
end end
private private
def mails def mail_templates
%w(initiated received closed refused without_continuation) [
.map { |name| @procedure.send(name + "_mail") } @procedure.initiated_mail_template,
@procedure.received_mail_template,
@procedure.closed_mail_template,
@procedure.refused_mail_template,
@procedure.without_continuation_mail_template
]
end end
def find_the_right_mail type def find_mail_template_by_slug(slug)
mails.find { |m| m.class.slug == type } mail_templates.find { |template| template.class.const_get(:SLUG) == slug }
end end
def update_params def update_params

View file

@ -99,7 +99,7 @@ class Backoffice::DossiersController < Backoffice::DossiersListController
dossier.received! dossier.received!
flash.notice = 'Dossier considéré comme reçu.' flash.notice = 'Dossier considéré comme reçu.'
NotificationMailer.send_notification(dossier, dossier.procedure.received_mail).deliver_now! NotificationMailer.send_notification(dossier, dossier.procedure.received_mail_template).deliver_now!
redirect_to backoffice_dossier_path(id: dossier.id) redirect_to backoffice_dossier_path(id: dossier.id)
end end
@ -112,7 +112,7 @@ class Backoffice::DossiersController < Backoffice::DossiersListController
dossier.next_step! 'gestionnaire', 'refuse' dossier.next_step! 'gestionnaire', 'refuse'
flash.notice = 'Dossier considéré comme refusé.' flash.notice = 'Dossier considéré comme refusé.'
NotificationMailer.send_notification(dossier, dossier.procedure.refused_mail).deliver_now! NotificationMailer.send_notification(dossier, dossier.procedure.refused_mail_template).deliver_now!
redirect_to backoffice_dossier_path(id: dossier.id) redirect_to backoffice_dossier_path(id: dossier.id)
end end
@ -125,7 +125,7 @@ class Backoffice::DossiersController < Backoffice::DossiersListController
dossier.next_step! 'gestionnaire', 'without_continuation' dossier.next_step! 'gestionnaire', 'without_continuation'
flash.notice = 'Dossier considéré comme sans suite.' flash.notice = 'Dossier considéré comme sans suite.'
NotificationMailer.send_notification(dossier, dossier.procedure.without_continuation_mail).deliver_now! NotificationMailer.send_notification(dossier, dossier.procedure.without_continuation_mail_template).deliver_now!
redirect_to backoffice_dossier_path(id: dossier.id) redirect_to backoffice_dossier_path(id: dossier.id)
end end
@ -138,7 +138,7 @@ class Backoffice::DossiersController < Backoffice::DossiersListController
dossier.next_step! 'gestionnaire', 'close' dossier.next_step! 'gestionnaire', 'close'
flash.notice = 'Dossier traité avec succès.' flash.notice = 'Dossier traité avec succès.'
NotificationMailer.send_notification(dossier, dossier.procedure.closed_mail).deliver_now! NotificationMailer.send_notification(dossier, dossier.procedure.closed_mail_template).deliver_now!
redirect_to backoffice_dossier_path(id: dossier.id) redirect_to backoffice_dossier_path(id: dossier.id)
end end

View file

@ -52,7 +52,7 @@ class Users::DescriptionController < UsersController
else else
if dossier.draft? if dossier.draft?
dossier.initiated! dossier.initiated!
NotificationMailer.send_notification(dossier, procedure.initiated_mail).deliver_now! NotificationMailer.send_notification(dossier, procedure.initiated_mail_template).deliver_now!
end end
flash.notice = 'Félicitations, votre demande a bien été enregistrée.' flash.notice = 'Félicitations, votre demande a bien été enregistrée.'
redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: dossier.id) redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: dossier.id)

View file

@ -3,16 +3,16 @@ class NotificationMailer < ApplicationMailer
after_action :create_commentaire_for_notification, only: :send_notification after_action :create_commentaire_for_notification, only: :send_notification
def send_notification dossier, mail_template def send_notification(dossier, mail_template)
vars_mailer(dossier) vars_mailer(dossier)
@obj = mail_template.object_for_dossier dossier @object = mail_template.object_for_dossier dossier
@body = mail_template.body_for_dossier dossier @body = mail_template.body_for_dossier dossier
mail(subject: @obj) { |format| format.html { @body } } mail(subject: @object) { |format| format.html { @body } }
end end
def new_answer dossier def new_answer(dossier)
send_mail dossier, "Nouveau message pour votre dossier TPS nº #{dossier.id}" send_mail dossier, "Nouveau message pour votre dossier TPS nº #{dossier.id}"
end end
@ -22,16 +22,16 @@ class NotificationMailer < ApplicationMailer
Commentaire.create( Commentaire.create(
dossier: @dossier, dossier: @dossier,
email: I18n.t("dynamics.contact_email"), email: I18n.t("dynamics.contact_email"),
body: ["[#{@obj}]", @body].join("<br><br>") body: ["[#{@object}]", @body].join("<br><br>")
) )
end end
def vars_mailer dossier def vars_mailer(dossier)
@dossier = dossier @dossier = dossier
@user = dossier.user @user = dossier.user
end end
def send_mail dossier, subject def send_mail(dossier, subject)
vars_mailer dossier vars_mailer dossier
mail(subject: subject) mail(subject: subject)

View file

@ -4,50 +4,23 @@ module MailTemplateConcern
include Rails.application.routes.url_helpers include Rails.application.routes.url_helpers
include ActionView::Helpers::UrlHelper include ActionView::Helpers::UrlHelper
TAGS = { TAGS = []
numero_dossier: { TAGS << TAG_NUMERO_DOSSIER = {
description: "Permet d'afficher le numéro de dossier de l'utilisateur.", name: "numero_dossier",
templates: [ description: "Permet d'afficher le numéro de dossier de l'utilisateur."
"initiated_mail",
"received_mail",
"closed_mail",
"refused_mail",
"without_continuation_mail"
]
},
lien_dossier: {
description: "Permet d'afficher un lien vers le dossier de l'utilisateur.",
templates: [
"initiated_mail",
"received_mail",
"closed_mail",
"refused_mail",
"without_continuation_mail"
]
},
libelle_procedure: {
description: "Permet d'afficher le libellé de la procédure.",
templates: [
"initiated_mail",
"received_mail",
"closed_mail",
"refused_mail",
"without_continuation_mail"
]
},
date_de_decision: {
description: "Permet d'afficher la date à laquelle la décision finale (acceptation, refus, classement sans suite) sur le dossier a été prise.",
templates: [
"closed_mail",
"refused_mail",
"without_continuation_mail"
]
} }
TAGS << TAG_LIEN_DOSSIER = {
name: "lien_dossier",
description: "Permet d'afficher un lien vers le dossier de l'utilisateur."
}
TAGS << TAG_LIBELLE_PROCEDURE = {
name: "libelle_procedure",
description: "Permet d'afficher le libellé de la procédure."
}
TAGS << TAG_DATE_DE_DECISION = {
name: "date_de_decision",
description: "Permet d'afficher la date à laquelle la décision finale (acceptation, refus, classement sans suite) sur le dossier a été prise."
} }
def self.tags_for_template(template)
TAGS.select { |key, value| value[:templates].include?(template) }
end
def object_for_dossier(dossier) def object_for_dossier(dossier)
replace_tags(object, dossier) replace_tags(object, dossier)
@ -59,17 +32,13 @@ module MailTemplateConcern
def replace_tags(string, dossier) def replace_tags(string, dossier)
TAGS.inject(string) do |acc, tag| TAGS.inject(string) do |acc, tag|
acc.gsub!("--#{tag.first}--", replace_tag(tag.first.to_sym, dossier)) || acc acc.gsub!("--#{tag[:name]}--", replace_tag(tag, dossier)) || acc
end end
end end
module ClassMethods module ClassMethods
def slug
self.name.demodulize.underscore.parameterize
end
def default def default
body = ActionController::Base.new.render_to_string(template: self.name.underscore) body = ActionController::Base.new.render_to_string(template: self.const_get(:TEMPLATE_NAME))
self.new(object: self.const_get(:DEFAULT_OBJECT), body: body) self.new(object: self.const_get(:DEFAULT_OBJECT), body: body)
end end
end end
@ -78,13 +47,13 @@ module MailTemplateConcern
def replace_tag(tag, dossier) def replace_tag(tag, dossier)
case tag case tag
when :numero_dossier when TAG_NUMERO_DOSSIER
dossier.id.to_s dossier.id.to_s
when :lien_dossier when TAG_LIEN_DOSSIER
link_to users_dossier_recapitulatif_url(dossier), users_dossier_recapitulatif_url(dossier), target: '_blank' link_to users_dossier_recapitulatif_url(dossier), users_dossier_recapitulatif_url(dossier), target: '_blank'
when :libelle_procedure when TAG_LIBELLE_PROCEDURE
dossier.procedure.libelle dossier.procedure.libelle
when :date_de_decision when TAG_DATE_DE_DECISION
dossier.processed_at.present? ? dossier.processed_at.localtime.strftime("%d/%m/%Y") : "" dossier.processed_at.present? ? dossier.processed_at.localtime.strftime("%d/%m/%Y") : ""
else else
'--BALISE_NON_RECONNUE--' '--BALISE_NON_RECONNUE--'

View file

@ -1,9 +1,11 @@
module Mails module Mails
class ClosedMail < ActiveRecord::Base class ClosedMail < ApplicationRecord
include MailTemplateConcern include MailTemplateConcern
SLUG = "closed_mail"
TEMPLATE_NAME = "mails/closed_mail"
DISPLAYED_NAME = "Accusé d'acceptation" DISPLAYED_NAME = "Accusé d'acceptation"
DEFAULT_OBJECT = 'Votre dossier TPS nº --numero_dossier-- a été accepté' DEFAULT_OBJECT = 'Votre dossier TPS nº --numero_dossier-- a été accepté'
ALLOWED_TAGS = [TAG_NUMERO_DOSSIER, TAG_LIEN_DOSSIER, TAG_LIBELLE_PROCEDURE, TAG_DATE_DE_DECISION]
end end
end end

View file

@ -1,9 +1,11 @@
module Mails module Mails
class InitiatedMail < ActiveRecord::Base class InitiatedMail < ApplicationRecord
include MailTemplateConcern include MailTemplateConcern
SLUG = "initiated_mail"
TEMPLATE_NAME = "mails/initiated_mail"
DISPLAYED_NAME = 'Accusé de réception' DISPLAYED_NAME = 'Accusé de réception'
DEFAULT_OBJECT = 'Votre dossier TPS nº --numero_dossier-- a été bien reçu' DEFAULT_OBJECT = 'Votre dossier TPS nº --numero_dossier-- a été bien reçu'
ALLOWED_TAGS = [TAG_NUMERO_DOSSIER, TAG_LIEN_DOSSIER, TAG_LIBELLE_PROCEDURE]
end end
end end

View file

@ -1,9 +1,11 @@
module Mails module Mails
class ReceivedMail < ActiveRecord::Base class ReceivedMail < ApplicationRecord
include MailTemplateConcern include MailTemplateConcern
SLUG = "received_mail"
TEMPLATE_NAME = "mails/received_mail"
DISPLAYED_NAME = 'Accusé de passage en instruction' DISPLAYED_NAME = 'Accusé de passage en instruction'
DEFAULT_OBJECT = 'Votre dossier TPS nº --numero_dossier-- va être instruit' DEFAULT_OBJECT = 'Votre dossier TPS nº --numero_dossier-- va être instruit'
ALLOWED_TAGS = [TAG_NUMERO_DOSSIER, TAG_LIEN_DOSSIER, TAG_LIBELLE_PROCEDURE]
end end
end end

View file

@ -2,8 +2,10 @@ module Mails
class RefusedMail < ApplicationRecord class RefusedMail < ApplicationRecord
include MailTemplateConcern include MailTemplateConcern
SLUG = "refused_mail"
TEMPLATE_NAME = "mails/refused_mail"
DISPLAYED_NAME = 'Accusé de rejet du dossier' DISPLAYED_NAME = 'Accusé de rejet du dossier'
DEFAULT_OBJECT = 'Votre dossier TPS nº --numero_dossier-- a été refusé' DEFAULT_OBJECT = 'Votre dossier TPS nº --numero_dossier-- a été refusé'
ALLOWED_TAGS = [TAG_NUMERO_DOSSIER, TAG_LIEN_DOSSIER, TAG_LIBELLE_PROCEDURE, TAG_DATE_DE_DECISION]
end end
end end

View file

@ -2,8 +2,10 @@ module Mails
class WithoutContinuationMail < ApplicationRecord class WithoutContinuationMail < ApplicationRecord
include MailTemplateConcern include MailTemplateConcern
SLUG = "without_continuation"
TEMPLATE_NAME = "mails/without_continuation_mail"
DISPLAYED_NAME = 'Accusé de classement sans suite' DISPLAYED_NAME = 'Accusé de classement sans suite'
DEFAULT_OBJECT = 'Votre dossier TPS nº --numero_dossier-- a été classé sans suite' DEFAULT_OBJECT = 'Votre dossier TPS nº --numero_dossier-- a été classé sans suite'
ALLOWED_TAGS = [TAG_NUMERO_DOSSIER, TAG_LIEN_DOSSIER, TAG_LIBELLE_PROCEDURE, TAG_DATE_DE_DECISION]
end end
end end

View file

@ -99,11 +99,11 @@ class Procedure < ActiveRecord::Base
procedure.logo_secure_token = nil procedure.logo_secure_token = nil
procedure.remote_logo_url = self.logo_url procedure.remote_logo_url = self.logo_url
procedure.initiated_mail = initiated_mail_without_override.try(:dup) procedure.initiated_mail = initiated_mail.try(:dup)
procedure.received_mail = received_mail_without_override.try(:dup) procedure.received_mail = received_mail.try(:dup)
procedure.closed_mail = closed_mail_without_override.try(:dup) procedure.closed_mail = closed_mail.try(:dup)
procedure.refused_mail = refused_mail_without_override.try(:dup) procedure.refused_mail = refused_mail.try(:dup)
procedure.without_continuation_mail = without_continuation_mail_without_override.try(:dup) procedure.without_continuation_mail = without_continuation_mail.try(:dup)
return procedure if procedure.save return procedure if procedure.save
end end
@ -137,28 +137,23 @@ class Procedure < ActiveRecord::Base
ProcedureOverview.new(self, start_date, notifications_count) ProcedureOverview.new(self, start_date, notifications_count)
end end
def initiated_mail_with_override def initiated_mail_template
self.initiated_mail_without_override || Mails::InitiatedMail.default initiated_mail || Mails::InitiatedMail.default
end end
alias_method_chain "initiated_mail", :override
def received_mail_with_override def received_mail_template
self.received_mail_without_override || Mails::ReceivedMail.default received_mail || Mails::ReceivedMail.default
end end
alias_method_chain "received_mail", :override
def closed_mail_with_override def closed_mail_template
self.closed_mail_without_override || Mails::ClosedMail.default closed_mail || Mails::ClosedMail.default
end end
alias_method_chain "closed_mail", :override
def refused_mail_with_override def refused_mail_template
self.refused_mail_without_override || Mails::RefusedMail.default refused_mail || Mails::RefusedMail.default
end end
alias_method_chain "refused_mail", :override
def without_continuation_mail_with_override def without_continuation_mail_template
self.without_continuation_mail_without_override || Mails::WithoutContinuationMail.default without_continuation_mail || Mails::WithoutContinuationMail.default
end end
alias_method_chain "without_continuation_mail", :override
end end

View file

@ -4,7 +4,7 @@
= simple_form_for @mail_template, = simple_form_for @mail_template,
as: 'mail_template', as: 'mail_template',
url: admin_procedure_mail_template_path(@procedure, @mail_template.class.slug), url: admin_procedure_mail_template_path(@procedure, @mail_template.class.const_get(:SLUG)),
method: :put do |f| method: :put do |f|
.row .row
.col-md-6 .col-md-6
@ -22,9 +22,9 @@
Balise Balise
%th %th
Description Description
- MailTemplateConcern.tags_for_template(@mail_template_name).each do |balise| - @mail_template.class.const_get(:ALLOWED_TAGS).each do |tag|
%tr %tr
%td.center %td.center
= "--#{balise.first}--" = "--#{tag[:name]}--"
%td %td
= balise.second[:description] = tag[:description]

View file

@ -5,9 +5,9 @@
%tr %tr
%th{ colspan: 2 } %th{ colspan: 2 }
Type d'email Type d'email
- @mails.each do |mail| - @mail_templates.each do |mail_template|
%tr %tr
%td %td
= mail.class.const_get(:DISPLAYED_NAME) = mail_template.class.const_get(:DISPLAYED_NAME)
%td.text-right %td.text-right
= link_to "Personnaliser l'e-mail", edit_admin_procedure_mail_template_path(@procedure, mail.class.slug) = link_to "Personnaliser l'e-mail", edit_admin_procedure_mail_template_path(@procedure, mail_template.class.const_get(:SLUG))

View file

@ -25,7 +25,7 @@ describe Admin::MailTemplatesController, type: :controller do
before :each do before :each do
patch :update, patch :update,
params: { procedure_id: procedure.id, params: { procedure_id: procedure.id,
id: initiated_mail.class.slug, id: initiated_mail.class.const_get(:SLUG),
mail_template: { object: object, body: body } mail_template: { object: object, body: body }
} }
end end
@ -33,7 +33,7 @@ describe Admin::MailTemplatesController, type: :controller do
it { expect(response).to redirect_to admin_procedure_mail_templates_path(procedure) } it { expect(response).to redirect_to admin_procedure_mail_templates_path(procedure) }
context 'the mail template' do context 'the mail template' do
subject { procedure.reload ; procedure.initiated_mail } subject { procedure.reload ; procedure.initiated_mail_template }
it { expect(subject.object).to eq(object) } it { expect(subject.object).to eq(object) }
it { expect(subject.body).to eq(body) } it { expect(subject.body).to eq(body) }

View file

@ -1,7 +1,7 @@
class NotificationMailerPreview < ActionMailer::Preview class NotificationMailerPreview < ActionMailer::Preview
def send_notification def send_notification
NotificationMailer.send_notification(Dossier.last, Dossier.last.procedure.initiated_mail) NotificationMailer.send_notification(Dossier.last, Dossier.last.procedure.initiated_mail_template)
end end
end end

View file

@ -1,20 +1,21 @@
require 'spec_helper' require 'spec_helper'
describe Procedure do describe Procedure do
describe 'mail templates' do
subject { create(:procedure) }
describe 'mails' do it { expect(subject.initiated_mail_template).to be_a(Mails::InitiatedMail) }
it { expect(subject.initiated_mail).to be_a(Mails::InitiatedMail) } it { expect(subject.received_mail_template).to be_a(Mails::ReceivedMail) }
it { expect(subject.received_mail).to be_a(Mails::ReceivedMail) } it { expect(subject.closed_mail_template).to be_a(Mails::ClosedMail) }
it { expect(subject.closed_mail).to be_a(Mails::ClosedMail) } it { expect(subject.refused_mail_template).to be_a(Mails::RefusedMail) }
it { expect(subject.refused_mail).to be_a(Mails::RefusedMail) } it { expect(subject.without_continuation_mail_template).to be_a(Mails::WithoutContinuationMail) }
it { expect(subject.without_continuation_mail).to be_a(Mails::WithoutContinuationMail) }
end end
describe 'initiated_mail' do describe 'initiated_mail' do
subject { create(:procedure) } subject { create(:procedure) }
context 'when initiated_mail is not customize' do context 'when initiated_mail is not customize' do
it { expect(subject.initiated_mail.body).to eq(Mails::InitiatedMail.default.body) } it { expect(subject.initiated_mail_template.body).to eq(Mails::InitiatedMail.default.body) }
end end
context 'when initiated_mail is customize' do context 'when initiated_mail is customize' do
@ -23,7 +24,7 @@ describe Procedure do
subject.save subject.save
subject.reload subject.reload
end end
it { expect(subject.initiated_mail.body).to eq('sisi') } it { expect(subject.initiated_mail_template.body).to eq('sisi') }
end end
context 'when initiated_mail is customize ... again' do context 'when initiated_mail is customize ... again' do
@ -32,7 +33,7 @@ describe Procedure do
subject.save subject.save
subject.reload subject.reload
end end
it { expect(subject.initiated_mail.body).to eq('toto') } it { expect(subject.initiated_mail_template.body).to eq('toto') }
it { expect(Mails::InitiatedMail.count).to eq(1) } it { expect(Mails::InitiatedMail.count).to eq(1) }
end end
@ -186,7 +187,7 @@ describe Procedure do
end end
it 'should not duplicate default mail_template' do it 'should not duplicate default mail_template' do
expect(subject.initiated_mail.attributes).to eq Mails::InitiatedMail.default.attributes expect(subject.initiated_mail_template.attributes).to eq Mails::InitiatedMail.default.attributes
end end
it 'should not duplicate specific related objects' do it 'should not duplicate specific related objects' do