From c578c88f48e1ff997e6826c947dbbdd913607fe6 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 12 Dec 2024 09:46:59 +0100 Subject: [PATCH 1/3] always show user choice even if no more in enum in columns --- app/models/columns/champ_column.rb | 11 +++-------- spec/models/columns/champ_column_spec.rb | 8 -------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/app/models/columns/champ_column.rb b/app/models/columns/champ_column.rb index b4c3977b9..4e5813562 100644 --- a/app/models/columns/champ_column.rb +++ b/app/models/columns/champ_column.rb @@ -70,7 +70,7 @@ class Columns::ChampColumn < Column when :date parse_datetime(value)&.to_date when :enum - parse_enum(value) + value when :enums parse_enums(value) else @@ -91,9 +91,9 @@ class Columns::ChampColumn < Column when ['integer_number', 'text'], ['decimal_number', 'text'] # number to text value when ['drop_down_list', 'multiple_drop_down_list'] # single list can become multi - [parse_enum(value)].compact_blank + [value] when ['drop_down_list', 'text'] # single list can become text - parse_enum(value) + value when ['multiple_drop_down_list', 'drop_down_list'] # multi list can become single parse_enums(value)&.first when ['multiple_drop_down_list', 'text'] # multi list can become text @@ -116,11 +116,6 @@ class Columns::ChampColumn < Column end end - def parse_enum(value) - return value if options_for_select.blank? - value if options_for_select.to_set(&:second).member?(value) - end - def parse_enums(value) values = JSON.parse(value) return values if options_for_select.blank? diff --git a/spec/models/columns/champ_column_spec.rb b/spec/models/columns/champ_column_spec.rb index af5cec1fa..e16302de8 100644 --- a/spec/models/columns/champ_column_spec.rb +++ b/spec/models/columns/champ_column_spec.rb @@ -121,14 +121,6 @@ describe Columns::ChampColumn do expect(column('multiple_drop_down_list').value(champ)).to eq(['val1']) expect(column('text').value(champ)).to eq('val1') end - - context 'value not in options' do - let(:value) { 'toto' } - - it do - expect(column('simple_drop_down_list').value(champ)).to eq(nil) - end - end end context 'from a multiple_drop_down_list' do From 5f63ade97a423ab51007091996f1552d4a80e526 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 12 Dec 2024 09:50:38 +0100 Subject: [PATCH 2/3] same for enums in columns --- app/models/columns/champ_column.rb | 13 +++---------- spec/models/columns/champ_column_spec.rb | 8 -------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/app/models/columns/champ_column.rb b/app/models/columns/champ_column.rb index 4e5813562..c333af916 100644 --- a/app/models/columns/champ_column.rb +++ b/app/models/columns/champ_column.rb @@ -95,9 +95,9 @@ class Columns::ChampColumn < Column when ['drop_down_list', 'text'] # single list can become text value when ['multiple_drop_down_list', 'drop_down_list'] # multi list can become single - parse_enums(value)&.first + parse_enums(value).first when ['multiple_drop_down_list', 'text'] # multi list can become text - parse_enums(value)&.join(', ') + parse_enums(value).join(', ') when ['date', 'datetime'] # date <=> datetime parse_datetime(value)&.to_datetime when ['datetime', 'date'] # may lose some data, but who cares ? @@ -116,14 +116,7 @@ class Columns::ChampColumn < Column end end - def parse_enums(value) - values = JSON.parse(value) - return values if options_for_select.blank? - options = options_for_select.to_set(&:second) - values.filter { options.member?(_1) } - rescue JSON::ParserError - nil - end + def parse_enums(value) = JSON.parse(value) rescue nil def parse_datetime(value) = Time.zone.parse(value) rescue nil end diff --git a/spec/models/columns/champ_column_spec.rb b/spec/models/columns/champ_column_spec.rb index e16302de8..5254dd96a 100644 --- a/spec/models/columns/champ_column_spec.rb +++ b/spec/models/columns/champ_column_spec.rb @@ -131,14 +131,6 @@ describe Columns::ChampColumn do expect(column('simple_drop_down_list').value(champ)).to eq('val1') expect(column('text').value(champ)).to eq('val1, val2') end - - context 'value not in options' do - let(:value) { '["toto","val2"]' } - - it do - expect(column('multiple_drop_down_list').value(champ)).to eq(['val2']) - end - end end end end From a362e955420329378888081b3adcb9f69a566d82 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 12 Dec 2024 13:20:30 +0100 Subject: [PATCH 3/3] same for direct access by champ.value --- .../types_de_champ/drop_down_list_type_de_champ.rb | 13 ------------- .../multiple_drop_down_list_type_de_champ.rb | 2 +- spec/models/concerns/dossier_rebase_concern_spec.rb | 4 ++-- 3 files changed, 3 insertions(+), 16 deletions(-) diff --git a/app/models/types_de_champ/drop_down_list_type_de_champ.rb b/app/models/types_de_champ/drop_down_list_type_de_champ.rb index eee0c03b8..4b9058397 100644 --- a/app/models/types_de_champ/drop_down_list_type_de_champ.rb +++ b/app/models/types_de_champ/drop_down_list_type_de_champ.rb @@ -1,17 +1,4 @@ # frozen_string_literal: true class TypesDeChamp::DropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBase - def champ_blank?(champ) - super || !champ_value_in_options?(champ) - end - - private - - def champ_value_in_options?(champ) - champ_with_other_value?(champ) || drop_down_options.include?(champ.value) - end - - def champ_with_other_value?(champ) - drop_down_other? && champ.value_json&.fetch('other', false) - end end diff --git a/app/models/types_de_champ/multiple_drop_down_list_type_de_champ.rb b/app/models/types_de_champ/multiple_drop_down_list_type_de_champ.rb index bbca8ee52..1be2be9d5 100644 --- a/app/models/types_de_champ/multiple_drop_down_list_type_de_champ.rb +++ b/app/models/types_de_champ/multiple_drop_down_list_type_de_champ.rb @@ -24,7 +24,7 @@ class TypesDeChamp::MultipleDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampB [champ.value] else JSON.parse(champ.value) - end.filter { drop_down_options.include?(_1) } + end rescue JSON::ParserError [] end diff --git a/spec/models/concerns/dossier_rebase_concern_spec.rb b/spec/models/concerns/dossier_rebase_concern_spec.rb index 851080b30..1f54fa997 100644 --- a/spec/models/concerns/dossier_rebase_concern_spec.rb +++ b/spec/models/concerns/dossier_rebase_concern_spec.rb @@ -449,7 +449,7 @@ describe DossierRebaseConcern do tdc_to_update.update(drop_down_options: ["option", "updated"]) end - it { expect { subject }.to change { dossier.project_champs_public.first.to_s }.from('v1').to('') } + it { expect { subject }.not_to change { dossier.project_champs_public.first.to_s } } end context 'when a dropdown unused option is removed' do @@ -495,7 +495,7 @@ describe DossierRebaseConcern do tdc_to_update.update(drop_down_options: ["option", "updated"]) end - it { expect { subject }.to change { dossier.project_champs_public.first.to_s }.from('v1, option').to('option') } + it { expect { subject }.not_to change { dossier.project_champs_public.first.to_s } } end context 'when a dropdown unused option is removed' do