remove default empty value in drop_down_list

This commit is contained in:
simon lehericey 2024-09-17 15:57:38 +02:00
parent 51e192939c
commit 061c9cc6f3
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
6 changed files with 11 additions and 10 deletions

View file

@ -496,10 +496,8 @@ class TypeDeChamp < ApplicationRecord
end end
end end
DEFAULT_EMPTY = ['']
def drop_down_list_value=(value) def drop_down_list_value=(value)
split = value.to_s.lines.map(&:strip).reject(&:empty?) self.drop_down_options = 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

View file

@ -89,8 +89,10 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas
end end
def unpack_options def unpack_options
_, *options = drop_down_options chunked = drop_down_options
chunked = options.slice_before(PRIMARY_PATTERN) .reject(&:empty?) # TODO: remove after removing empty options
.slice_before(PRIMARY_PATTERN)
chunked.map do |chunk| chunked.map do |chunk|
primary, *secondary = chunk primary, *secondary = chunk
secondary = add_blank_option_when_not_mandatory(secondary) secondary = add_blank_option_when_not_mandatory(secondary)
@ -99,7 +101,8 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas
end end
def check_presence_of_primary_options def check_presence_of_primary_options
if !PRIMARY_PATTERN.match?(drop_down_options.second) # TODO: replace by `drop_down_options.first` when the empty options are removed
if !PRIMARY_PATTERN.match?(drop_down_options.find(&:present?))
errors.add(libelle.presence || "La liste", "doit commencer par une entrée de menu primaire de la forme <code style='white-space: pre-wrap;'>--texte--</code>") errors.add(libelle.presence || "La liste", "doit commencer par une entrée de menu primaire de la forme <code style='white-space: pre-wrap;'>--texte--</code>")
end end
end end

View file

@ -96,7 +96,7 @@ FactoryBot.define do
end end
factory :type_de_champ_linked_drop_down_list do factory :type_de_champ_linked_drop_down_list do
type_champ { TypeDeChamp.type_champs.fetch(:linked_drop_down_list) } type_champ { TypeDeChamp.type_champs.fetch(:linked_drop_down_list) }
drop_down_list_value { "--primary--\nsecondary\n" } drop_down_list_value { "--primary--\r\nsecondary\n" }
end end
factory :type_de_champ_expression_reguliere do factory :type_de_champ_expression_reguliere do
type_champ { TypeDeChamp.type_champs.fetch(:expression_reguliere) } type_champ { TypeDeChamp.type_champs.fetch(:expression_reguliere) }

View file

@ -78,7 +78,7 @@ describe Champs::LinkedDropDownListChamp do
end end
describe '#mandatory_and_blank' do describe '#mandatory_and_blank' do
let(:value) { "--Primary--\nSecondary" } let(:value) { "--Primary--\r\nSecondary" }
subject { described_class.new } subject { described_class.new }
before { allow(subject).to receive(:type_de_champ).and_return(type_de_champ) } before { allow(subject).to receive(:type_de_champ).and_return(type_de_champ) }

View file

@ -203,7 +203,7 @@ describe TypeDeChamp do
expect(type_de_champ.drop_down_options).to eq([]) expect(type_de_champ.drop_down_options).to eq([])
type_de_champ.drop_down_list_value = " 1 / 2 \r\n 3" type_de_champ.drop_down_list_value = " 1 / 2 \r\n 3"
expect(type_de_champ.drop_down_options).to eq(['', '1 / 2', '3']) expect(type_de_champ.drop_down_options).to eq(['1 / 2', '3'])
end end
end end

View file

@ -162,7 +162,7 @@ describe 'As an administrateur I can edit types de champ', js: true do
fill_in 'Options de la liste', with: 'Un menu', fill_options: { clear: :backspace } fill_in 'Options de la liste', with: 'Un menu', fill_options: { clear: :backspace }
check "Proposer une option « autre » avec un texte libre" check "Proposer une option « autre » avec un texte libre"
wait_until { procedure.active_revision.types_de_champ_public.first.drop_down_options == ['', 'Un menu'] } wait_until { procedure.active_revision.types_de_champ_public.first.drop_down_options == ['Un menu'] }
wait_until { procedure.active_revision.types_de_champ_public.first.drop_down_other == "1" } wait_until { procedure.active_revision.types_de_champ_public.first.drop_down_other == "1" }
expect(page).to have_content('Formulaire enregistré') expect(page).to have_content('Formulaire enregistré')