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
def index
@mails = mails
@mail_templates = mail_templates
end
def edit
@mail_template = find_the_right_mail params[:id]
@mail_template_name = params[:id]
@mail_template = find_mail_template_by_slug(params[:id])
end
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)
redirect_to admin_procedure_mail_templates_path
end
private
def mails
%w(initiated received closed refused without_continuation)
.map { |name| @procedure.send(name + "_mail") }
def mail_templates
[
@procedure.initiated_mail_template,
@procedure.received_mail_template,
@procedure.closed_mail_template,
@procedure.refused_mail_template,
@procedure.without_continuation_mail_template
]
end
def find_the_right_mail type
mails.find { |m| m.class.slug == type }
def find_mail_template_by_slug(slug)
mail_templates.find { |template| template.class.const_get(:SLUG) == slug }
end
def update_params

View file

@ -99,7 +99,7 @@ class Backoffice::DossiersController < Backoffice::DossiersListController
dossier.received!
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)
end
@ -112,7 +112,7 @@ class Backoffice::DossiersController < Backoffice::DossiersListController
dossier.next_step! 'gestionnaire', 'refuse'
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)
end
@ -125,7 +125,7 @@ class Backoffice::DossiersController < Backoffice::DossiersListController
dossier.next_step! 'gestionnaire', 'without_continuation'
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)
end
@ -138,7 +138,7 @@ class Backoffice::DossiersController < Backoffice::DossiersListController
dossier.next_step! 'gestionnaire', 'close'
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)
end

View file

@ -52,7 +52,7 @@ class Users::DescriptionController < UsersController
else
if dossier.draft?
dossier.initiated!
NotificationMailer.send_notification(dossier, procedure.initiated_mail).deliver_now!
NotificationMailer.send_notification(dossier, procedure.initiated_mail_template).deliver_now!
end
flash.notice = 'Félicitations, votre demande a bien été enregistrée.'
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
def send_notification dossier, mail_template
def send_notification(dossier, mail_template)
vars_mailer(dossier)
@obj = mail_template.object_for_dossier dossier
@object = mail_template.object_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
def new_answer dossier
def new_answer(dossier)
send_mail dossier, "Nouveau message pour votre dossier TPS nº #{dossier.id}"
end
@ -22,16 +22,16 @@ class NotificationMailer < ApplicationMailer
Commentaire.create(
dossier: @dossier,
email: I18n.t("dynamics.contact_email"),
body: ["[#{@obj}]", @body].join("<br><br>")
body: ["[#{@object}]", @body].join("<br><br>")
)
end
def vars_mailer dossier
def vars_mailer(dossier)
@dossier = dossier
@user = dossier.user
end
def send_mail dossier, subject
def send_mail(dossier, subject)
vars_mailer dossier
mail(subject: subject)

View file

@ -4,50 +4,23 @@ module MailTemplateConcern
include Rails.application.routes.url_helpers
include ActionView::Helpers::UrlHelper
TAGS = {
numero_dossier: {
description: "Permet d'afficher le numéro de dossier de l'utilisateur.",
templates: [
"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 = []
TAGS << TAG_NUMERO_DOSSIER = {
name: "numero_dossier",
description: "Permet d'afficher le numéro de dossier de l'utilisateur."
}
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)
replace_tags(object, dossier)
@ -59,17 +32,13 @@ module MailTemplateConcern
def replace_tags(string, dossier)
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
module ClassMethods
def slug
self.name.demodulize.underscore.parameterize
end
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)
end
end
@ -78,13 +47,13 @@ module MailTemplateConcern
def replace_tag(tag, dossier)
case tag
when :numero_dossier
when TAG_NUMERO_DOSSIER
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'
when :libelle_procedure
when TAG_LIBELLE_PROCEDURE
dossier.procedure.libelle
when :date_de_decision
when TAG_DATE_DE_DECISION
dossier.processed_at.present? ? dossier.processed_at.localtime.strftime("%d/%m/%Y") : ""
else
'--BALISE_NON_RECONNUE--'

View file

@ -1,9 +1,11 @@
module Mails
class ClosedMail < ActiveRecord::Base
class ClosedMail < ApplicationRecord
include MailTemplateConcern
SLUG = "closed_mail"
TEMPLATE_NAME = "mails/closed_mail"
DISPLAYED_NAME = "Accusé d'acceptation"
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

View file

@ -1,9 +1,11 @@
module Mails
class InitiatedMail < ActiveRecord::Base
class InitiatedMail < ApplicationRecord
include MailTemplateConcern
SLUG = "initiated_mail"
TEMPLATE_NAME = "mails/initiated_mail"
DISPLAYED_NAME = 'Accusé de réception'
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

View file

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

View file

@ -2,8 +2,10 @@ module Mails
class RefusedMail < ApplicationRecord
include MailTemplateConcern
SLUG = "refused_mail"
TEMPLATE_NAME = "mails/refused_mail"
DISPLAYED_NAME = 'Accusé de rejet du dossier'
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

View file

@ -2,8 +2,10 @@ module Mails
class WithoutContinuationMail < ApplicationRecord
include MailTemplateConcern
SLUG = "without_continuation"
TEMPLATE_NAME = "mails/without_continuation_mail"
DISPLAYED_NAME = 'Accusé de classement 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

View file

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

View file

@ -4,7 +4,7 @@
= simple_form_for @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|
.row
.col-md-6
@ -22,9 +22,9 @@
Balise
%th
Description
- MailTemplateConcern.tags_for_template(@mail_template_name).each do |balise|
- @mail_template.class.const_get(:ALLOWED_TAGS).each do |tag|
%tr
%td.center
= "--#{balise.first}--"
= "--#{tag[:name]}--"
%td
= balise.second[:description]
= tag[:description]

View file

@ -5,9 +5,9 @@
%tr
%th{ colspan: 2 }
Type d'email
- @mails.each do |mail|
- @mail_templates.each do |mail_template|
%tr
%td
= mail.class.const_get(:DISPLAYED_NAME)
= mail_template.class.const_get(:DISPLAYED_NAME)
%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
patch :update,
params: { procedure_id: procedure.id,
id: initiated_mail.class.slug,
id: initiated_mail.class.const_get(:SLUG),
mail_template: { object: object, body: body }
}
end
@ -33,7 +33,7 @@ describe Admin::MailTemplatesController, type: :controller do
it { expect(response).to redirect_to admin_procedure_mail_templates_path(procedure) }
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.body).to eq(body) }

View file

@ -1,7 +1,7 @@
class NotificationMailerPreview < ActionMailer::Preview
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

View file

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