simplify drop_down_list_value
This commit is contained in:
parent
2f64fe5e4c
commit
b3ad237fa1
2 changed files with 11 additions and 27 deletions
|
@ -482,8 +482,10 @@ class TypeDeChamp < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
DEFAULT_EMPTY = ['']
|
||||||
def drop_down_list_value=(value)
|
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
|
end
|
||||||
|
|
||||||
def header_section_level_value
|
def header_section_level_value
|
||||||
|
@ -769,13 +771,6 @@ class TypeDeChamp < ApplicationRecord
|
||||||
|
|
||||||
private
|
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
|
def populate_stable_id
|
||||||
if !stable_id
|
if !stable_id
|
||||||
update_column(:stable_id, id)
|
update_column(:stable_id, id)
|
||||||
|
|
|
@ -193,28 +193,17 @@ describe TypeDeChamp do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#drop_down_options' do
|
describe '#drop_down_options' do
|
||||||
let(:value) do
|
let(:type_de_champ) { create(:type_de_champ_drop_down_list) }
|
||||||
<<~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) }
|
|
||||||
|
|
||||||
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
|
type_de_champ.drop_down_list_value = "\n\r"
|
||||||
let(:value) do
|
expect(type_de_champ.drop_down_options).to eq([])
|
||||||
<<~EOS
|
|
||||||
Cohésion sociale
|
|
||||||
Cadre de vie / Urb.
|
|
||||||
Pilotage / Ingénierie
|
|
||||||
EOS
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue