MailTemplateController: use new initiated_mail

This commit is contained in:
Simon Lehericey 2017-03-05 11:05:28 +01:00
parent c22c84f1f9
commit 89763ec8d4
4 changed files with 38 additions and 34 deletions

View file

@ -2,23 +2,34 @@ class Admin::MailTemplatesController < AdminController
before_action :retrieve_procedure
def index
@mail_templates = @procedure.mail_templates
@mails = mails
end
def edit
@mail_template = @procedure.mail_templates.find(params[:id])
@mail_template = find_the_right_mail params[:id]
end
def update
mail_template = @procedure.mail_templates.find(params[:id])
mail_template = find_the_right_mail params[:id]
mail_template.update_attributes(update_params)
redirect_to admin_procedure_mail_templates_path
end
private
def mails
[@procedure.initiated_mail]
end
def find_the_right_mail type
mails.find { |m| m.class.slug == type }
end
def update_params
params.require(:mail_template).permit(:body, :object)
{
procedure_id: params[:procedure_id],
object: params[:mail_template][:object],
body: params[:mail_template][:body],
}
end
end

View file

@ -2,7 +2,9 @@
%h3
= @mail_template.name
= simple_form_for @mail_template.becomes(MailTemplate), url: admin_procedure_mail_template_path(@procedure, @mail_template) do |f|
= simple_form_for @mail_template.becomes(MailTemplate),
url: admin_procedure_mail_template_path(@procedure, @mail_template.class.slug),
method: :put do |f|
.row
.col-md-6
= f.input :object, label: "Objet de l'email"

View file

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

View file

@ -1,8 +1,8 @@
require 'spec_helper'
describe Admin::MailTemplatesController, type: :controller do
let(:mail_template) { create :mail_template, :dossier_received }
let(:procedure) { create :procedure, mail_templates: [mail_template]}
let(:initiated_mail) { InitiatedMail.default }
let(:procedure) { create :procedure }
before do
sign_in procedure.administrateur
@ -11,41 +11,32 @@ describe Admin::MailTemplatesController, type: :controller do
describe 'GET index' do
render_views
subject { get :index, params: {procedure_id: procedure.id} }
subject { get :index, params: { procedure_id: procedure.id } }
it { expect(subject.status).to eq 200 }
it { expect(subject.body).to include("E-mails personnalisables") }
it { expect(subject.body).to include(*procedure.mail_templates.map{ |mt| mt.decorate.name }) }
it { expect(subject.body).to include(initiated_mail.name ) }
end
describe 'PATCH update' do
let(:object) { 'plop modif' }
let(:body) { 'plip modif' }
context 'when is mail_template id' do
subject { patch :update,
params: {procedure_id: mail_template.procedure.id,
id: mail_template.id,
mail_template: {
object: object,
body: body
}} }
before :each do
patch :update,
params: { procedure_id: procedure.id,
id: initiated_mail.class.slug,
mail_template: { object: object, body: body }
}
end
it { expect(subject).to redirect_to admin_procedure_mail_templates_path(mail_template.procedure) }
it { expect(response).to redirect_to admin_procedure_mail_templates_path(procedure) }
it {
expect {
subject
mail_template.reload
}.to change(mail_template, :object).from("[TPS] Accusé de réception pour votre dossier n°--numero_dossier--").to(object)
}
context 'the mail template' do
subject { procedure.reload ; procedure.initiated_mail }
it {
expect {
subject
mail_template.reload
}.to change(mail_template, :body).from("Votre administration vous confirme la bonne réception de votre dossier n°--numero_dossier--").to(body)
}
it { expect(subject.object).to eq(object) }
it { expect(subject.body).to eq(body) }
end
end
end