From a24885c2eedf8cc23b07453f7e21f46ac59e64b8 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 2 Aug 2022 12:57:41 +0200 Subject: [PATCH] feat(champ): can condition on other option --- .../conditions_component.rb | 2 +- app/models/champ.rb | 2 +- app/models/champs/drop_down_list_champ.rb | 4 ---- app/models/logic.rb | 4 ++-- app/models/logic/champ_value.rb | 9 +++++++-- app/models/procedure_revision.rb | 6 +++--- app/models/type_de_champ.rb | 4 ++++ spec/factories/champ.rb | 6 +++++- spec/models/logic/champ_value_spec.rb | 18 ++++++++++++++++-- 9 files changed, 39 insertions(+), 16 deletions(-) diff --git a/app/components/types_de_champ_editor/conditions_component.rb b/app/components/types_de_champ_editor/conditions_component.rb index d9beae3ad..fa32a37e3 100644 --- a/app/components/types_de_champ_editor/conditions_component.rb +++ b/app/components/types_de_champ_editor/conditions_component.rb @@ -176,7 +176,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent in [:boolean, :boolean] | [:number, :number] | [:empty, :empty] true in [:enum, :string] - left.options.include?(right.value) + left.options.map(&:second).include?(right.value) else false end diff --git a/app/models/champ.rb b/app/models/champ.rb index 9ba1f2165..12dbec7eb 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -37,7 +37,7 @@ class Champ < ApplicationRecord :mandatory?, :description, :drop_down_list_options, - :drop_down_other, + :drop_down_other?, :drop_down_list_options?, :drop_down_list_disabled_options, :drop_down_list_enabled_non_empty_options, diff --git a/app/models/champs/drop_down_list_champ.rb b/app/models/champs/drop_down_list_champ.rb index 9af41a2b6..baad4c3b0 100644 --- a/app/models/champs/drop_down_list_champ.rb +++ b/app/models/champs/drop_down_list_champ.rb @@ -56,10 +56,6 @@ class Champs::DropDownListChamp < Champ drop_down_other? && value.present? && drop_down_list_options.exclude?(value) end - def drop_down_other? - drop_down_other == "1" || drop_down_other == true - end - def value=(value) if value != OTHER write_attribute(:value, value) diff --git a/app/models/logic.rb b/app/models/logic.rb index a69c979f3..9888097bd 100644 --- a/app/models/logic.rb +++ b/app/models/logic.rb @@ -36,7 +36,7 @@ module Logic when :empty Empty.new when :enum - Constant.new(left.options.first) + Constant.new(left.options.first.second) when :number Constant.new(0) end @@ -50,7 +50,7 @@ module Logic in [a, ^a] # syntax for same type true in [:enum, :string] - left.options.include?(right.value) + left.options.map(&:second).include?(right.value) else false end diff --git a/app/models/logic/champ_value.rb b/app/models/logic/champ_value.rb index b406f52da..d6d2068fb 100644 --- a/app/models/logic/champ_value.rb +++ b/app/models/logic/champ_value.rb @@ -34,7 +34,7 @@ class Logic::ChampValue < Logic::Term when MANAGED_TYPE_DE_CHAMP.fetch(:integer_number), MANAGED_TYPE_DE_CHAMP.fetch(:decimal_number) targeted_champ.for_api when MANAGED_TYPE_DE_CHAMP.fetch(:drop_down_list) - targeted_champ.value + targeted_champ.selected end end @@ -78,7 +78,12 @@ class Logic::ChampValue < Logic::Term end def options - type_de_champ.drop_down_list_enabled_non_empty_options + opts = type_de_champ.drop_down_list_enabled_non_empty_options.map { |option| [option, option] } + if type_de_champ.drop_down_other? + opts + [["Autre", Champs::DropDownListChamp::OTHER]] + else + opts + end end private diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 52fd5f49d..086f43412 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -428,15 +428,15 @@ class ProcedureRevision < ApplicationRecord } end end - if from_type_de_champ.drop_down_other != to_type_de_champ.drop_down_other + if from_type_de_champ.drop_down_other? != to_type_de_champ.drop_down_other? changes << { model: :type_de_champ, op: :update, attribute: :drop_down_other, label: from_type_de_champ.libelle, private: from_type_de_champ.private?, - from: from_type_de_champ.drop_down_other, - to: to_type_de_champ.drop_down_other, + from: from_type_de_champ.drop_down_other?, + to: to_type_de_champ.drop_down_other?, stable_id: from_type_de_champ.stable_id } end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index e5e965476..6221e78a3 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -182,6 +182,10 @@ class TypeDeChamp < ApplicationRecord revisions.size == 1 end + def drop_down_other? + drop_down_other == "1" || drop_down_other == true + end + def non_fillable? type_champ.in?([ TypeDeChamp.type_champs.fetch(:header_section), diff --git a/spec/factories/champ.rb b/spec/factories/champ.rb index 2c9d64886..dbea8d84b 100644 --- a/spec/factories/champ.rb +++ b/spec/factories/champ.rb @@ -87,7 +87,11 @@ FactoryBot.define do end factory :champ_drop_down_list, class: 'Champs::DropDownListChamp' do - type_de_champ { association :type_de_champ_drop_down_list, procedure: dossier.procedure } + transient do + other { false } + end + + type_de_champ { association :type_de_champ_drop_down_list, procedure: dossier.procedure, drop_down_other: other } value { 'choix 1' } end diff --git a/spec/models/logic/champ_value_spec.rb b/spec/models/logic/champ_value_spec.rb index 108050402..7b1b2bf13 100644 --- a/spec/models/logic/champ_value_spec.rb +++ b/spec/models/logic/champ_value_spec.rb @@ -49,10 +49,24 @@ describe Logic::ChampValue do end context 'dropdown tdc' do - let(:champ) { create(:champ_drop_down_list, value: 'choix 1') } + let(:champ) { create(:champ_drop_down_list, value: 'val1') } it { expect(champ_value(champ.stable_id).type).to eq(:enum) } - it { is_expected.to eq('choix 1') } + it { is_expected.to eq('val1') } + it { expect(champ_value(champ.stable_id).options).to match_array([["val1", "val1"], ["val2", "val2"], ["val3", "val3"]]) } + + context 'with other enabled' do + let(:champ) { create(:champ_drop_down_list, value: 'val1', other: true) } + + it { is_expected.to eq('val1') } + it { expect(champ_value(champ.stable_id).options).to match_array([["val1", "val1"], ["val2", "val2"], ["val3", "val3"], ["Autre", "__other__"]]) } + end + + context 'with other filled' do + let(:champ) { create(:champ_drop_down_list, value: 'other value', other: true) } + + it { is_expected.to eq(Champs::DropDownListChamp::OTHER) } + end end context 'checkbox tdc' do