Mails: move to their own namespace

This commit is contained in:
Simon Lehericey 2017-03-06 22:37:37 +01:00
parent 335caed65e
commit 02bbf0543f
15 changed files with 95 additions and 85 deletions

View file

@ -1,13 +0,0 @@
class ClosedMail < ActiveRecord::Base
include MailTemplateConcern
def name
"Accusé d'acceptation"
end
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

View file

@ -1,13 +0,0 @@
class InitiatedMail < ActiveRecord::Base
include MailTemplateConcern
def name
"Accusé de réception"
end
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

View file

@ -0,0 +1,15 @@
module Mails
class ClosedMail < ActiveRecord::Base
include MailTemplateConcern
def name
"Accusé d'acceptation"
end
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

View file

@ -0,0 +1,15 @@
module Mails
class InitiatedMail < ActiveRecord::Base
include MailTemplateConcern
def name
"Accusé de réception"
end
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

View file

@ -0,0 +1,15 @@
module Mails
class ReceivedMail < ActiveRecord::Base
include MailTemplateConcern
def name
"Accusé de passage en instruction"
end
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

View file

@ -0,0 +1,15 @@
module Mails
class RefusedMail < ApplicationRecord
include MailTemplateConcern
def name
"Accusé de rejet du dossier"
end
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

View file

@ -0,0 +1,15 @@
module Mails
class WithoutContinuationMail < ApplicationRecord
include MailTemplateConcern
def name
"Accusé de classement sans suite"
end
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

View file

@ -28,18 +28,18 @@ class Procedure < ActiveRecord::Base
validates :description, presence: true, allow_blank: false, allow_nil: false
# for all those mails do
# has_one initiated_mail
# has_one :initiated_mail, class_name: 'Mails::InitiatedMail'
#
# add a method to return default mail if none is saved
# define method :initiated_mail_with_override
# self initiated_mail_without_override || InitiatedMail.default
# def initiated_mail_with_override
# self.initiated_mail_without_override || InitiatedMail.default
# end
# alias_method_chain :initiated_mail, :override
%w(InitiatedMail ReceivedMail ClosedMail RefusedMail WithoutContinuationMail).each do |name|
has_one "#{name.underscore}".to_sym
has_one "#{name.underscore}".to_sym, class_name: "Mails::#{name}"
define_method("#{name.underscore}_with_override") do
self.send("#{name.underscore}_without_override") || Object.const_get(name).default
self.send("#{name.underscore}_without_override") || Object.const_get("Mails::#{name}").default
end
alias_method_chain "#{name.underscore.to_sym}".to_s, :override
end

View file

@ -1,13 +0,0 @@
class ReceivedMail < ActiveRecord::Base
include MailTemplateConcern
def name
"Accusé de passage en instruction"
end
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

View file

@ -1,13 +0,0 @@
class RefusedMail < ApplicationRecord
include MailTemplateConcern
def name
"Accusé de rejet du dossier"
end
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

View file

@ -1,13 +0,0 @@
class WithoutContinuationMail < ApplicationRecord
include MailTemplateConcern
def name
"Accusé de classement sans suite"
end
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

View file

@ -1,7 +1,7 @@
require 'spec_helper'
describe Admin::MailTemplatesController, type: :controller do
let(:initiated_mail) { InitiatedMail.default }
let(:initiated_mail) { Mails::InitiatedMail.default }
let(:procedure) { create :procedure }
before do

View file

@ -237,7 +237,7 @@ describe Backoffice::DossiersController, type: :controller do
it 'Notification email is send' do
expect(NotificationMailer).to receive(:send_notification)
.with(dossier, kind_of(ReceivedMail)).and_return(NotificationMailer)
.with(dossier, kind_of(Mails::ReceivedMail)).and_return(NotificationMailer)
expect(NotificationMailer).to receive(:deliver_now!)
subject
@ -263,7 +263,7 @@ describe Backoffice::DossiersController, type: :controller do
it 'Notification email is sent' do
expect(NotificationMailer).to receive(:send_notification)
.with(dossier, kind_of(RefusedMail)).and_return(NotificationMailer)
.with(dossier, kind_of(Mails::RefusedMail)).and_return(NotificationMailer)
expect(NotificationMailer).to receive(:deliver_now!)
subject
@ -289,7 +289,7 @@ describe Backoffice::DossiersController, type: :controller do
it 'Notification email is sent' do
expect(NotificationMailer).to receive(:send_notification)
.with(dossier, kind_of(WithoutContinuationMail)).and_return(NotificationMailer)
.with(dossier, kind_of(Mails::WithoutContinuationMail)).and_return(NotificationMailer)
expect(NotificationMailer).to receive(:deliver_now!)
subject
@ -314,7 +314,7 @@ describe Backoffice::DossiersController, type: :controller do
it 'Notification email is sent' do
expect(NotificationMailer).to receive(:send_notification)
.with(dossier, kind_of(ClosedMail)).and_return(NotificationMailer)
.with(dossier, kind_of(Mails::ClosedMail)).and_return(NotificationMailer)
expect(NotificationMailer).to receive(:deliver_now!)
subject

View file

@ -3,7 +3,7 @@ require 'spec_helper'
describe MailTemplateConcern do
describe '.replace_tags' do
let(:dossier) { create :dossier }
let(:initiated_mail) { InitiatedMail.default }
let(:initiated_mail) { Mails::InitiatedMail.default }
it 'works' do
initiated_mail.object = '[TPS] --numero_dossier-- --libelle_procedure-- --lien_dossier--'

View file

@ -23,23 +23,23 @@ describe Procedure do
end
describe 'mails' do
it { expect(subject.initiated_mail).to be_a(InitiatedMail) }
it { expect(subject.received_mail).to be_a(ReceivedMail) }
it { expect(subject.closed_mail).to be_a(ClosedMail) }
it { expect(subject.refused_mail).to be_a(RefusedMail) }
it { expect(subject.without_continuation_mail).to be_a(WithoutContinuationMail) }
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) }
end
describe 'initiated_mail' do
subject { create(:procedure) }
context 'when initiated_mail is not customize' do
it { expect(subject.initiated_mail.body).to eq(InitiatedMail.default.body) }
it { expect(subject.initiated_mail.body).to eq(Mails::InitiatedMail.default.body) }
end
context 'when initiated_mail is customize' do
before :each do
subject.initiated_mail = InitiatedMail.new(body: 'sisi')
subject.initiated_mail = Mails::InitiatedMail.new(body: 'sisi')
subject.save
subject.reload
end
@ -48,13 +48,13 @@ describe Procedure do
context 'when initiated_mail is customize ... again' do
before :each do
subject.initiated_mail = InitiatedMail.new(body: 'toto')
subject.initiated_mail = Mails::InitiatedMail.new(body: 'toto')
subject.save
subject.reload
end
it { expect(subject.initiated_mail.body).to eq('toto') }
it { expect(InitiatedMail.count).to eq(1) }
it { expect(Mails::InitiatedMail.count).to eq(1) }
end
end