diff --git a/app/controllers/admin/attestation_templates_controller.rb b/app/controllers/admin/attestation_templates_controller.rb index ca96f235e..bc3bf0f40 100644 --- a/app/controllers/admin/attestation_templates_controller.rb +++ b/app/controllers/admin/attestation_templates_controller.rb @@ -39,6 +39,21 @@ class Admin::AttestationTemplatesController < AdminController redirect_to edit_admin_procedure_attestation_template_path(@procedure) end + def preview + @title = activated_attestation_params[:title] + @body = activated_attestation_params[:body] + @footer = activated_attestation_params[:footer] + @created_at = DateTime.now + + # In a case of a preview, when the user does not change its images, + # the images are not uploaded and thus should be retrieved from previous + # attestation_template + @logo = activated_attestation_params[:logo] || @procedure.attestation_template&.logo + @signature = activated_attestation_params[:signature] || @procedure.attestation_template&.signature + + render 'admin/attestation_templates/show', formats: [:pdf] + end + private def activated_attestation_params diff --git a/app/views/admin/attestation_templates/edit.html.haml b/app/views/admin/attestation_templates/edit.html.haml index 9b3069919..5c50f5023 100644 --- a/app/views/admin/attestation_templates/edit.html.haml +++ b/app/views/admin/attestation_templates/edit.html.haml @@ -61,6 +61,8 @@ ~ f.text_area :footer, class: 'form-control', rows: 2, placeholder: "Direction interministérielle du numérique et du système d'information et de communication de l'Etat (DINSIC)" + %button.btn.btn-primary{ formaction: admin_procedure_attestation_template_preview_path, formtarget: '_blank' } Prévisualiser + .pull-right - if @attestation_template.activated %button.btn.btn-warning{ formaction: admin_procedure_attestation_template_disactivate_path } désactiver l'attestation diff --git a/app/views/admin/attestation_templates/show.pdf.prawn b/app/views/admin/attestation_templates/show.pdf.prawn new file mode 100644 index 000000000..228bdc375 --- /dev/null +++ b/app/views/admin/attestation_templates/show.pdf.prawn @@ -0,0 +1,38 @@ +require 'prawn/measurement_extensions' + +prawn_document(margin: [50, 100, 20, 100]) do |pdf| + pdf.font_families.update( 'open sans' => { normal: './lib/prawn/fonts/OpenSans-Regular.ttf' }) + pdf.font 'open sans' + + grey = '555555' + black = '333333' + max_logo_size = 40.mm + max_signature_size = 40.mm + + pdf.bounding_box([0, pdf.cursor], width: 400, height: 650) do + if @logo.present? + pdf.image StringIO.new(@logo.read), fit: [max_logo_size , max_logo_size], position: :center + end + + pdf.fill_color grey + pdf.pad_top(40) { pdf.text "le #{l(@created_at, format: '%e %B %Y')}", size: 10, align: :right, character_spacing: -0.5 } + + pdf.fill_color black + pdf.pad_top(40) { pdf.text @title, size: 18, character_spacing: -0.2 } + + pdf.fill_color grey + pdf.pad_top(30) { pdf.text @body, size: 10, character_spacing: -0.2 } + + if @signature.present? + pdf.pad_top(40) do + pdf.image StringIO.new(@signature.read), fit: [max_signature_size , max_signature_size], position: :right + end + end + end + + pdf.repeat(:all) do + pdf.move_cursor_to 20 + pdf.fill_color grey + pdf.text @footer, align: :center, size: 8 + end +end diff --git a/config/routes.rb b/config/routes.rb index 0a2075ca8..41cac5e7d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -148,6 +148,9 @@ Rails.application.routes.draw do post 'attestation_template/disactivate' => 'attestation_templates#disactivate' patch 'attestation_template/disactivate' => 'attestation_templates#disactivate' + + post 'attestation_template/preview' => 'attestation_templates#preview' + patch 'attestation_template/preview' => 'attestation_templates#preview' end namespace :accompagnateurs do diff --git a/lib/prawn/fonts/OpenSans-Regular.ttf b/lib/prawn/fonts/OpenSans-Regular.ttf new file mode 100644 index 000000000..db433349b Binary files /dev/null and b/lib/prawn/fonts/OpenSans-Regular.ttf differ diff --git a/spec/controllers/admin/attestation_templates_controller_spec.rb b/spec/controllers/admin/attestation_templates_controller_spec.rb index 7f40c6ecc..3e620e764 100644 --- a/spec/controllers/admin/attestation_templates_controller_spec.rb +++ b/spec/controllers/admin/attestation_templates_controller_spec.rb @@ -7,6 +7,38 @@ describe Admin::AttestationTemplatesController, type: :controller do before do sign_in admin + Timecop.freeze(Time.now) + end + + describe 'POST #preview' do + let(:upload_params) { { title: 't', body: 'b', footer: 'f' } } + + before do + post :preview, + params: { procedure_id: procedure.id, + attestation_template: upload_params } + end + + context 'if an attestation template does not exist on the procedure' do + let(:attestation_template) { nil } + it { expect(subject.status).to eq(200) } + it { expect(assigns).to include(upload_params.stringify_keys) } + end + + context 'if an attestation template exists on the procedure' do + context 'with logos' do + let!(:attestation_template) do + create(:attestation_template, logo: logo, signature: signature) + end + + it { expect(subject.status).to eq(200) } + it { expect(assigns).to include(upload_params.stringify_keys) } + it { expect(assigns[:created_at]).to eq(DateTime.now) } + it { expect(assigns(:logo).read).to eq(logo.read) } + it { expect(assigns(:signature).read).to eq(signature.read) } + after { procedure.attestation_template.destroy } + end + end end describe 'GET #edit' do