From f7cd7e615d8f7267436aeb1f379e6085049ac28b Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 22 Aug 2024 17:33:32 +0200 Subject: [PATCH] refactor(champs): validate only champs in current revision --- app/models/concerns/champs_validate_concern.rb | 8 ++++++-- spec/models/champs/cnaf_champ_spec.rb | 5 ++++- spec/models/champs/decimal_number_champ_spec.rb | 1 + spec/models/champs/departement_champ_spec.rb | 5 ++++- spec/models/champs/dgfip_champ_spec.rb | 5 ++++- spec/models/champs/drop_down_list_champ_spec.rb | 5 ++++- spec/models/champs/email_champ_spec.rb | 5 ++++- .../{ => champs}/engagement_juridique_champ_spec.rb | 5 ++++- spec/models/champs/epci_champ_spec.rb | 5 ++++- spec/models/champs/iban_champ_spec.rb | 5 ++++- spec/models/champs/integer_number_champ_spec.rb | 5 ++++- spec/models/champs/multiple_drop_down_list_champ_spec.rb | 5 ++++- spec/models/champs/phone_champ_spec.rb | 5 ++++- spec/models/champs/rna_champ_spec.rb | 5 ++++- 14 files changed, 55 insertions(+), 14 deletions(-) rename spec/models/{ => champs}/engagement_juridique_champ_spec.rb (83%) diff --git a/app/models/concerns/champs_validate_concern.rb b/app/models/concerns/champs_validate_concern.rb index bef65d73b..ec4372cf1 100644 --- a/app/models/concerns/champs_validate_concern.rb +++ b/app/models/concerns/champs_validate_concern.rb @@ -16,9 +16,9 @@ module ChampsValidateConcern def validate_champ_value? case validation_context when :champs_public_value - public? && visible? + public? && in_dossier_revision? && visible? when :champs_private_value - private? && visible? + private? && in_dossier_revision? && visible? else false end @@ -27,5 +27,9 @@ module ChampsValidateConcern def validate_champ_value_or_prefill? validate_champ_value? || validation_context == :prefill end + + def in_dossier_revision? + dossier.revision.types_de_champ.any? { _1.stable_id = stable_id } + end end end diff --git a/spec/models/champs/cnaf_champ_spec.rb b/spec/models/champs/cnaf_champ_spec.rb index f8f03ad08..acc7d0ae0 100644 --- a/spec/models/champs/cnaf_champ_spec.rb +++ b/spec/models/champs/cnaf_champ_spec.rb @@ -2,7 +2,10 @@ describe Champs::CnafChamp, type: :model do let(:champ) { described_class.new(dossier: build(:dossier)) } - before { allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_cnaf)) } + before do + allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_cnaf)) + allow(champ).to receive(:in_dossier_revision?).and_return(true) + end describe 'numero_allocataire and code_postal' do before do champ.numero_allocataire = '1234567' diff --git a/spec/models/champs/decimal_number_champ_spec.rb b/spec/models/champs/decimal_number_champ_spec.rb index 78958b7a8..8603f2f9a 100644 --- a/spec/models/champs/decimal_number_champ_spec.rb +++ b/spec/models/champs/decimal_number_champ_spec.rb @@ -6,6 +6,7 @@ describe Champs::DecimalNumberChamp do before do allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_decimal_number)) allow(champ).to receive(:visible?).and_return(true) + allow(champ).to receive(:in_dossier_revision?).and_return(true) champ.run_callbacks(:validation) end subject { champ.validate(:champs_public_value) } diff --git a/spec/models/champs/departement_champ_spec.rb b/spec/models/champs/departement_champ_spec.rb index 9794e01a7..3bfb5f41c 100644 --- a/spec/models/champs/departement_champ_spec.rb +++ b/spec/models/champs/departement_champ_spec.rb @@ -4,7 +4,10 @@ describe Champs::DepartementChamp, type: :model do describe 'validations' do describe 'external link' do let(:champ) { described_class.new(external_id: external_id, dossier: build(:dossier)) } - before { allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_departements)) } + before do + allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_departements)) + allow(champ).to receive(:in_dossier_revision?).and_return(true) + end subject { champ.validate(:champs_public_value) } context 'when nil' do diff --git a/spec/models/champs/dgfip_champ_spec.rb b/spec/models/champs/dgfip_champ_spec.rb index bfe94d895..bafa48adb 100644 --- a/spec/models/champs/dgfip_champ_spec.rb +++ b/spec/models/champs/dgfip_champ_spec.rb @@ -40,7 +40,10 @@ describe Champs::DgfipChamp, type: :model do let(:numero_fiscal) { '1122299999092' } let(:reference_avis) { 'FC22299999092' } let(:champ) { described_class.new(dossier: build(:dossier)) } - before { allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_dgfip)) } + before do + allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_dgfip)) + allow(champ).to receive(:in_dossier_revision?).and_return(true) + end let(:validation_context) { :champs_public_value } subject { champ.valid?(validation_context) } diff --git a/spec/models/champs/drop_down_list_champ_spec.rb b/spec/models/champs/drop_down_list_champ_spec.rb index 842adf8f4..d69487b08 100644 --- a/spec/models/champs/drop_down_list_champ_spec.rb +++ b/spec/models/champs/drop_down_list_champ_spec.rb @@ -4,7 +4,10 @@ describe Champs::DropDownListChamp do describe 'validations' do describe 'inclusion' do let(:champ) { described_class.new(other:, value:, dossier: build(:dossier)) } - before { allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_drop_down_list, drop_down_other: other)) } + before do + allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_drop_down_list, drop_down_other: other)) + allow(champ).to receive(:in_dossier_revision?).and_return(true) + end subject { champ.validate(:champs_public_value) } context 'when the other value is accepted' do diff --git a/spec/models/champs/email_champ_spec.rb b/spec/models/champs/email_champ_spec.rb index e24775296..5dbb7a1b6 100644 --- a/spec/models/champs/email_champ_spec.rb +++ b/spec/models/champs/email_champ_spec.rb @@ -3,7 +3,10 @@ describe Champs::EmailChamp do describe 'validation' do let(:champ) { described_class.new(value:, dossier: build(:dossier)) } - before { allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_email)) } + before do + allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_email)) + allow(champ).to receive(:in_dossier_revision?).and_return(true) + end subject { champ.validate(:champs_public_value) } context 'when nil' do diff --git a/spec/models/engagement_juridique_champ_spec.rb b/spec/models/champs/engagement_juridique_champ_spec.rb similarity index 83% rename from spec/models/engagement_juridique_champ_spec.rb rename to spec/models/champs/engagement_juridique_champ_spec.rb index 55b51df08..795cd7813 100644 --- a/spec/models/engagement_juridique_champ_spec.rb +++ b/spec/models/champs/engagement_juridique_champ_spec.rb @@ -7,7 +7,10 @@ describe Champs::EngagementJuridiqueChamp do .new(dossier: build(:dossier)) .tap { _1.value = value } end - before { allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_engagement_juridique)) } + before { + allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_engagement_juridique)) + allow(champ).to receive(:in_dossier_revision?).and_return(true) + } subject { champ.validate(:champs_public_value) } context 'with [A-Z]' do diff --git a/spec/models/champs/epci_champ_spec.rb b/spec/models/champs/epci_champ_spec.rb index d79cc7df6..58d7b7868 100644 --- a/spec/models/champs/epci_champ_spec.rb +++ b/spec/models/champs/epci_champ_spec.rb @@ -6,7 +6,10 @@ describe Champs::EpciChamp, type: :model do describe 'code_departement' do let(:champ) { Champs::EpciChamp.new(code_departement: code_departement, dossier: build(:dossier)) } - before { allow(champ).to receive(:visible?).and_return(true) } + before do + allow(champ).to receive(:visible?).and_return(true) + allow(champ).to receive(:in_dossier_revision?).and_return(true) + end context 'when nil' do let(:code_departement) { nil } diff --git a/spec/models/champs/iban_champ_spec.rb b/spec/models/champs/iban_champ_spec.rb index 7088f9a34..bf7c63e2f 100644 --- a/spec/models/champs/iban_champ_spec.rb +++ b/spec/models/champs/iban_champ_spec.rb @@ -3,7 +3,10 @@ describe Champs::IbanChamp do describe '#valid?' do let(:champ) { Champs::IbanChamp.new(dossier: build(:dossier)) } - before { allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_iban)) } + before do + allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_iban)) + allow(champ).to receive(:in_dossier_revision?).and_return(true) + end def with_value(value) champ.tap { _1.value = value } end diff --git a/spec/models/champs/integer_number_champ_spec.rb b/spec/models/champs/integer_number_champ_spec.rb index 4367a2465..8a5d2182f 100644 --- a/spec/models/champs/integer_number_champ_spec.rb +++ b/spec/models/champs/integer_number_champ_spec.rb @@ -2,7 +2,10 @@ describe Champs::IntegerNumberChamp do let(:champ) { Champs::IntegerNumberChamp.new(value:, dossier: build(:dossier)) } - before { allow(champ).to receive(:visible?).and_return(true) } + before do + allow(champ).to receive(:visible?).and_return(true) + allow(champ).to receive(:in_dossier_revision?).and_return(true) + end subject { champ.validate(:champs_public_value) } describe '#valid?' do diff --git a/spec/models/champs/multiple_drop_down_list_champ_spec.rb b/spec/models/champs/multiple_drop_down_list_champ_spec.rb index 7702f9494..6e47f7cb8 100644 --- a/spec/models/champs/multiple_drop_down_list_champ_spec.rb +++ b/spec/models/champs/multiple_drop_down_list_champ_spec.rb @@ -4,7 +4,10 @@ describe Champs::MultipleDropDownListChamp do let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list, drop_down_list_value: "val1\r\nval2\r\nval3\r\n[brackets] val4") } let(:value) { nil } let(:champ) { Champs::MultipleDropDownListChamp.new(value:, dossier: build(:dossier)) } - before { allow(champ).to receive(:type_de_champ).and_return(type_de_champ) } + before do + allow(champ).to receive(:type_de_champ).and_return(type_de_champ) + allow(champ).to receive(:in_dossier_revision?).and_return(true) + end describe 'validations' do subject { champ.validate(:champs_public_value) } diff --git a/spec/models/champs/phone_champ_spec.rb b/spec/models/champs/phone_champ_spec.rb index c90951bc7..0653bf39d 100644 --- a/spec/models/champs/phone_champ_spec.rb +++ b/spec/models/champs/phone_champ_spec.rb @@ -2,7 +2,10 @@ describe Champs::PhoneChamp do let(:champ) { Champs::PhoneChamp.new(dossier: build(:dossier)) } - before { allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_phone)) } + before do + allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_phone)) + allow(champ).to receive(:in_dossier_revision?).and_return(true) + end describe '#validate' do it do expect(champ_with_value(nil).validate(:champs_public_value)).to be_truthy diff --git a/spec/models/champs/rna_champ_spec.rb b/spec/models/champs/rna_champ_spec.rb index 248f27c06..c2968f752 100644 --- a/spec/models/champs/rna_champ_spec.rb +++ b/spec/models/champs/rna_champ_spec.rb @@ -2,7 +2,10 @@ describe Champs::RNAChamp do let(:champ) { Champs::RNAChamp.new(value: "W182736273", dossier: build(:dossier)) } - before { allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_rna)) } + before do + allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_rna)) + allow(champ).to receive(:in_dossier_revision?).and_return(true) + end def with_value(value) champ.tap { _1.value = value } end