simplify drop_down_list_value

This commit is contained in:
simon lehericey 2024-09-13 22:33:11 +02:00
parent 2f64fe5e4c
commit b3ad237fa1
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
2 changed files with 11 additions and 27 deletions

View file

@ -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)

View file

@ -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