[Fix #196] AttestionTemplate: add preview

This commit is contained in:
Simon Lehericey 2017-05-31 17:39:15 +02:00
parent fdb4d15bcf
commit c0facbf679
6 changed files with 90 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

Binary file not shown.

View file

@ -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