From 65e83dd6ec9a342c3fef40ba0a6c0ac3e576a7a6 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Mon, 6 Mar 2017 23:18:16 +0100 Subject: [PATCH] Mails: factorize default and change slug --- app/models/concerns/mail_template_concern.rb | 7 ++++++- app/models/mails/closed_mail.rb | 10 ++-------- app/models/mails/initiated_mail.rb | 10 ++-------- app/models/mails/received_mail.rb | 10 ++-------- app/models/mails/refused_mail.rb | 10 ++-------- app/models/mails/without_continuation_mail.rb | 10 ++-------- app/views/admin/mail_templates/edit.html.haml | 2 +- app/views/admin/mail_templates/index.html.haml | 2 +- .../closed_mail.html.haml | 0 .../initiated_mail.html.haml | 0 .../received_mail.html.haml | 0 .../refused_mail.html.haml | 0 .../without_continuation_mail.html.haml | 0 .../admin/mail_templates_controller_spec.rb | 2 +- 14 files changed, 19 insertions(+), 44 deletions(-) rename app/views/{notification_mailer => mails}/closed_mail.html.haml (100%) rename app/views/{notification_mailer => mails}/initiated_mail.html.haml (100%) rename app/views/{notification_mailer => mails}/received_mail.html.haml (100%) rename app/views/{notification_mailer => mails}/refused_mail.html.haml (100%) rename app/views/{notification_mailer => mails}/without_continuation_mail.html.haml (100%) diff --git a/app/models/concerns/mail_template_concern.rb b/app/models/concerns/mail_template_concern.rb index f63d68990..f0e9863e9 100644 --- a/app/models/concerns/mail_template_concern.rb +++ b/app/models/concerns/mail_template_concern.rb @@ -32,7 +32,12 @@ module MailTemplateConcern module ClassMethods def slug - self.name.underscore + self.name.underscore.parameterize + end + + def default + body = ActionController::Base.new.render_to_string(template: self.name.underscore) + self.new(object: self.const_get(:DEFAULT_OBJECT), body: body) end end diff --git a/app/models/mails/closed_mail.rb b/app/models/mails/closed_mail.rb index 0e6052a75..3db0d84dc 100644 --- a/app/models/mails/closed_mail.rb +++ b/app/models/mails/closed_mail.rb @@ -2,14 +2,8 @@ module Mails class ClosedMail < ActiveRecord::Base include MailTemplateConcern - def name - "Accusé d'acceptation" - end + DISPLAYED_NAME = "Accusé d'acceptation" + DEFAULT_OBJECT = 'Votre dossier TPS N°--numero_dossier-- a été accepté' - def self.default - obj = "Votre dossier TPS N°--numero_dossier-- a été accepté" - body = ActionController::Base.new.render_to_string(template: 'notification_mailer/closed_mail') - ClosedMail.new(object: obj, body: body) - end end end diff --git a/app/models/mails/initiated_mail.rb b/app/models/mails/initiated_mail.rb index 03b34c59b..e39aa0e7a 100644 --- a/app/models/mails/initiated_mail.rb +++ b/app/models/mails/initiated_mail.rb @@ -2,14 +2,8 @@ module Mails class InitiatedMail < ActiveRecord::Base include MailTemplateConcern - def name - "Accusé de réception" - end + DISPLAYED_NAME = 'Accusé de réception' + DEFAULT_OBJECT = 'Votre dossier TPS N°--numero_dossier-- a été bien reçu' - def self.default - obj = "Votre dossier TPS N°--numero_dossier-- a été bien reçu" - body = ActionController::Base.new.render_to_string(template: 'notification_mailer/initiated_mail') - InitiatedMail.new(object: obj, body: body) - end end end diff --git a/app/models/mails/received_mail.rb b/app/models/mails/received_mail.rb index 04d4b98ef..41344f6ca 100644 --- a/app/models/mails/received_mail.rb +++ b/app/models/mails/received_mail.rb @@ -2,14 +2,8 @@ module Mails class ReceivedMail < ActiveRecord::Base include MailTemplateConcern - def name - "Accusé de passage en instruction" - end + DISPLAYED_NAME = 'Accusé de passage en instruction' + DEFAULT_OBJECT = 'Votre dossier TPS N°--numero_dossier-- va être instruit' - def self.default - obj = "Votre dossier TPS N°--numero_dossier-- va être instruit" - body = ActionController::Base.new.render_to_string(template: 'notification_mailer/received_mail') - ReceivedMail.new(object: obj, body: body) - end end end diff --git a/app/models/mails/refused_mail.rb b/app/models/mails/refused_mail.rb index 45c876c02..a1a1af8b4 100644 --- a/app/models/mails/refused_mail.rb +++ b/app/models/mails/refused_mail.rb @@ -2,14 +2,8 @@ module Mails class RefusedMail < ApplicationRecord include MailTemplateConcern - def name - "Accusé de rejet du dossier" - end + DISPLAYED_NAME = 'Accusé de rejet du dossier' + DEFAULT_OBJECT = 'Votre dossier TPS N°--numero_dossier-- a été refusé' - def self.default - obj = "Votre dossier TPS N°--numero_dossier-- a été refusé" - body = ActionController::Base.new.render_to_string(template: 'notification_mailer/refused_mail') - RefusedMail.new(object: obj, body: body) - end end end diff --git a/app/models/mails/without_continuation_mail.rb b/app/models/mails/without_continuation_mail.rb index d5c61a323..da9724cf6 100644 --- a/app/models/mails/without_continuation_mail.rb +++ b/app/models/mails/without_continuation_mail.rb @@ -2,14 +2,8 @@ module Mails class WithoutContinuationMail < ApplicationRecord include MailTemplateConcern - def name - "Accusé de classement sans suite" - end + DISPLAYED_NAME = 'Accusé de classement sans suite' + DEFAULT_OBJECT = 'Votre dossier TPS N°--numero_dossier-- a été classé sans suite' - def self.default - obj = "Votre dossier TPS N°--numero_dossier-- a été classé sans suite" - body = ActionController::Base.new.render_to_string(template: 'notification_mailer/without_continuation_mail') - WithoutContinuationMail.new(object: obj, body: body) - end end end diff --git a/app/views/admin/mail_templates/edit.html.haml b/app/views/admin/mail_templates/edit.html.haml index 4e1aa67ba..35efe2506 100644 --- a/app/views/admin/mail_templates/edit.html.haml +++ b/app/views/admin/mail_templates/edit.html.haml @@ -1,6 +1,6 @@ .white-back %h3 - = @mail_template.name + = @mail_template.class.const_get(:DISPLAYED_NAME) = simple_form_for @mail_template, as: 'mail_template', diff --git a/app/views/admin/mail_templates/index.html.haml b/app/views/admin/mail_templates/index.html.haml index 26a55b850..b8eff250a 100644 --- a/app/views/admin/mail_templates/index.html.haml +++ b/app/views/admin/mail_templates/index.html.haml @@ -8,6 +8,6 @@ - @mails.each do |mail| %tr %td - = mail.name + = mail.class.const_get(:DISPLAYED_NAME) %td.text-right = link_to "Personnaliser l'e-mail", edit_admin_procedure_mail_template_path(@procedure, mail.class.slug) diff --git a/app/views/notification_mailer/closed_mail.html.haml b/app/views/mails/closed_mail.html.haml similarity index 100% rename from app/views/notification_mailer/closed_mail.html.haml rename to app/views/mails/closed_mail.html.haml diff --git a/app/views/notification_mailer/initiated_mail.html.haml b/app/views/mails/initiated_mail.html.haml similarity index 100% rename from app/views/notification_mailer/initiated_mail.html.haml rename to app/views/mails/initiated_mail.html.haml diff --git a/app/views/notification_mailer/received_mail.html.haml b/app/views/mails/received_mail.html.haml similarity index 100% rename from app/views/notification_mailer/received_mail.html.haml rename to app/views/mails/received_mail.html.haml diff --git a/app/views/notification_mailer/refused_mail.html.haml b/app/views/mails/refused_mail.html.haml similarity index 100% rename from app/views/notification_mailer/refused_mail.html.haml rename to app/views/mails/refused_mail.html.haml diff --git a/app/views/notification_mailer/without_continuation_mail.html.haml b/app/views/mails/without_continuation_mail.html.haml similarity index 100% rename from app/views/notification_mailer/without_continuation_mail.html.haml rename to app/views/mails/without_continuation_mail.html.haml diff --git a/spec/controllers/admin/mail_templates_controller_spec.rb b/spec/controllers/admin/mail_templates_controller_spec.rb index 1bcae94b8..31adfd4ee 100644 --- a/spec/controllers/admin/mail_templates_controller_spec.rb +++ b/spec/controllers/admin/mail_templates_controller_spec.rb @@ -15,7 +15,7 @@ describe Admin::MailTemplatesController, type: :controller do it { expect(subject.status).to eq 200 } it { expect(subject.body).to include("E-mails personnalisables") } - it { expect(subject.body).to include(initiated_mail.name ) } + it { expect(subject.body).to include(Mails::InitiatedMail::DISPLAYED_NAME) } end describe 'PATCH update' do