fix(attestation): always return v1 template when exists, otherwise it's a v2

This commit is contained in:
Colin Darie 2024-02-19 16:08:47 +01:00
parent 8ba1c0e481
commit 3c22f4ea48
No known key found for this signature in database
GPG key ID: 8C76CADD40253590
2 changed files with 31 additions and 1 deletions

View file

@ -51,7 +51,7 @@ class Procedure < ApplicationRecord
has_one :attestation_template_v1, -> { AttestationTemplate.v1 }, dependent: :destroy, class_name: "AttestationTemplate", inverse_of: :procedure
has_one :attestation_template_v2, -> { AttestationTemplate.v2 }, dependent: :destroy, class_name: "AttestationTemplate", inverse_of: :procedure
has_one :attestation_template, -> { AttestationTemplate.v1.or(AttestationTemplate.v2) }, dependent: :destroy, inverse_of: :procedure
has_one :attestation_template, -> { order(Arel.sql("CASE WHEN version = '1' THEN 0 ELSE 1 END")) }, dependent: :destroy, inverse_of: :procedure
belongs_to :parent_procedure, class_name: 'Procedure', optional: true
belongs_to :canonical_procedure, class_name: 'Procedure', optional: true

View file

@ -1761,6 +1761,36 @@ describe Procedure do
end
end
describe "#attestation_template" do
let(:procedure) { create(:procedure) }
context "when there is a v2 created after v1" do
before do
create(:attestation_template, procedure: procedure)
create(:attestation_template, :v2, procedure: procedure)
end
it { expect(procedure.attestation_template.version).to eq(1) }
end
context "when there is a v2 created before v1" do
before do
create(:attestation_template, :v2, procedure: procedure)
create(:attestation_template, procedure: procedure, activated: true)
end
it { expect(procedure.attestation_template.version).to eq(1) }
end
context "when there is only a v2" do
before do
create(:attestation_template, :v2, procedure: procedure)
end
it { expect(procedure.attestation_template.version).to eq(2) }
end
end
private
def create_dossier_with_pj_of_size(size, procedure)