diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index ca94c3c3c..3d1d3b164 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -496,10 +496,8 @@ class TypeDeChamp < ApplicationRecord end end - DEFAULT_EMPTY = [''] def drop_down_list_value=(value) - split = value.to_s.lines.map(&:strip).reject(&:empty?) - self.drop_down_options = split.blank? ? [] : DEFAULT_EMPTY + split + self.drop_down_options = value.to_s.lines.map(&:strip).reject(&:empty?) end def header_section_level_value diff --git a/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb b/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb index 0b3b3677c..a308a7859 100644 --- a/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb +++ b/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb @@ -89,8 +89,10 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas end def unpack_options - _, *options = drop_down_options - chunked = options.slice_before(PRIMARY_PATTERN) + chunked = drop_down_options + .reject(&:empty?) # TODO: remove after removing empty options + .slice_before(PRIMARY_PATTERN) + chunked.map do |chunk| primary, *secondary = chunk secondary = add_blank_option_when_not_mandatory(secondary) @@ -99,7 +101,8 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas end 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 --texte--") end end diff --git a/spec/factories/type_de_champ.rb b/spec/factories/type_de_champ.rb index e6af07b4f..d1c269ace 100644 --- a/spec/factories/type_de_champ.rb +++ b/spec/factories/type_de_champ.rb @@ -96,7 +96,7 @@ FactoryBot.define do end factory :type_de_champ_linked_drop_down_list do 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 factory :type_de_champ_expression_reguliere do type_champ { TypeDeChamp.type_champs.fetch(:expression_reguliere) } diff --git a/spec/models/champs/linked_drop_down_list_champ_spec.rb b/spec/models/champs/linked_drop_down_list_champ_spec.rb index 3798e9f38..462f22612 100644 --- a/spec/models/champs/linked_drop_down_list_champ_spec.rb +++ b/spec/models/champs/linked_drop_down_list_champ_spec.rb @@ -78,7 +78,7 @@ describe Champs::LinkedDropDownListChamp do end describe '#mandatory_and_blank' do - let(:value) { "--Primary--\nSecondary" } + let(:value) { "--Primary--\r\nSecondary" } subject { described_class.new } before { allow(subject).to receive(:type_de_champ).and_return(type_de_champ) } diff --git a/spec/models/type_de_champ_spec.rb b/spec/models/type_de_champ_spec.rb index 49b792cb2..61e66c0b6 100644 --- a/spec/models/type_de_champ_spec.rb +++ b/spec/models/type_de_champ_spec.rb @@ -203,7 +203,7 @@ describe TypeDeChamp do expect(type_de_champ.drop_down_options).to eq([]) 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 diff --git a/spec/system/administrateurs/types_de_champ_spec.rb b/spec/system/administrateurs/types_de_champ_spec.rb index 5586242a5..c7a847857 100644 --- a/spec/system/administrateurs/types_de_champ_spec.rb +++ b/spec/system/administrateurs/types_de_champ_spec.rb @@ -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 } 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" } expect(page).to have_content('Formulaire enregistré')