diff --git a/app/models/champs/commune_champ.rb b/app/models/champs/commune_champ.rb index 20827d93c..06b6f51fe 100644 --- a/app/models/champs/commune_champ.rb +++ b/app/models/champs/commune_champ.rb @@ -52,6 +52,10 @@ class Champs::CommuneChamp < Champs::TextChamp code_postal.present? end + def code_postal=(value) + super(value&.gsub(/[[:space:]]/, '')) + end + alias postal_code code_postal def name diff --git a/spec/models/champs/commune_champ_spec.rb b/spec/models/champs/commune_champ_spec.rb index a2ade44b2..622e6fb67 100644 --- a/spec/models/champs/commune_champ_spec.rb +++ b/spec/models/champs/commune_champ_spec.rb @@ -2,10 +2,9 @@ describe Champs::CommuneChamp do let(:code_insee) { '63102' } let(:code_postal) { '63290' } let(:code_departement) { '63' } + let(:champ) { create(:champ_communes, code_postal: code_postal) } describe 'value' do - let(:champ) { create(:champ_communes, code_postal: code_postal) } - it 'with code_postal' do champ.update(value: code_insee) expect(champ.to_s).to eq('Châteldon (63290)') @@ -18,4 +17,12 @@ describe Champs::CommuneChamp do expect(champ.communes.size).to eq(8) end end + + describe 'code_postal with spaces' do + let(:code_postal) { ' 63 2 90  ' } + + it 'with code_postal' do + expect(champ.communes.size).to eq(8) + end + end end