factorize departements and regions options

This commit is contained in:
simon lehericey 2024-11-07 10:27:35 +01:00
parent aa8ce15c2a
commit 08fb49d176
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
13 changed files with 27 additions and 26 deletions

View file

@ -10,7 +10,7 @@ class EditableChamp::DepartementsComponent < EditableChamp::EditableChampBaseCom
end end
def options def options
APIGeoService.departements.map { ["#{_1[:code]} #{_1[:name]}", _1[:code]] } APIGeoService.departement_options
end end
def select_options def select_options

View file

@ -10,7 +10,7 @@ class EditableChamp::RegionsComponent < EditableChamp::EditableChampBaseComponen
private private
def options def options
APIGeoService.regions.map { [_1[:name], _1[:code]] } APIGeoService.region_options
end end
def select_options def select_options

View file

@ -49,16 +49,16 @@ module Administrateurs
case tdc.type_champ case tdc.type_champ
when TypeDeChamp.type_champs.fetch(:departements) 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 rule_operator = :ds_eq
create_groups_from_territorial_tdc(tdc_options, stable_id, rule_operator) 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) 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 rule_operator = :ds_in_departement
create_groups_from_territorial_tdc(tdc_options, stable_id, rule_operator) create_groups_from_territorial_tdc(tdc_options, stable_id, rule_operator)
when TypeDeChamp.type_champs.fetch(:regions) when TypeDeChamp.type_champs.fetch(:regions)
rule_operator = :ds_eq 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) create_groups_from_territorial_tdc(tdc_options, stable_id, rule_operator)
when TypeDeChamp.type_champs.fetch(:pays) when TypeDeChamp.type_champs.fetch(:pays)
rule_operator = :ds_eq rule_operator = :ds_eq

View file

@ -5,15 +5,11 @@ module AddressableColumnConcern
included do included do
def columns(procedure:, displayable: true, prefix: nil) 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 = [ addressable_columns = [
["code postal (5 chiffres)", '$.postal_code', :text, []], ["code postal (5 chiffres)", '$.postal_code', :text, []],
["commune", '$.city_name', :text, []], ["commune", '$.city_name', :text, []],
["département", '$.departement_code', :enum, departement_options], ["département", '$.departement_code', :enum, APIGeoService.departement_options],
["region", '$.region_name', :enum, region_options] ["region", '$.region_name', :enum, APIGeoService.region_options]
].map do |(label, jsonpath, type, options_for_select)| ].map do |(label, jsonpath, type, options_for_select)|
Columns::JSONPathColumn.new( Columns::JSONPathColumn.new(
procedure_id: procedure.id, procedure_id: procedure.id,

View file

@ -131,9 +131,9 @@ class Logic::ChampValue < Logic::Term
tdc = type_de_champ(type_de_champs) 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) 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)]) 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) elsif tdc.type_champ == MANAGED_TYPE_DE_CHAMP.fetch(:pays)
APIGeoService.countries.map { ["#{_1[:name]} #{_1[:code]}", _1[:code]] } APIGeoService.countries.map { ["#{_1[:name]} #{_1[:code]}", _1[:code]] }
else else

View file

@ -547,9 +547,9 @@ class TypeDeChamp < ApplicationRecord
def options_for_select def options_for_select
if departement? if departement?
APIGeoService.departements.map { ["#{_1[:code]} #{_1[:name]}", _1[:code]] }.sort APIGeoService.departement_options
elsif region? elsif region?
APIGeoService.regions.map { [_1[:name], _1[:code]] } APIGeoService.region_options
elsif drop_down_list? elsif drop_down_list?
drop_down_options drop_down_options
elsif yes_no? elsif yes_no?

View file

@ -27,6 +27,8 @@ class APIGeoService
get_from_api_geo(:regions).sort_by { I18n.transliterate(_1[:name]) } get_from_api_geo(:regions).sort_by { I18n.transliterate(_1[:name]) }
end end
def region_options = regions.map { [_1[:name], _1[:code]] }
def region_name(code) def region_name(code)
regions.find { _1[:code] == code }&.dig(:name) regions.find { _1[:code] == code }&.dig(:name)
end end
@ -42,7 +44,11 @@ class APIGeoService
end end
def departements 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 end
def departement_name(code) def departement_name(code)

View file

@ -85,7 +85,7 @@
.fr-ml-1w.hidden{ 'data-expand-target': 'content' } .fr-ml-1w.hidden{ 'data-expand-target': 'content' }
%div %div
= f.select :service_departement, = f.select :service_departement,
APIGeoService.departements.map { ["#{_1[:code]} #{_1[:name]}", _1[:code]] }, APIGeoService.departement_options,
{ selected: @filter.service_departement, include_blank: ''}, { selected: @filter.service_departement, include_blank: ''},
id: "service_dep_select", id: "service_dep_select",
class: 'fr-select' class: 'fr-select'

View file

@ -123,7 +123,7 @@ describe Conditions::ChampsConditionsComponent, type: :component do
let(:regions) { create(:type_de_champ_regions) } let(:regions) { create(:type_de_champ_regions) }
let(:upper_tdcs) { [regions] } let(:upper_tdcs) { [regions] }
let(:condition) { empty_operator(champ_value(regions.stable_id), constant(true)) } 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 it do
expect(page).to have_select('type_de_champ[condition_form][rows][][operator_name]', with_options: ['Est']) expect(page).to have_select('type_de_champ[condition_form][rows][][operator_name]', with_options: ['Est'])

View file

@ -959,8 +959,8 @@ describe Administrateurs::GroupeInstructeursController, type: :controller do
it do it do
expect(response).to redirect_to(admin_procedure_groupe_instructeurs_path(procedure3)) expect(response).to redirect_to(admin_procedure_groupe_instructeurs_path(procedure3))
expect(flash.notice).to eq 'Les groupes instructeurs ont été ajoutés' expect(flash.notice).to eq 'Les groupes instructeurs ont été ajoutés'
expect(procedure3.groupe_instructeurs.pluck(:label)).to include("01 Guadeloupe") expect(procedure3.groupe_instructeurs.pluck(:label)).to include("Guadeloupe")
expect(procedure3.reload.defaut_groupe_instructeur.routing_rule).to eq(ds_eq(champ_value(regions_tdc.stable_id), constant('01'))) expect(procedure3.reload.defaut_groupe_instructeur.routing_rule).to eq(ds_eq(champ_value(regions_tdc.stable_id), constant('84')))
expect(procedure3.routing_enabled).to be_truthy expect(procedure3.routing_enabled).to be_truthy
end end
end end

View file

@ -16,7 +16,7 @@ describe 're_routing_dossiers' do
tdc = procedure.active_revision.simple_routable_types_de_champ.first 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 rule_operator = :ds_eq

View file

@ -32,9 +32,8 @@ describe APIGeoService do
describe 'departements' do describe 'departements' do
it 'return sorted results' do it 'return sorted results' do
expect(APIGeoService.departements.size).to eq(110) expect(APIGeoService.departements.size).to eq(110)
expect(APIGeoService.departements.first).to eq(code: '99', name: 'Etranger') expect(APIGeoService.departements.first).to eq(code: '01', name: 'Ain', region_code: "84")
expect(APIGeoService.departements.second).to eq(code: '01', name: 'Ain', region_code: "84") expect(APIGeoService.departements.last).to eq(code: '99', name: 'Etranger')
expect(APIGeoService.departements.last).to eq(code: '989', name: 'Île de Clipperton', region_code: "989")
end end
end end

View file

@ -556,7 +556,7 @@ describe DossierFilterService do
it 'describes column' do it 'describes column' do
expect(column.type).to eq(:enum) expect(column.type).to eq(:enum)
expect(column.options_for_select.first).to eq(["99 Etranger", "99"]) expect(column.options_for_select.first).to eq(["01 Ain", "01"])
end end
end end
@ -573,7 +573,7 @@ describe DossierFilterService do
it 'describes column' do it 'describes column' do
expect(column.type).to eq(:enum) expect(column.type).to eq(:enum)
expect(column.options_for_select.first).to eq(["Auvergne-Rhône-Alpes", "Auvergne-Rhône-Alpes"]) expect(column.options_for_select.first).to eq(["Auvergne-Rhône-Alpes", "84"])
end end
end end
end end