diff --git a/app/models/attestation_template.rb b/app/models/attestation_template.rb index 8b1e9d792..db966a89f 100644 --- a/app/models/attestation_template.rb +++ b/app/models/attestation_template.rb @@ -68,7 +68,7 @@ class AttestationTemplate < ApplicationRecord body: dossier ? replace_tags(body, dossier) : params.fetch(:body, body), footer: params.fetch(:footer, footer), logo: params.fetch(:logo, logo.attached? ? logo : nil), - signature: params.fetch(:signature, signature.attached? ? signature : nil) + signature: signature_to_render(params) } end @@ -90,6 +90,16 @@ class AttestationTemplate < ApplicationRecord private + def signature_to_render(params) + dossier = params.fetch(:dossier, false) + groupe_instructeur = dossier ? dossier.groupe_instructeur : params[:groupe_instructeur] + if groupe_instructeur && groupe_instructeur.signature.attached? + groupe_instructeur.signature + else + signature + end + end + def used_tags used_tags_for(title) + used_tags_for(body) end diff --git a/app/models/groupe_instructeur.rb b/app/models/groupe_instructeur.rb index 1d0af0c7b..d14434586 100644 --- a/app/models/groupe_instructeur.rb +++ b/app/models/groupe_instructeur.rb @@ -15,6 +15,11 @@ class GroupeInstructeur < ApplicationRecord has_one :defaut_procedure, -> { with_discarded }, class_name: 'Procedure', foreign_key: :defaut_groupe_instructeur_id, dependent: :nullify, inverse_of: :defaut_groupe_instructeur has_one :contact_information + has_one_attached :signature + + SIGNATURE_MAX_SIZE = 1.megabytes + validates :signature, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: SIGNATURE_MAX_SIZE } + validates :label, presence: true, allow_nil: false validates :label, uniqueness: { scope: :procedure } validates :closed, acceptance: { accept: [false] }, if: -> { (self == procedure.defaut_groupe_instructeur) } diff --git a/spec/controllers/administrateurs/attestation_templates_controller_spec.rb b/spec/controllers/administrateurs/attestation_templates_controller_spec.rb index 0e670de35..a1930f1d5 100644 --- a/spec/controllers/administrateurs/attestation_templates_controller_spec.rb +++ b/spec/controllers/administrateurs/attestation_templates_controller_spec.rb @@ -58,7 +58,7 @@ describe Administrateurs::AttestationTemplatesController, type: :controller do expect(assigns(:attestation)).to include(attestation_params) expect(assigns(:attestation)[:created_at]).to eq(Time.zone.now) expect(assigns(:attestation)[:logo]).to eq(nil) - expect(assigns(:attestation)[:signature]).to eq(nil) + expect(assigns(:attestation)[:signature]).not_to be_attached end it_behaves_like 'rendering a PDF successfully' end diff --git a/spec/models/attestation_template_spec.rb b/spec/models/attestation_template_spec.rb index 3f307c2f8..e47f596a7 100644 --- a/spec/models/attestation_template_spec.rb +++ b/spec/models/attestation_template_spec.rb @@ -1,45 +1,4 @@ describe AttestationTemplate, type: :model do - # describe 'validate' do - # let(:logo_size) { AttestationTemplate::FILE_MAX_SIZE_IN_MB.megabyte } - # let(:signature_size) { AttestationTemplate::FILE_MAX_SIZE_IN_MB.megabyte } - # let(:fake_logo) { double(AttestationTemplateLogoUploader, file: double(size: logo_size)) } - # let(:fake_signature) { double(AttestationTemplateSignatureUploader, file: double(size: signature_size)) } - # let(:attestation_template) { AttestationTemplate.new } - - # before do - # allow(attestation_template).to receive(:logo).and_return(fake_logo) - # allow(attestation_template).to receive(:signature).and_return(fake_signature) - # attestation_template.validate - # end - - # subject { attestation_template.errors.details } - - # context 'when no files are present' do - # let(:fake_logo) { nil } - # let(:fake_signature) { nil } - - # it { is_expected.to match({}) } - # end - - # context 'when the logo and the signature have the right size' do - # it { is_expected.to match({}) } - # end - - # context 'when the logo and the signature are too heavy' do - # let(:logo_size) { AttestationTemplate::FILE_MAX_SIZE_IN_MB.megabyte + 1 } - # let(:signature_size) { AttestationTemplate::FILE_MAX_SIZE_IN_MB.megabyte + 1 } - - # it do - # expected = { - # signature: [{ error: ' : vous ne pouvez pas charger une image de plus de 0,5 Mo' }], - # logo: [{ error: ' : vous ne pouvez pas charger une image de plus de 0,5 Mo' }] - # } - - # is_expected.to match(expected) - # end - # end - # end - describe 'validates footer length' do let(:attestation_template) { build(:attestation_template, footer: footer) } @@ -175,4 +134,44 @@ describe AttestationTemplate, type: :model do end end end + + describe '#render_attributes_for' do + context 'signature' do + let(:dossier) { create(:dossier, procedure: attestation.procedure, groupe_instructeur: groupe_instructeur) } + + subject { attestation.render_attributes_for(dossier: dossier)[:signature] } + + context 'procedure with signature' do + let(:attestation) { create(:attestation_template, signature: Rack::Test::UploadedFile.new('spec/fixtures/files/logo_test_procedure.png', 'image/png')) } + + context "groupe instructeur without signature" do + let(:groupe_instructeur) { create(:groupe_instructeur, signature: nil) } + + it { expect(subject.blob.filename).to eq("logo_test_procedure.png") } + end + + context 'groupe instructeur with signature' do + let(:groupe_instructeur) { create(:groupe_instructeur, signature: Rack::Test::UploadedFile.new('spec/fixtures/files/black.png', 'image/png')) } + + it { expect(subject.blob.filename).to eq("black.png") } + end + end + + context 'procedure without signature' do + let(:attestation) { create(:attestation_template, signature: nil) } + + context "groupe instructeur without signature" do + let(:groupe_instructeur) { create(:groupe_instructeur, signature: nil) } + + it { expect(subject.attached?).to be_falsey } + end + + context 'groupe instructeur with signature' do + let(:groupe_instructeur) { create(:groupe_instructeur, signature: Rack::Test::UploadedFile.new('spec/fixtures/files/black.png', 'image/png')) } + + it { expect(subject.blob.filename).to eq("black.png") } + end + end + end + end end