From def32a3615cbbb91ce61b565862f6d18bcdf37cf Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 11 Apr 2023 10:49:01 +0200 Subject: [PATCH] chore(dossier): remove fallback from commune champ --- .../communes_component.html.haml | 4 +-- app/jobs/migrations/normalize_communes_job.rb | 17 +++++++++++-- app/models/champs/commune_champ.rb | 25 +++---------------- spec/models/champs/commune_champ_spec.rb | 17 ------------- 4 files changed, 21 insertions(+), 42 deletions(-) diff --git a/app/components/editable_champ/communes_component/communes_component.html.haml b/app/components/editable_champ/communes_component/communes_component.html.haml index 38920bfc6..849a5e1c8 100644 --- a/app/components/editable_champ/communes_component/communes_component.html.haml +++ b/app/components/editable_champ/communes_component/communes_component.html.haml @@ -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| diff --git a/app/jobs/migrations/normalize_communes_job.rb b/app/jobs/migrations/normalize_communes_job.rb index 736ca0383..5fa0cfbef 100644 --- a/app/jobs/migrations/normalize_communes_job.rb +++ b/app/jobs/migrations/normalize_communes_job.rb @@ -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 diff --git a/app/models/champs/commune_champ.rb b/app/models/champs/commune_champ.rb index b42d05a7b..20827d93c 100644 --- a/app/models/champs/commune_champ.rb +++ b/app/models/champs/commune_champ.rb @@ -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 diff --git a/spec/models/champs/commune_champ_spec.rb b/spec/models/champs/commune_champ_spec.rb index ee143ade9..0c819ad30 100644 --- a/spec/models/champs/commune_champ_spec.rb +++ b/spec/models/champs/commune_champ_spec.rb @@ -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