add signature to groupe_instructeur

This commit is contained in:
Christophe Robillard 2023-09-19 15:41:51 +02:00 committed by krichtof
parent 7529294845
commit 5482863eea
4 changed files with 57 additions and 43 deletions

View file

@ -68,7 +68,7 @@ class AttestationTemplate < ApplicationRecord
body: dossier ? replace_tags(body, dossier) : params.fetch(:body, body), body: dossier ? replace_tags(body, dossier) : params.fetch(:body, body),
footer: params.fetch(:footer, footer), footer: params.fetch(:footer, footer),
logo: params.fetch(:logo, logo.attached? ? logo : nil), logo: params.fetch(:logo, logo.attached? ? logo : nil),
signature: params.fetch(:signature, signature.attached? ? signature : nil) signature: signature_to_render(params)
} }
end end
@ -90,6 +90,16 @@ class AttestationTemplate < ApplicationRecord
private 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 def used_tags
used_tags_for(title) + used_tags_for(body) used_tags_for(title) + used_tags_for(body)
end end

View file

@ -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 :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 :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, presence: true, allow_nil: false
validates :label, uniqueness: { scope: :procedure } validates :label, uniqueness: { scope: :procedure }
validates :closed, acceptance: { accept: [false] }, if: -> { (self == procedure.defaut_groupe_instructeur) } validates :closed, acceptance: { accept: [false] }, if: -> { (self == procedure.defaut_groupe_instructeur) }

View file

@ -58,7 +58,7 @@ describe Administrateurs::AttestationTemplatesController, type: :controller do
expect(assigns(:attestation)).to include(attestation_params) expect(assigns(:attestation)).to include(attestation_params)
expect(assigns(:attestation)[:created_at]).to eq(Time.zone.now) expect(assigns(:attestation)[:created_at]).to eq(Time.zone.now)
expect(assigns(:attestation)[:logo]).to eq(nil) expect(assigns(:attestation)[:logo]).to eq(nil)
expect(assigns(:attestation)[:signature]).to eq(nil) expect(assigns(:attestation)[:signature]).not_to be_attached
end end
it_behaves_like 'rendering a PDF successfully' it_behaves_like 'rendering a PDF successfully'
end end

View file

@ -1,45 +1,4 @@
describe AttestationTemplate, type: :model do 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 describe 'validates footer length' do
let(:attestation_template) { build(:attestation_template, footer: footer) } let(:attestation_template) { build(:attestation_template, footer: footer) }
@ -175,4 +134,44 @@ describe AttestationTemplate, type: :model do
end end
end 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 end