chore(dossier): remove fallback from commune champ

This commit is contained in:
Paul Chavard 2023-04-11 10:49:01 +02:00
parent 54c9690289
commit def32a3615
4 changed files with 21 additions and 42 deletions

View file

@ -1,8 +1,8 @@
%label.notice{ for: code_postal_input_id }= t('.postal_code')
= @form.text_field :code_postal, required: @champ.required?, id: code_postal_input_id, class: "width-33-desktop width-100-mobile small-margin"
- if @champ.code_postal_with_fallback?
- if @champ.code_postal?
- if commune_options.empty?
.fr-error-text.mb-4= t('.not_found', postal_code: @champ.code_postal_with_fallback)
.fr-error-text.mb-4= t('.not_found', postal_code: @champ.code_postal)
- elsif commune_options.size <= 3
%fieldset.radios
- commune_options.each.with_index do |(option, value), index|

View file

@ -16,8 +16,8 @@ class Migrations::NormalizeCommunesJob < ApplicationJob
end
end
if !champ.code_postal? && champ.code_postal_with_fallback?
value_json[:code_postal] = champ.code_postal_with_fallback
if !champ.code_postal? && code_postal_with_fallback(champ).present?
value_json[:code_postal] = code_postal_with_fallback(champ)
end
if value_json.present?
@ -25,4 +25,17 @@ class Migrations::NormalizeCommunesJob < ApplicationJob
end
end
end
private
# We try to extract the postal code from the value, which is the name of the commune and the
# postal code in brackets.
def code_postal_with_fallback(champ)
if champ.value.present?
match = champ.value.match(/[^(]\(([^\)]*)\)$/)
match[1] if match.present?
else
nil
end
end
end

View file

@ -64,7 +64,7 @@ class Champs::CommuneChamp < Champs::TextChamp
def to_s
if code?
"#{APIGeoService.commune_name(code_departement, code)} (#{code_postal_with_fallback})"
"#{APIGeoService.commune_name(code_departement, code)} (#{code_postal})"
else
value.present? ? value.to_s : ''
end
@ -79,15 +79,15 @@ class Champs::CommuneChamp < Champs::TextChamp
end
def communes
if code_postal_with_fallback?
APIGeoService.communes_by_postal_code(code_postal_with_fallback)
if code_postal?
APIGeoService.communes_by_postal_code(code_postal)
else
[]
end
end
def value=(code)
if code.blank? || !code_postal_with_fallback?
if code.blank? || !code_postal?
self.code_departement = nil
self.external_id = nil
super(nil)
@ -105,23 +105,6 @@ class Champs::CommuneChamp < Champs::TextChamp
end
end
def code_postal_with_fallback?
code_postal_with_fallback.present?
end
# We try to extract the postal code from the value, which is the name of the commune and the
# postal code in brackets. This is temporary until we do a full data migration.
def code_postal_with_fallback
if code_postal?
code_postal
elsif value.present?
match = value.match(/[^(]\(([^\)]*)\)$/)
match[1] if match.present?
else
nil
end
end
private
def on_code_postal_change

View file

@ -24,22 +24,5 @@ describe Champs::CommuneChamp do
expect(champ.for_export).to eq(['Châteldon (63290)', '63102', '63 Puy-de-Dôme'])
expect(champ.communes.size).to eq(8)
end
context 'when code_postal is nil', vcr: { cassette_name: 'api_geo_communes' } do
let(:champ) { create(:champ_communes, external_id: code_insee, code_departement:) }
it 'with value' do
champ.update_column(:value, 'Châteldon (63290)')
expect(champ.to_s).to eq('Châteldon (63290)')
expect(champ.name).to eq('Châteldon')
expect(champ.external_id).to eq(code_insee)
expect(champ.code).to eq(code_insee)
expect(champ.code_departement).to eq(code_departement)
expect(champ.code_postal).to be_nil
expect(champ.code_postal_with_fallback).to eq(code_postal)
expect(champ.for_export).to eq(['Châteldon (63290)', '63102', '63 Puy-de-Dôme'])
expect(champ.communes.size).to eq(8)
end
end
end
end