fix(graphql): fix departements with alphanumeric codes

This commit is contained in:
Paul Chavard 2023-01-05 16:39:36 +01:00
parent 5a4a5d89fd
commit 940f8c0474
2 changed files with 27 additions and 2 deletions

View file

@ -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

View file

@ -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