diff --git a/app/controllers/admin/mail_templates_controller.rb b/app/controllers/admin/mail_templates_controller.rb index 39f2b7955..0d3b5e71a 100644 --- a/app/controllers/admin/mail_templates_controller.rb +++ b/app/controllers/admin/mail_templates_controller.rb @@ -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 diff --git a/app/views/admin/mail_templates/edit.html.haml b/app/views/admin/mail_templates/edit.html.haml index 4b2503f8e..010f3a82f 100644 --- a/app/views/admin/mail_templates/edit.html.haml +++ b/app/views/admin/mail_templates/edit.html.haml @@ -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" diff --git a/app/views/admin/mail_templates/index.html.haml b/app/views/admin/mail_templates/index.html.haml index b7dce9af4..26a55b850 100644 --- a/app/views/admin/mail_templates/index.html.haml +++ b/app/views/admin/mail_templates/index.html.haml @@ -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) diff --git a/spec/controllers/admin/mail_templates_controller_spec.rb b/spec/controllers/admin/mail_templates_controller_spec.rb index e80ba6c5b..dc9b2db35 100644 --- a/spec/controllers/admin/mail_templates_controller_spec.rb +++ b/spec/controllers/admin/mail_templates_controller_spec.rb @@ -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