From 940f8c0474dc3cb5b934e257b4b4b70ff5c93dab Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 5 Jan 2023 16:39:36 +0100 Subject: [PATCH] fix(graphql): fix departements with alphanumeric codes --- app/models/champs/departement_champ.rb | 6 ++++- spec/models/champs/departement_champ_spec.rb | 23 +++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/models/champs/departement_champ.rb b/app/models/champs/departement_champ.rb index ac9e177be..61358021c 100644 --- a/app/models/champs/departement_champ.rb +++ b/app/models/champs/departement_champ.rb @@ -37,6 +37,10 @@ class Champs::DepartementChamp < Champs::TextChamp formatted_value end + def for_api_v2 + formatted_value.tr('–', '-') + end + def selected code end @@ -46,7 +50,7 @@ class Champs::DepartementChamp < Champs::TextChamp end def name - maybe_code_and_name = value&.match(/(\d+) - (.+)/) + maybe_code_and_name = value&.match(/^(\w{2,3}) - (.+)/) if maybe_code_and_name maybe_code_and_name[2] else diff --git a/spec/models/champs/departement_champ_spec.rb b/spec/models/champs/departement_champ_spec.rb index 1c88c2f60..35353f865 100644 --- a/spec/models/champs/departement_champ_spec.rb +++ b/spec/models/champs/departement_champ_spec.rb @@ -17,6 +17,7 @@ describe Champs::DepartementChamp, type: :model do expect(champ.value).to eq('Ain') expect(champ.selected).to eq('01') expect(champ.to_s).to eq('01 – Ain') + expect(champ.for_api_v2).to eq('01 - Ain') end it 'with code having 3 chars' do @@ -29,6 +30,16 @@ describe Champs::DepartementChamp, type: :model do expect(champ.to_s).to eq('971 – Guadeloupe') end + it 'with alphanumeric code' do + champ.value = '2B' + expect(champ.external_id).to eq('2B') + expect(champ.code).to eq('2B') + expect(champ.name).to eq('Haute-Corse') + expect(champ.value).to eq('Haute-Corse') + expect(champ.selected).to eq('2B') + expect(champ.to_s).to eq('2B – Haute-Corse') + end + it 'with nil' do champ.write_attribute(:value, 'Ain') champ.write_attribute(:external_id, '01') @@ -61,7 +72,7 @@ describe Champs::DepartementChamp, type: :model do expect(champ.to_s).to eq('') end - it 'with initial name' do + it 'with initial code and name' do champ.write_attribute(:value, '01 - Ain') expect(champ.external_id).to be_nil expect(champ.code).to eq('01') @@ -70,5 +81,15 @@ describe Champs::DepartementChamp, type: :model do expect(champ.selected).to eq('01') expect(champ.to_s).to eq('01 – Ain') end + + it 'with initial code and alphanumeric name' do + champ.write_attribute(:value, '2B - Haute-Corse') + expect(champ.external_id).to be_nil + expect(champ.code).to eq('2B') + expect(champ.name).to eq('Haute-Corse') + expect(champ.value).to eq('2B - Haute-Corse') + expect(champ.selected).to eq('2B') + expect(champ.to_s).to eq('2B – Haute-Corse') + end end end