From 90524cb541aca2bdcb2492404b9e6f33d288ac7b Mon Sep 17 00:00:00 2001 From: Eric Leroy-Terquem Date: Tue, 17 Oct 2023 11:14:58 +0200 Subject: [PATCH] feat(conditional): can condition on region champ value --- app/models/logic/champ_value.rb | 11 ++++++++--- .../conditions_component_spec.rb | 12 ++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/models/logic/champ_value.rb b/app/models/logic/champ_value.rb index bad3fe362..77068f8bd 100644 --- a/app/models/logic/champ_value.rb +++ b/app/models/logic/champ_value.rb @@ -6,7 +6,8 @@ class Logic::ChampValue < Logic::Term :decimal_number, :drop_down_list, :multiple_drop_down_list, - :departements + :departements, + :regions ) CHAMP_VALUE_TYPE = { @@ -61,7 +62,7 @@ class Logic::ChampValue < Logic::Term CHAMP_VALUE_TYPE.fetch(:boolean) when MANAGED_TYPE_DE_CHAMP.fetch(:integer_number), MANAGED_TYPE_DE_CHAMP.fetch(:decimal_number) CHAMP_VALUE_TYPE.fetch(:number) - when MANAGED_TYPE_DE_CHAMP.fetch(:drop_down_list), MANAGED_TYPE_DE_CHAMP.fetch(:departements) + when MANAGED_TYPE_DE_CHAMP.fetch(:drop_down_list),MANAGED_TYPE_DE_CHAMP.fetch(:departements), MANAGED_TYPE_DE_CHAMP.fetch(:regions) CHAMP_VALUE_TYPE.fetch(:enum) when MANAGED_TYPE_DE_CHAMP.fetch(:multiple_drop_down_list) CHAMP_VALUE_TYPE.fetch(:enums) @@ -95,8 +96,12 @@ class Logic::ChampValue < Logic::Term def options(type_de_champs) tdc = type_de_champ(type_de_champs) - if tdc.departement? + + case tdc.type_champ + when MANAGED_TYPE_DE_CHAMP.fetch(:departements) APIGeoService.departements.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] } + when MANAGED_TYPE_DE_CHAMP.fetch(:regions) + APIGeoService.regions.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] } else opts = tdc.drop_down_list_enabled_non_empty_options.map { |option| [option, option] } if tdc.drop_down_other? diff --git a/spec/components/types_de_champ_editor/conditions_component_spec.rb b/spec/components/types_de_champ_editor/conditions_component_spec.rb index 7a840b596..6d86b7c0b 100644 --- a/spec/components/types_de_champ_editor/conditions_component_spec.rb +++ b/spec/components/types_de_champ_editor/conditions_component_spec.rb @@ -92,6 +92,18 @@ describe TypesDeChampEditor::ConditionsComponent, type: :component do expect(page).to have_select('type_de_champ[condition_form][rows][][value]', options: (['Sélectionner'] + departement_options)) end end + + context 'regions' do + 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]}" } } + + 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][][value]', options: (['Sélectionner'] + region_options)) + end + end end context 'and 2 conditions' do