diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index eda2176bb..4b96b3b0c 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -482,8 +482,10 @@ class TypeDeChamp < ApplicationRecord end end + DEFAULT_EMPTY = [''] def drop_down_list_value=(value) - self.drop_down_options = parse_drop_down_list_value(value) + split = value.to_s.lines.map(&:strip).reject(&:empty?) + self.drop_down_options = split.blank? ? [] : DEFAULT_EMPTY + split end def header_section_level_value @@ -769,13 +771,6 @@ class TypeDeChamp < ApplicationRecord private - DEFAULT_EMPTY = [''] - def parse_drop_down_list_value(value) - value = value ? value.split("\r\n").map(&:strip).join("\r\n") : '' - result = value.split(/[\r\n]|[\r]|[\n]|[\n\r]/).reject(&:empty?) - result.blank? ? [] : DEFAULT_EMPTY + result - end - def populate_stable_id if !stable_id update_column(:stable_id, id) diff --git a/spec/models/type_de_champ_spec.rb b/spec/models/type_de_champ_spec.rb index 28c5365e8..49b792cb2 100644 --- a/spec/models/type_de_champ_spec.rb +++ b/spec/models/type_de_champ_spec.rb @@ -193,28 +193,17 @@ describe TypeDeChamp do end describe '#drop_down_options' do - let(:value) do - <<~EOS - Cohésion sociale - Dév.Eco / Emploi - Cadre de vie / Urb. - Pilotage / Ingénierie - EOS - end - let(:type_de_champ) { create(:type_de_champ_drop_down_list, drop_down_list_value: value) } + let(:type_de_champ) { create(:type_de_champ_drop_down_list) } - it { expect(type_de_champ.drop_down_options).to eq ['', 'Cohésion sociale', 'Dév.Eco / Emploi', 'Cadre de vie / Urb.', 'Pilotage / Ingénierie'] } + it "splits input" do + type_de_champ.drop_down_list_value = nil + expect(type_de_champ.drop_down_options).to eq([]) - context 'when one value is empty' do - let(:value) do - <<~EOS - Cohésion sociale - Cadre de vie / Urb. - Pilotage / Ingénierie - EOS - end + type_de_champ.drop_down_list_value = "\n\r" + expect(type_de_champ.drop_down_options).to eq([]) - it { expect(type_de_champ.drop_down_options).to eq ['', 'Cohésion sociale', 'Cadre de vie / Urb.', 'Pilotage / Ingénierie'] } + type_de_champ.drop_down_list_value = " 1 / 2 \r\n 3" + expect(type_de_champ.drop_down_options).to eq(['', '1 / 2', '3']) end end