chore(attestation): procedure#attestation_template always returns the published attestation

This commit is contained in:
Colin Darie 2024-05-29 11:31:49 +02:00
parent 45fcfd2d16
commit c5c24c01d8
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
3 changed files with 26 additions and 10 deletions

View file

@ -2,7 +2,7 @@ class AttestationTemplate < ApplicationRecord
include ActionView::Helpers::NumberHelper include ActionView::Helpers::NumberHelper
include TagsSubstitutionConcern include TagsSubstitutionConcern
belongs_to :procedure, inverse_of: :attestation_template_v2 belongs_to :procedure, inverse_of: :attestation_template
has_one_attached :logo has_one_attached :logo
has_one_attached :signature has_one_attached :signature

View file

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

View file

@ -1813,23 +1813,23 @@ describe Procedure do
describe "#attestation_template" do describe "#attestation_template" do
let(:procedure) { create(:procedure) } let(:procedure) { create(:procedure) }
subject { procedure.reload }
context "when there is a v2 created after v1" do context "when there is a v2 draft and a v1" do
before do before do
create(:attestation_template, procedure: procedure) create(:attestation_template, procedure: procedure)
create(:attestation_template, :v2, procedure: procedure) create(:attestation_template, :v2, :draft, procedure: procedure)
end end
it { expect(procedure.attestation_template.version).to eq(1) } it { expect(subject.attestation_template.version).to eq(1) }
end end
context "when there is a v2 created before v1" do context "when there is only a v1" do
before do before do
create(:attestation_template, :v2, procedure: procedure) create(:attestation_template, procedure: procedure)
create(:attestation_template, procedure: procedure, activated: true)
end end
it { expect(procedure.attestation_template.version).to eq(1) } it { expect(subject.attestation_template.version).to eq(1) }
end end
context "when there is only a v2" do context "when there is only a v2" do
@ -1837,7 +1837,23 @@ describe Procedure do
create(:attestation_template, :v2, procedure: procedure) create(:attestation_template, :v2, procedure: procedure)
end end
it { expect(procedure.attestation_template.version).to eq(2) } it { expect(subject.attestation_template.version).to eq(2) }
end
context "when there is a v2 draft" do
before do
create(:attestation_template, :v2, :draft, procedure: procedure)
end
it { expect(subject.attestation_template).to be_nil }
context "and a published" do
before do
create(:attestation_template, :v2, :published, procedure: procedure)
end
it { expect(subject.attestation_template).to be_published }
end
end end
end end