diff --git a/app/models/types_de_champ/prefill_departement_type_de_champ.rb b/app/models/types_de_champ/prefill_departement_type_de_champ.rb new file mode 100644 index 000000000..9a6fa807a --- /dev/null +++ b/app/models/types_de_champ/prefill_departement_type_de_champ.rb @@ -0,0 +1,15 @@ +class TypesDeChamp::PrefillDepartementTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp + def possible_values + departements.map { |departement| "#{departement[:code]} (#{departement[:name]})" } + end + + def example_value + departements.pick(:code) + end + + private + + def departements + @departements ||= APIGeoService.departements.sort_by { |departement| departement[:code] } + end +end diff --git a/app/models/types_de_champ/prefill_type_de_champ.rb b/app/models/types_de_champ/prefill_type_de_champ.rb index 296c04c48..5e9e55380 100644 --- a/app/models/types_de_champ/prefill_type_de_champ.rb +++ b/app/models/types_de_champ/prefill_type_de_champ.rb @@ -9,6 +9,8 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator TypesDeChamp::PrefillPaysTypeDeChamp.new(type_de_champ) when TypeDeChamp.type_champs.fetch(:regions) TypesDeChamp::PrefillRegionTypeDeChamp.new(type_de_champ) + when TypeDeChamp.type_champs.fetch(:departements) + TypesDeChamp::PrefillDepartementTypeDeChamp.new(type_de_champ) else new(type_de_champ) end diff --git a/spec/models/types_de_champ/prefill_departement_type_de_champ_spec.rb b/spec/models/types_de_champ/prefill_departement_type_de_champ_spec.rb new file mode 100644 index 000000000..bba1b47a7 --- /dev/null +++ b/spec/models/types_de_champ/prefill_departement_type_de_champ_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +RSpec.describe TypesDeChamp::PrefillDepartementTypeDeChamp, type: :model do + let(:type_de_champ) { build(:type_de_champ_departements) } + let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) } + + before do + allow(Rails).to receive(:cache).and_return(memory_store) + Rails.cache.clear + end + + describe '#possible_values', vcr: { cassette_name: 'api_geo_departements' } do + let(:expected_values) { + APIGeoService.departements.sort_by { |departement| departement[:code] }.map { |departement| "#{departement[:code]} (#{departement[:name]})" } + } + subject(:possible_values) { described_class.new(type_de_champ).possible_values } + + it { expect(possible_values).to match(expected_values) } + end + + describe '#example_value', vcr: { cassette_name: 'api_geo_departements' } do + subject(:example_value) { described_class.new(type_de_champ).example_value } + + it { expect(example_value).to eq(APIGeoService.departements.sort_by { |departement| departement[:code] }.first[:code]) } + end +end diff --git a/spec/models/types_de_champ/prefill_type_de_champ_spec.rb b/spec/models/types_de_champ/prefill_type_de_champ_spec.rb index af0e3cfc9..52508be91 100644 --- a/spec/models/types_de_champ/prefill_type_de_champ_spec.rb +++ b/spec/models/types_de_champ/prefill_type_de_champ_spec.rb @@ -22,6 +22,12 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do it { expect(built).to be_kind_of(TypesDeChamp::PrefillRegionTypeDeChamp) } end + context 'when the type de champ is a departements' do + let(:type_de_champ) { build(:type_de_champ_departements) } + + it { expect(built).to be_kind_of(TypesDeChamp::PrefillDepartementTypeDeChamp) } + end + context 'when any other type de champ' do let(:type_de_champ) { build(:type_de_champ_date) }