diff --git a/app/models/attestation_template.rb b/app/models/attestation_template.rb index c56c030b0..f415e8e8b 100644 --- a/app/models/attestation_template.rb +++ b/app/models/attestation_template.rb @@ -2,7 +2,7 @@ class AttestationTemplate < ApplicationRecord include ActionView::Helpers::NumberHelper include TagsSubstitutionConcern - belongs_to :procedure, inverse_of: :attestation_template_v2 + belongs_to :procedure, inverse_of: :attestation_template has_one_attached :logo has_one_attached :signature diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 7995bd49a..818b3c982 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -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_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 :canonical_procedure, class_name: 'Procedure', optional: true diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 0b8b3f4ed..971683eb6 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -1813,23 +1813,23 @@ describe Procedure do describe "#attestation_template" do 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 create(:attestation_template, procedure: procedure) - create(:attestation_template, :v2, procedure: procedure) + create(:attestation_template, :v2, :draft, procedure: procedure) end - it { expect(procedure.attestation_template.version).to eq(1) } + it { expect(subject.attestation_template.version).to eq(1) } end - context "when there is a v2 created before v1" do + context "when there is only a v1" do before do - create(:attestation_template, :v2, procedure: procedure) - create(:attestation_template, procedure: procedure, activated: true) + create(:attestation_template, procedure: procedure) end - it { expect(procedure.attestation_template.version).to eq(1) } + it { expect(subject.attestation_template.version).to eq(1) } end context "when there is only a v2" do @@ -1837,7 +1837,23 @@ describe Procedure do create(:attestation_template, :v2, procedure: procedure) 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