fix(dropdown-multiple): a valid option starting by [ may not be a JSON value

This commit is contained in:
Colin Darie 2024-02-20 17:41:53 +01:00
parent da19ba5fe4
commit 560d50f52d
2 changed files with 4 additions and 2 deletions

View file

@ -90,7 +90,7 @@ class Champs::MultipleDropDownListChamp < Champ
values = if value.is_a?(Array)
value
elsif value.starts_with?('[')
JSON.parse(value)
JSON.parse(value) rescue selected_options + [value] # value may start by [ without being a real JSON value
else
selected_options + [value]
end.uniq.without('')

View file

@ -1,5 +1,5 @@
describe Champs::MultipleDropDownListChamp do
let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list, drop_down_list_value: "val1\r\nval2\r\nval3") }
let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list, drop_down_list_value: "val1\r\nval2\r\nval3\r\n[brackets] val4") }
let(:value) { nil }
subject { build(:champ_multiple_drop_down_list, type_de_champ:, value:) }
@ -41,6 +41,8 @@ describe Champs::MultipleDropDownListChamp do
expect(subject.value).to eq("[\"val1\",\"val2\"]")
subject.value = ''
expect(subject.value).to eq("[\"val1\",\"val2\"]")
subject.value = "[brackets] val4"
expect(subject.value).to eq("[\"val1\",\"val2\",\"[brackets] val4\"]")
subject.value = nil
expect(subject.value).to be_nil
subject.value = ["val1"]