Merge pull request #10019 from colinux/fix-multiple-dropdown-not-json

ETQ uager, fix sélection d'un choix multiple qui commence par `[`
This commit is contained in:
Colin Darie 2024-02-20 17:56:03 +00:00 committed by GitHub
commit 566f5968ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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) values = if value.is_a?(Array)
value value
elsif value.starts_with?('[') 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 else
selected_options + [value] selected_options + [value]
end.uniq.without('') end.uniq.without('')

View file

@ -1,5 +1,5 @@
describe Champs::MultipleDropDownListChamp do 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 } let(:value) { nil }
subject { build(:champ_multiple_drop_down_list, type_de_champ:, value:) } 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\"]") expect(subject.value).to eq("[\"val1\",\"val2\"]")
subject.value = '' subject.value = ''
expect(subject.value).to eq("[\"val1\",\"val2\"]") expect(subject.value).to eq("[\"val1\",\"val2\"]")
subject.value = "[brackets] val4"
expect(subject.value).to eq("[\"val1\",\"val2\",\"[brackets] val4\"]")
subject.value = nil subject.value = nil
expect(subject.value).to be_nil expect(subject.value).to be_nil
subject.value = ["val1"] subject.value = ["val1"]