Merge pull request #11135 from demarches-simplifiees/be_lax_with_drop_down
ETQ instructeur / usager, je vois toujours les choix que j'ai sélectionné dans une liste mm si l'admin a changé les options
This commit is contained in:
commit
f839f5ab2c
5 changed files with 9 additions and 50 deletions
|
@ -70,7 +70,7 @@ class Columns::ChampColumn < Column
|
||||||
when :date
|
when :date
|
||||||
parse_datetime(value)&.to_date
|
parse_datetime(value)&.to_date
|
||||||
when :enum
|
when :enum
|
||||||
parse_enum(value)
|
value
|
||||||
when :enums
|
when :enums
|
||||||
parse_enums(value)
|
parse_enums(value)
|
||||||
else
|
else
|
||||||
|
@ -91,13 +91,13 @@ class Columns::ChampColumn < Column
|
||||||
when ['integer_number', 'text'], ['decimal_number', 'text'] # number to text
|
when ['integer_number', 'text'], ['decimal_number', 'text'] # number to text
|
||||||
value
|
value
|
||||||
when ['drop_down_list', 'multiple_drop_down_list'] # single list can become multi
|
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
|
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
|
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,19 +116,7 @@ class Columns::ChampColumn < Column
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_enum(value)
|
def parse_enums(value) = JSON.parse(value) rescue nil
|
||||||
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
|
def parse_datetime(value) = Time.zone.parse(value) rescue nil
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,17 +1,4 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class TypesDeChamp::DropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
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
|
end
|
||||||
|
|
|
@ -24,7 +24,7 @@ class TypesDeChamp::MultipleDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampB
|
||||||
[champ.value]
|
[champ.value]
|
||||||
else
|
else
|
||||||
JSON.parse(champ.value)
|
JSON.parse(champ.value)
|
||||||
end.filter { drop_down_options.include?(_1) }
|
end
|
||||||
rescue JSON::ParserError
|
rescue JSON::ParserError
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
|
|
|
@ -121,14 +121,6 @@ describe Columns::ChampColumn do
|
||||||
expect(column('multiple_drop_down_list').value(champ)).to eq(['val1'])
|
expect(column('multiple_drop_down_list').value(champ)).to eq(['val1'])
|
||||||
expect(column('text').value(champ)).to eq('val1')
|
expect(column('text').value(champ)).to eq('val1')
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context 'from a multiple_drop_down_list' do
|
context 'from a multiple_drop_down_list' do
|
||||||
|
@ -139,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
|
||||||
|
|
|
@ -449,7 +449,7 @@ describe DossierRebaseConcern do
|
||||||
tdc_to_update.update(drop_down_options: ["option", "updated"])
|
tdc_to_update.update(drop_down_options: ["option", "updated"])
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context 'when a dropdown unused option is removed' do
|
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"])
|
tdc_to_update.update(drop_down_options: ["option", "updated"])
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context 'when a dropdown unused option is removed' do
|
context 'when a dropdown unused option is removed' do
|
||||||
|
|
Loading…
Reference in a new issue