diff --git a/app/models/columns/champ_column.rb b/app/models/columns/champ_column.rb index 2d3859774..b4c3977b9 100644 --- a/app/models/columns/champ_column.rb +++ b/app/models/columns/champ_column.rb @@ -69,6 +69,8 @@ class Columns::ChampColumn < Column parse_datetime(value) when :date parse_datetime(value)&.to_date + when :enum + parse_enum(value) when :enums parse_enums(value) else @@ -89,13 +91,13 @@ 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 - [value] + [parse_enum(value)].compact_blank when ['drop_down_list', 'text'] # single list can become text - value + parse_enum(value) when ['multiple_drop_down_list', 'drop_down_list'] # multi list can become single - parse_enums(value).first - when ['multiple_drop_down_list', 'text'] # single list can become text - parse_enums(value).join(', ') + parse_enums(value)&.first + when ['multiple_drop_down_list', 'text'] # multi list can become text + parse_enums(value)&.join(', ') when ['date', 'datetime'] # date <=> datetime parse_datetime(value)&.to_datetime when ['datetime', 'date'] # may lose some data, but who cares ? @@ -114,7 +116,19 @@ class Columns::ChampColumn < Column end end - def parse_enums(value) = JSON.parse(value) rescue nil + 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? + options = options_for_select.to_set(&:second) + values.filter { options.member?(_1) } + rescue JSON::ParserError + nil + end def parse_datetime(value) = Time.zone.parse(value) rescue nil end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 36904f1fe..707848b12 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -443,7 +443,7 @@ class TypeDeChamp < ApplicationRecord elsif regions? APIGeoService.region_options elsif any_drop_down_list? - drop_down_options + drop_down_options.map { [_1, _1] } elsif yes_no? Champs::YesNoChamp.options elsif checkbox? diff --git a/spec/models/columns/champ_column_spec.rb b/spec/models/columns/champ_column_spec.rb index a7dfa85bc..4f4ce0b0e 100644 --- a/spec/models/columns/champ_column_spec.rb +++ b/spec/models/columns/champ_column_spec.rb @@ -100,21 +100,39 @@ describe Columns::ChampColumn do end context 'from a drop_down_list' do - let(:champ) { Champs::DropDownListChamp.new(value: 'val1') } + let(:champ) { Champs::DropDownListChamp.new(value:) } + let(:value) { 'val1' } it 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 - let(:champ) { Champs::MultipleDropDownListChamp.new(value: '["val1","val2"]') } + let(:champ) { Champs::MultipleDropDownListChamp.new(value:) } + let(:value) { '["val1","val2"]' } it 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