From ca25788f079314c952fe0173dced28e18d89a151 Mon Sep 17 00:00:00 2001 From: Eric Leroy-Terquem Date: Tue, 29 Aug 2023 11:35:39 +0200 Subject: [PATCH] feat(routing): use only department codes in routing rules --- .../one_groupe_management_component.rb | 2 +- .../groupe_instructeurs_controller.rb | 29 ++++++++++++------- app/models/groupe_instructeur.rb | 2 +- app/models/logic/champ_value.rb | 2 +- app/models/type_de_champ.rb | 6 ---- spec/models/routing_engine_spec.rb | 2 +- 6 files changed, 22 insertions(+), 21 deletions(-) diff --git a/app/components/procedure/one_groupe_management_component.rb b/app/components/procedure/one_groupe_management_component.rb index 570617540..f1b8aa0e1 100644 --- a/app/components/procedure/one_groupe_management_component.rb +++ b/app/components/procedure/one_groupe_management_component.rb @@ -70,6 +70,6 @@ class Procedure::OneGroupeManagementComponent < ApplicationComponent end def departements_for_select - APIGeoService.departements.map { ["#{_1[:code]} – #{_1[:name]}", constant("#{_1[:code]} – #{_1[:name]}").to_json] } + APIGeoService.departements.map { ["#{_1[:code]} – #{_1[:name]}", constant(_1[:code]).to_json] } end end diff --git a/app/controllers/administrateurs/groupe_instructeurs_controller.rb b/app/controllers/administrateurs/groupe_instructeurs_controller.rb index d8416180b..d042f74c4 100644 --- a/app/controllers/administrateurs/groupe_instructeurs_controller.rb +++ b/app/controllers/administrateurs/groupe_instructeurs_controller.rb @@ -40,19 +40,26 @@ module Administrateurs tdc = @procedure.active_revision.routable_types_de_champ.find { |tdc| tdc.stable_id == stable_id } - tdc_options = case tdc.type_champ + case tdc.type_champ when TypeDeChamp.type_champs.fetch(:departements) - tdc.codes_and_names - when TypeDeChamp.type_champs.fetch(:drop_down_list) - tdc.drop_down_options.reject(&:empty?) - end + tdc_options = APIGeoService.departements.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] } + tdc_options.each do |code_and_name, code| + routing_rule = ds_eq(champ_value(stable_id), constant(code)) + @procedure + .groupe_instructeurs + .find_or_create_by(label: code_and_name) + .update(instructeurs: [current_administrateur.instructeur], routing_rule:) + end - tdc_options.each do |option_label| - routing_rule = ds_eq(champ_value(stable_id), constant(option_label)) - @procedure - .groupe_instructeurs - .find_or_create_by(label: option_label) - .update(instructeurs: [current_administrateur.instructeur], routing_rule:) + when TypeDeChamp.type_champs.fetch(:drop_down_list) + tdc_options = tdc.drop_down_options.reject(&:empty?) + tdc_options.each do |option_label| + routing_rule = ds_eq(champ_value(stable_id), constant(option_label)) + @procedure + .groupe_instructeurs + .find_or_create_by(label: option_label) + .update(instructeurs: [current_administrateur.instructeur], routing_rule:) + end end if tdc.drop_down_other? diff --git a/app/models/groupe_instructeur.rb b/app/models/groupe_instructeur.rb index a411fad69..69dc0ea2e 100644 --- a/app/models/groupe_instructeur.rb +++ b/app/models/groupe_instructeur.rb @@ -108,7 +108,7 @@ class GroupeInstructeur < ApplicationRecord options = case routing_tdc.type_champ when TypeDeChamp.type_champs.fetch(:departements) - routing_tdc.codes_and_names + APIGeoService.departements.map { _1[:code] } when TypeDeChamp.type_champs.fetch(:drop_down_list) routing_tdc.options_with_drop_down_other end diff --git a/app/models/logic/champ_value.rb b/app/models/logic/champ_value.rb index d0ade372d..4a1c9dcc2 100644 --- a/app/models/logic/champ_value.rb +++ b/app/models/logic/champ_value.rb @@ -45,7 +45,7 @@ class Logic::ChampValue < Logic::Term when "Champs::MultipleDropDownListChamp" targeted_champ.selected_options when "Champs::DepartementChamp" - targeted_champ.to_s + targeted_champ.code end end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index d3a2ad2e4..1d7be7f07 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -477,12 +477,6 @@ class TypeDeChamp < ApplicationRecord end end - def codes_and_names - if departement? - APIGeoService.departements.map { "#{_1[:code]} – #{_1[:name]}" } - end - end - # historicaly we added a blank ("") option by default to avoid wrong selection # see self.parse_drop_down_list_value # then rails decided to add this blank ("") option when the select is required diff --git a/spec/models/routing_engine_spec.rb b/spec/models/routing_engine_spec.rb index f123132df..3095da91f 100644 --- a/spec/models/routing_engine_spec.rb +++ b/spec/models/routing_engine_spec.rb @@ -74,7 +74,7 @@ describe RoutingEngine, type: :model do context 'with a matching rule' do before do - gi_2.update(routing_rule: ds_eq(champ_value(departements_tdc.stable_id), constant('43 – Haute-Loire'))) + gi_2.update(routing_rule: ds_eq(champ_value(departements_tdc.stable_id), constant('43'))) dossier.champs.first.update(value: 'Haute-Loire') end