Skip to content

Commit

Permalink
factorize departements and regions options
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSim committed Nov 7, 2024
1 parent f76abca commit 0066bb9
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/components/editable_champ/departements_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def dsfr_input_classname
end

def options
APIGeoService.departements.map { ["#{_1[:code]}#{_1[:name]}", _1[:code]] }
APIGeoService.departement_options
end

def select_options
Expand Down
2 changes: 1 addition & 1 deletion app/components/editable_champ/regions_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def dsfr_input_classname
private

def options
APIGeoService.regions.map { [_1[:name], _1[:code]] }
APIGeoService.region_options
end

def select_options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ def create_simple_routing

case tdc.type_champ
when TypeDeChamp.type_champs.fetch(:departements)
tdc_options = APIGeoService.departements.map { ["#{_1[:code]}#{_1[:name]}", _1[:code]] }
tdc_options = APIGeoService.departement_options
rule_operator = :ds_eq
create_groups_from_territorial_tdc(tdc_options, stable_id, rule_operator)
when TypeDeChamp.type_champs.fetch(:communes), TypeDeChamp.type_champs.fetch(:epci), TypeDeChamp.type_champs.fetch(:address)
tdc_options = APIGeoService.departements.map { ["#{_1[:code]}#{_1[:name]}", _1[:code]] }
tdc_options = APIGeoService.departement_options
rule_operator = :ds_in_departement
create_groups_from_territorial_tdc(tdc_options, stable_id, rule_operator)
when TypeDeChamp.type_champs.fetch(:regions)
rule_operator = :ds_eq
tdc_options = APIGeoService.regions.map { ["#{_1[:code]}#{_1[:name]}", _1[:code]] }
tdc_options = APIGeoService.region_options
create_groups_from_territorial_tdc(tdc_options, stable_id, rule_operator)
when TypeDeChamp.type_champs.fetch(:pays)
rule_operator = :ds_eq
Expand Down
8 changes: 2 additions & 6 deletions app/models/concerns/addressable_column_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ module AddressableColumnConcern

included do
def columns(procedure:, displayable: true, prefix: nil)
departement_options = APIGeoService.departements
.map { ["#{_1[:code]}#{_1[:name]}", _1[:code]] }
region_options = APIGeoService.regions.map { [_1[:name], _1[:name]] }

addressable_columns = [
["code postal (5 chiffres)", '$.postal_code', :text, []],
["commune", '$.city_name', :text, []],
["département", '$.departement_code', :enum, departement_options],
["region", '$.region_name', :enum, region_options]
["département", '$.departement_code', :enum, APIGeoService.departement_options],
["region", '$.region_name', :enum, APIGeoService.region_options]
].map do |(label, jsonpath, type, options_for_select)|
Columns::JSONPathColumn.new(
procedure_id: procedure.id,
Expand Down
4 changes: 2 additions & 2 deletions app/models/logic/champ_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ def options(type_de_champs, operator_name = nil)
tdc = type_de_champ(type_de_champs)

if operator_name.in?([Logic::InRegionOperator.name, Logic::NotInRegionOperator.name]) || tdc.type_champ == MANAGED_TYPE_DE_CHAMP.fetch(:regions)
APIGeoService.regions.map { ["#{_1[:code]}#{_1[:name]}", _1[:code]] }
APIGeoService.region_options
elsif operator_name.in?([Logic::InDepartementOperator.name, Logic::NotInDepartementOperator.name]) || tdc.type_champ.in?([MANAGED_TYPE_DE_CHAMP.fetch(:communes), MANAGED_TYPE_DE_CHAMP.fetch(:epci), MANAGED_TYPE_DE_CHAMP.fetch(:departements), MANAGED_TYPE_DE_CHAMP.fetch(:address)])
APIGeoService.departements.map { ["#{_1[:code]}#{_1[:name]}", _1[:code]] }
APIGeoService.departement_options
elsif tdc.type_champ == MANAGED_TYPE_DE_CHAMP.fetch(:pays)
APIGeoService.countries.map { ["#{_1[:name]}#{_1[:code]}", _1[:code]] }
else
Expand Down
4 changes: 2 additions & 2 deletions app/models/type_de_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -547,9 +547,9 @@ def self.column_type(type_champ)

def options_for_select
if departement?
APIGeoService.departements.map { ["#{_1[:code]}#{_1[:name]}", _1[:code]] }.sort
APIGeoService.departement_options
elsif region?
APIGeoService.regions.map { [_1[:name], _1[:code]] }
APIGeoService.region_options
elsif drop_down_list?
drop_down_options
elsif yes_no?
Expand Down
1 change: 1 addition & 0 deletions app/models/types_de_champ/prefill_region_type_de_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class TypesDeChamp::PrefillRegionTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp
def all_possible_values
# WHAT TO DO
regions.map { |region| "#{region[:code]} (#{region[:name]})" }
end

Expand Down
8 changes: 7 additions & 1 deletion app/services/api_geo_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def regions
get_from_api_geo(:regions).sort_by { I18n.transliterate(_1[:name]) }
end

def region_options = regions.map { [_1[:name], _1[:code]] }

def region_name(code)
regions.find { _1[:code] == code }&.dig(:name)
end
Expand All @@ -42,7 +44,11 @@ def region_code_by_departement(code)
end

def departements
[{ code: '99', name: 'Etranger' }] + get_from_api_geo(:departements).sort_by { _1[:code] }
([{ code: '99', name: 'Etranger' }] + get_from_api_geo(:departements)).sort_by { _1[:code] }
end

def departement_options
departements.map { ["#{_1[:code]}#{_1[:name]}", _1[:code]] }
end

def departement_name(code)
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/all.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
.fr-ml-1w.hidden{ 'data-expand-target': 'content' }
%div
= f.select :service_departement,
APIGeoService.departements.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] },
APIGeoService.departement_options,
{ selected: @filter.service_departement, include_blank: ''},
id: "service_dep_select",
class: 'fr-select'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
let(:regions) { create(:type_de_champ_regions) }
let(:upper_tdcs) { [regions] }
let(:condition) { empty_operator(champ_value(regions.stable_id), constant(true)) }
let(:region_options) { APIGeoService.regions.map { "#{_1[:code]}#{_1[:name]}" } }
let(:region_options) { APIGeoService.regions.map { _1[:name] } }

it do
expect(page).to have_select('type_de_champ[condition_form][rows][][operator_name]', with_options: ['Est'])
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/tasks/re_routing_dossiers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

tdc = procedure.active_revision.simple_routable_types_de_champ.first

tdc_options = APIGeoService.departements.map { ["#{_1[:code]}#{_1[:name]}", _1[:code]] }
tdc_options = APIGeoService.departement_options

rule_operator = :ds_eq

Expand Down
3 changes: 1 addition & 2 deletions spec/services/api_geo_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@
describe 'departements' do
it 'return sorted results' do
expect(APIGeoService.departements.size).to eq(110)
expect(APIGeoService.departements.first).to eq(code: '99', name: 'Etranger')
expect(APIGeoService.departements.second).to eq(code: '01', name: 'Ain', region_code: "84")
expect(APIGeoService.departements.last).to eq(code: '989', name: 'Île de Clipperton', region_code: "989")
expect(APIGeoService.departements.last).to eq(code: '99', name: 'Etranger')
end
end

Expand Down

0 comments on commit 0066bb9

Please sign in to comment.