diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 4c1a7cb71..9b39f6086 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -242,7 +242,7 @@ class TypeDeChamp < ApplicationRecord end def only_present_on_draft? - revisions.size == 1 + revisions.one? && revisions.first.draft? end def drop_down_other? diff --git a/spec/models/procedure_revision_spec.rb b/spec/models/procedure_revision_spec.rb index b394fa2d0..9f3277267 100644 --- a/spec/models/procedure_revision_spec.rb +++ b/spec/models/procedure_revision_spec.rb @@ -872,4 +872,21 @@ describe ProcedureRevision do it { expect(draft.dependent_conditions(first_champ)).to eq([second_champ]) } it { expect(draft.dependent_conditions(second_champ)).to eq([]) } end + + describe 'only_present_on_draft?' do + let(:procedure) { create(:procedure, types_de_champ_public: [{ libelle: 'Un champ texte' }]) } + let(:type_de_champ) { procedure.draft_revision.types_de_champ_public.first } + + it { + expect(type_de_champ.only_present_on_draft?).to be_truthy + procedure.publish! + expect(type_de_champ.only_present_on_draft?).to be_falsey + procedure.draft_revision.remove_type_de_champ(type_de_champ.stable_id) + expect(type_de_champ.only_present_on_draft?).to be_falsey + expect(type_de_champ.revisions.count).to eq(1) + procedure.publish_revision! + expect(type_de_champ.only_present_on_draft?).to be_falsey + expect(type_de_champ.revisions.count).to eq(1) + } + end end