same for enums in columns

This commit is contained in:
simon lehericey 2024-12-12 09:50:38 +01:00
parent c578c88f48
commit 5f63ade97a
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
2 changed files with 3 additions and 18 deletions

View file

@ -95,9 +95,9 @@ class Columns::ChampColumn < Column
when ['drop_down_list', 'text'] # single list can become text when ['drop_down_list', 'text'] # single list can become text
value value
when ['multiple_drop_down_list', 'drop_down_list'] # multi list can become single 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 when ['multiple_drop_down_list', 'text'] # multi list can become text
parse_enums(value)&.join(', ') parse_enums(value).join(', ')
when ['date', 'datetime'] # date <=> datetime when ['date', 'datetime'] # date <=> datetime
parse_datetime(value)&.to_datetime parse_datetime(value)&.to_datetime
when ['datetime', 'date'] # may lose some data, but who cares ? when ['datetime', 'date'] # may lose some data, but who cares ?
@ -116,14 +116,7 @@ class Columns::ChampColumn < Column
end end
end end
def parse_enums(value) def parse_enums(value) = JSON.parse(value) rescue nil
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 def parse_datetime(value) = Time.zone.parse(value) rescue nil
end end

View file

@ -131,14 +131,6 @@ describe Columns::ChampColumn do
expect(column('simple_drop_down_list').value(champ)).to eq('val1') expect(column('simple_drop_down_list').value(champ)).to eq('val1')
expect(column('text').value(champ)).to eq('val1, val2') expect(column('text').value(champ)).to eq('val1, val2')
end 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 end
end end