Merge pull request #8889 from tchak/chore-commune-remove-fallback
chore(dossier): remove fallback from commune champ
This commit is contained in:
commit
8d89e2aec6
4 changed files with 21 additions and 42 deletions
|
@ -1,8 +1,8 @@
|
||||||
%label.notice{ for: code_postal_input_id }= t('.postal_code')
|
%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"
|
= @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?
|
- 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
|
- elsif commune_options.size <= 3
|
||||||
%fieldset.radios
|
%fieldset.radios
|
||||||
- commune_options.each.with_index do |(option, value), index|
|
- commune_options.each.with_index do |(option, value), index|
|
||||||
|
|
|
@ -16,8 +16,8 @@ class Migrations::NormalizeCommunesJob < ApplicationJob
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if !champ.code_postal? && champ.code_postal_with_fallback?
|
if !champ.code_postal? && code_postal_with_fallback(champ).present?
|
||||||
value_json[:code_postal] = champ.code_postal_with_fallback
|
value_json[:code_postal] = code_postal_with_fallback(champ)
|
||||||
end
|
end
|
||||||
|
|
||||||
if value_json.present?
|
if value_json.present?
|
||||||
|
@ -25,4 +25,17 @@ class Migrations::NormalizeCommunesJob < ApplicationJob
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -64,7 +64,7 @@ class Champs::CommuneChamp < Champs::TextChamp
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
if code?
|
if code?
|
||||||
"#{APIGeoService.commune_name(code_departement, code)} (#{code_postal_with_fallback})"
|
"#{APIGeoService.commune_name(code_departement, code)} (#{code_postal})"
|
||||||
else
|
else
|
||||||
value.present? ? value.to_s : ''
|
value.present? ? value.to_s : ''
|
||||||
end
|
end
|
||||||
|
@ -79,15 +79,15 @@ class Champs::CommuneChamp < Champs::TextChamp
|
||||||
end
|
end
|
||||||
|
|
||||||
def communes
|
def communes
|
||||||
if code_postal_with_fallback?
|
if code_postal?
|
||||||
APIGeoService.communes_by_postal_code(code_postal_with_fallback)
|
APIGeoService.communes_by_postal_code(code_postal)
|
||||||
else
|
else
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def value=(code)
|
def value=(code)
|
||||||
if code.blank? || !code_postal_with_fallback?
|
if code.blank? || !code_postal?
|
||||||
self.code_departement = nil
|
self.code_departement = nil
|
||||||
self.external_id = nil
|
self.external_id = nil
|
||||||
super(nil)
|
super(nil)
|
||||||
|
@ -105,23 +105,6 @@ class Champs::CommuneChamp < Champs::TextChamp
|
||||||
end
|
end
|
||||||
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
|
private
|
||||||
|
|
||||||
def on_code_postal_change
|
def on_code_postal_change
|
||||||
|
|
|
@ -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.for_export).to eq(['Châteldon (63290)', '63102', '63 – Puy-de-Dôme'])
|
||||||
expect(champ.communes.size).to eq(8)
|
expect(champ.communes.size).to eq(8)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue