Merge pull request #9888 from mfo/US/validate-epci-by-code

Correctif: Le noms des EPCIs changent, ce qui bloque la validation et ou demande a resaisir le champs
This commit is contained in:
mfo 2024-01-08 09:56:09 +00:00 committed by GitHub
commit a0248bf08b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 5 deletions

View file

@ -1,6 +1,7 @@
class Champs::EpciChamp < Champs::TextChamp
store_accessor :value_json, :code_departement, :code_region
before_validation :on_departement_change
before_validation :on_epci_name_changes
validate :code_departement_in_departement_codes, unless: -> { code_departement.nil? }
validate :external_id_in_departement_epci_codes, unless: -> { code_departement.nil? || external_id.nil? }
@ -99,4 +100,13 @@ class Champs::EpciChamp < Champs::TextChamp
errors.add(:value, :not_in_departement_epci_names)
end
def on_epci_name_changes
return if external_id.nil? || code_departement.nil?
return if value.in?(APIGeoService.epcis(code_departement).pluck(:name))
if external_id.in?(APIGeoService.epcis(code_departement).pluck(:code))
self.value = (external_id)
end
end
end

View file

@ -110,10 +110,10 @@ describe Champs::EpciChamp, type: :model do
it { is_expected.to be_valid }
end
context 'when value is empty' do
let(:value) { '' }
context 'when value is in departement epci names' do
let(:value) { 'CA Haut - Bugey Agglomération' }
it { is_expected.not_to be_valid }
it { is_expected.to be_valid }
end
context 'when value is in departement epci names' do
@ -122,10 +122,22 @@ describe Champs::EpciChamp, type: :model do
it { is_expected.to be_valid }
end
context 'when value is not in departement epci names' do
context 'when epci name had been renamed' do
let(:value) { 'totoro' }
it { is_expected.not_to be_valid }
it 'is valid and updates its own value' do
expect(subject).to be_valid
expect(subject.value).to eq('CA Haut - Bugey Agglomération')
end
end
context 'when value is not in departement epci names nor in departement epci codes' do
let(:value) { 'totoro' }
it 'is invalid' do
allow(APIGeoService).to receive(:epcis).with(champ.code_departement).and_return([])
expect(subject).not_to be_valid
end
end
end
end