use drop_down_options when possible
This commit is contained in:
parent
77b754a08d
commit
8b5f689a67
14 changed files with 81 additions and 92 deletions
|
@ -53,19 +53,23 @@ class RootController < ApplicationController
|
|||
repetition_after_stable_id = repetition_type_de_champ.stable_id
|
||||
end
|
||||
elsif type_de_champ.linked_drop_down_list?
|
||||
type_de_champ.drop_down_list_value =
|
||||
"-- section 1 --
|
||||
option A
|
||||
option B
|
||||
-- section 2 --
|
||||
option C"
|
||||
type_de_champ.drop_down_options =
|
||||
[
|
||||
"-- section 1 --",
|
||||
"option A",
|
||||
"option B",
|
||||
"-- section 2 --",
|
||||
"option C"
|
||||
]
|
||||
type_de_champ.save
|
||||
elsif type_de_champ.drop_down_list?
|
||||
type_de_champ.drop_down_list_value =
|
||||
"option A
|
||||
option B
|
||||
-- avant l'option C --
|
||||
option C"
|
||||
type_de_champ.drop_down_options =
|
||||
[
|
||||
"option A",
|
||||
"option B",
|
||||
"-- avant l'option C --",
|
||||
"option C"
|
||||
]
|
||||
type_de_champ.save
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,7 +15,7 @@ class TypesDeChampEditor::ConditionsComponentPreview < ViewComponent::Preview
|
|||
def with_conditions
|
||||
surface = TypeDeChamp.create(type_champ: :integer_number, libelle: 'surface')
|
||||
appartement = TypeDeChamp.create(type_champ: :yes_no, libelle: 'appartement')
|
||||
type_appartement = TypeDeChamp.create(type_champ: :drop_down_list, libelle: 'type', drop_down_list_value: "T1\r\nT2\r\nT3")
|
||||
type_appartement = TypeDeChamp.create(type_champ: :drop_down_list, libelle: 'type', drop_down_options: ["T1", "T2", "T3"])
|
||||
upper_tdcs = [surface, appartement, type_appartement]
|
||||
|
||||
condition = ds_and([
|
||||
|
|
|
@ -19,7 +19,7 @@ describe Procedure::OneGroupeManagementComponent, type: :component do
|
|||
procedure.draft_revision.add_type_de_champ({
|
||||
type_champ: :drop_down_list,
|
||||
libelle: 'Votre ville',
|
||||
drop_down_list_value: "Paris\nLyon\nMarseille"
|
||||
drop_down_options: ["Paris", "Lyon", "Marseille"]
|
||||
})
|
||||
procedure.publish_revision!
|
||||
procedure.reload
|
||||
|
|
|
@ -308,13 +308,7 @@ def build_types_de_champ(types_de_champ, revision:, scope: :public, parent: nil)
|
|||
end
|
||||
|
||||
if type.in?([:drop_down_list, :multiple_drop_down_list, :linked_drop_down_list])
|
||||
if options.delete(:short).present?
|
||||
type_de_champ_attributes[:drop_down_list_value] = "val1\r\nval2\r\n--separateur--\r\nval3"
|
||||
elsif options.delete(:long).present?
|
||||
type_de_champ_attributes[:drop_down_list_value] = "alpha\r\nbravo\r\n--separateur--\r\ncharly\r\ndelta\r\necho\r\nfox-trot\r\ngolf"
|
||||
else
|
||||
type_de_champ_attributes[:drop_down_list_value] = options.join("\r\n")
|
||||
end
|
||||
type_de_champ_attributes[:drop_down_options] = options
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -79,9 +79,9 @@ FactoryBot.define do
|
|||
factory :type_de_champ_drop_down_list do
|
||||
libelle { 'Choix unique' }
|
||||
type_champ { TypeDeChamp.type_champs.fetch(:drop_down_list) }
|
||||
drop_down_list_value { "val1\r\nval2\r\nval3" }
|
||||
drop_down_options { ["val1", "val2", "val3"] }
|
||||
trait :long do
|
||||
drop_down_list_value { "alpha\r\nbravo\r\ncharly\r\ndelta\r\necho\r\nfox-trot\r\ngolf" }
|
||||
drop_down_options { ["alpha", "bravo", "charly", "delta", "echo", "fox-trot", "golf"] }
|
||||
end
|
||||
trait :with_other do
|
||||
drop_down_other { true }
|
||||
|
@ -89,14 +89,14 @@ FactoryBot.define do
|
|||
end
|
||||
factory :type_de_champ_multiple_drop_down_list do
|
||||
type_champ { TypeDeChamp.type_champs.fetch(:multiple_drop_down_list) }
|
||||
drop_down_list_value { "val1\r\nval2\r\nval3" }
|
||||
drop_down_options { ["val1", "val2", "val3"] }
|
||||
trait :long do
|
||||
drop_down_list_value { "alpha\r\nbravo\r\ncharly\r\ndelta\r\necho\r\nfox-trot\r\ngolf" }
|
||||
drop_down_options { ["alpha", "bravo", "charly", "delta", "echo", "fox-trot", "golf"] }
|
||||
end
|
||||
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--\r\nsecondary\n" }
|
||||
drop_down_options { ["--primary--", "secondary"] }
|
||||
end
|
||||
factory :type_de_champ_expression_reguliere do
|
||||
type_champ { TypeDeChamp.type_champs.fetch(:expression_reguliere) }
|
||||
|
|
|
@ -4,18 +4,18 @@ describe '20240912091625_clean_drop_down_options.rake' do
|
|||
let(:rake_task) { Rake::Task['after_party:clean_drop_down_options'] }
|
||||
|
||||
let!(:dashed_drop_down_list) do
|
||||
drop_down_list_value = ['1', '-- nop --', '2'].join("\r\n")
|
||||
create(:type_de_champ_drop_down_list, drop_down_list_value:)
|
||||
drop_down_options = ['1', '-- nop --', '2']
|
||||
create(:type_de_champ_drop_down_list, drop_down_options:)
|
||||
end
|
||||
|
||||
let!(:witness_drop_down_list) do
|
||||
drop_down_list_value = ['1', 'hi', '2'].join("\r\n")
|
||||
create(:type_de_champ_drop_down_list, drop_down_list_value:)
|
||||
drop_down_options = ['1', 'hi', '2']
|
||||
create(:type_de_champ_drop_down_list, drop_down_options:)
|
||||
end
|
||||
|
||||
let!(:multiple_drop_down_list) do
|
||||
drop_down_list_value = ['1', '-- nop --', '2'].join("\r\n")
|
||||
create(:type_de_champ_multiple_drop_down_list, drop_down_list_value:)
|
||||
drop_down_options = ['1', '-- nop --', '2']
|
||||
create(:type_de_champ_multiple_drop_down_list, drop_down_options:)
|
||||
end
|
||||
|
||||
before do
|
||||
|
|
|
@ -78,13 +78,13 @@ describe Champs::LinkedDropDownListChamp do
|
|||
end
|
||||
|
||||
describe '#mandatory_and_blank' do
|
||||
let(:value) { "--Primary--\r\nSecondary" }
|
||||
let(:options) { ["--Primary--", "Secondary"] }
|
||||
|
||||
subject { described_class.new }
|
||||
before { allow(subject).to receive(:type_de_champ).and_return(type_de_champ) }
|
||||
|
||||
context 'when the champ is not mandatory' do
|
||||
let(:type_de_champ) { build(:type_de_champ_linked_drop_down_list, mandatory: false, drop_down_list_value: value) }
|
||||
let(:type_de_champ) { build(:type_de_champ_linked_drop_down_list, mandatory: false, drop_down_options: options) }
|
||||
|
||||
it 'blank is fine' do
|
||||
is_expected.not_to be_mandatory_blank
|
||||
|
@ -92,7 +92,7 @@ describe Champs::LinkedDropDownListChamp do
|
|||
end
|
||||
|
||||
context 'when the champ is mandatory' do
|
||||
let(:type_de_champ) { build(:type_de_champ_linked_drop_down_list, mandatory: true, drop_down_list_value: value) }
|
||||
let(:type_de_champ) { build(:type_de_champ_linked_drop_down_list, mandatory: true, drop_down_options: options) }
|
||||
|
||||
context 'when there is no value' do
|
||||
it { is_expected.to be_mandatory_blank }
|
||||
|
@ -112,7 +112,7 @@ describe Champs::LinkedDropDownListChamp do
|
|||
end
|
||||
|
||||
context 'when there is nothing to select for the secondary value' do
|
||||
let(:value) { "--A--\nAbbott\nAbelard\n--B--\n--C--\nCynthia" }
|
||||
let(:options) { ["--A--", "Abbott", "Abelard", "--B--", "--C--", "Cynthia"] }
|
||||
before { subject.primary_value = 'B' }
|
||||
|
||||
it { is_expected.not_to be_mandatory_blank }
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
describe Champs::MultipleDropDownListChamp do
|
||||
let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list, drop_down_list_value: "val1\r\nval2\r\nval3\r\n[brackets] val4") }
|
||||
let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list, drop_down_options: ["val1", "val2", "val3", "[brackets] val4"]) }
|
||||
let(:value) { nil }
|
||||
let(:champ) { Champs::MultipleDropDownListChamp.new(value:, dossier: build(:dossier)) }
|
||||
before do
|
||||
|
|
|
@ -229,7 +229,7 @@ RSpec.describe DossierCloneConcern do
|
|||
context 'invalid origin' do
|
||||
let(:procedure) do
|
||||
create(:procedure, types_de_champ_public: [
|
||||
{ type: :drop_down_list, libelle: "Le savez-vous?", stable_id: 992, drop_down_list_value: ["Oui", "Non", "Peut-être"].join("\r\n"), mandatory: true }
|
||||
{ type: :drop_down_list, libelle: "Le savez-vous?", stable_id: 992, drop_down_options: ["Oui", "Non", "Peut-être"], mandatory: true }
|
||||
])
|
||||
end
|
||||
|
||||
|
|
|
@ -422,7 +422,7 @@ describe DossierRebaseConcern do
|
|||
context 'with a procedure with a dropdown tdc' do
|
||||
let!(:procedure) do
|
||||
create(:procedure).tap do |p|
|
||||
p.draft_revision.add_type_de_champ(type_champ: :drop_down_list, libelle: 'l1', drop_down_list_value: "option\nv1\n")
|
||||
p.draft_revision.add_type_de_champ(type_champ: :drop_down_list, libelle: 'l1', drop_down_options: ["option", "v1"])
|
||||
p.publish!
|
||||
end
|
||||
end
|
||||
|
@ -434,7 +434,7 @@ describe DossierRebaseConcern do
|
|||
|
||||
stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'l1')
|
||||
tdc_to_update = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id)
|
||||
tdc_to_update.update(drop_down_list_value: "option\nupdated\nv1")
|
||||
tdc_to_update.update(drop_down_options: ["option", "updated", "v1"])
|
||||
end
|
||||
|
||||
it { expect { subject }.not_to change { dossier.champs_public.first.value } }
|
||||
|
@ -446,7 +446,7 @@ describe DossierRebaseConcern do
|
|||
|
||||
stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'l1')
|
||||
tdc_to_update = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id)
|
||||
tdc_to_update.update(drop_down_list_value: "option\nupdated")
|
||||
tdc_to_update.update(drop_down_options: ["option", "updated"])
|
||||
end
|
||||
|
||||
it { expect { subject }.to change { dossier.champs_public.first.value }.from('v1').to(nil) }
|
||||
|
@ -458,7 +458,7 @@ describe DossierRebaseConcern do
|
|||
|
||||
stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'l1')
|
||||
tdc_to_update = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id)
|
||||
tdc_to_update.update(drop_down_list_value: "v1\nupdated")
|
||||
tdc_to_update.update(drop_down_options: ["v1", "updated"])
|
||||
end
|
||||
|
||||
it { expect { subject }.not_to change { dossier.champs_public.first.value } }
|
||||
|
@ -468,7 +468,7 @@ describe DossierRebaseConcern do
|
|||
context 'with a procedure with a multiple dropdown tdc' do
|
||||
let!(:procedure) do
|
||||
create(:procedure).tap do |p|
|
||||
p.draft_revision.add_type_de_champ(type_champ: :multiple_drop_down_list, libelle: 'l1', drop_down_list_value: "option\nv1\n")
|
||||
p.draft_revision.add_type_de_champ(type_champ: :multiple_drop_down_list, libelle: 'l1', drop_down_options: ["option", "v1"])
|
||||
p.publish!
|
||||
end
|
||||
end
|
||||
|
@ -480,7 +480,7 @@ describe DossierRebaseConcern do
|
|||
|
||||
stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'l1')
|
||||
tdc_to_update = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id)
|
||||
tdc_to_update.update(drop_down_list_value: "option\nupdated\nv1")
|
||||
tdc_to_update.update(drop_down_options: ["option", "updated", "v1"])
|
||||
end
|
||||
|
||||
it { expect { subject }.not_to change { dossier.champs_public.first.value } }
|
||||
|
@ -492,7 +492,7 @@ describe DossierRebaseConcern do
|
|||
|
||||
stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'l1')
|
||||
tdc_to_update = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id)
|
||||
tdc_to_update.update(drop_down_list_value: "option\nupdated")
|
||||
tdc_to_update.update(drop_down_options: ["option", "updated"])
|
||||
end
|
||||
|
||||
it { expect { subject }.to change { dossier.champs_public.first.value }.from('["v1","option"]').to('["option"]') }
|
||||
|
@ -504,7 +504,7 @@ describe DossierRebaseConcern do
|
|||
|
||||
stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'l1')
|
||||
tdc_to_update = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id)
|
||||
tdc_to_update.update(drop_down_list_value: "v1\nupdated")
|
||||
tdc_to_update.update(drop_down_options: ["v1", "updated"])
|
||||
end
|
||||
|
||||
it { expect { subject }.not_to change { dossier.champs_public.first.value } }
|
||||
|
@ -514,7 +514,7 @@ describe DossierRebaseConcern do
|
|||
context 'with a procedure with a linked dropdown tdc' do
|
||||
let!(:procedure) do
|
||||
create(:procedure).tap do |p|
|
||||
p.draft_revision.add_type_de_champ(type_champ: :linked_drop_down_list, libelle: 'l1', drop_down_list_value: "--titre1--\noption\nv1\n--titre2--\noption2\nv2\n")
|
||||
p.draft_revision.add_type_de_champ(type_champ: :linked_drop_down_list, libelle: 'l1', drop_down_options: ["--titre1--", "option", "v1", "--titre2--", "option2", "v2"])
|
||||
p.publish!
|
||||
end
|
||||
end
|
||||
|
@ -526,7 +526,7 @@ describe DossierRebaseConcern do
|
|||
|
||||
stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'l1')
|
||||
tdc_to_update = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id)
|
||||
tdc_to_update.update(drop_down_list_value: "--titre1--\noption\nv1\nupdated\n--titre2--\noption2\nv2\n")
|
||||
tdc_to_update.update(drop_down_options: ["--titre1--", "option", "v1", "updated", "--titre2--", "option2", "v2"])
|
||||
end
|
||||
|
||||
it { expect { subject }.not_to change { dossier.champs_public.first.value } }
|
||||
|
@ -538,7 +538,7 @@ describe DossierRebaseConcern do
|
|||
|
||||
stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'l1')
|
||||
tdc_to_update = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id)
|
||||
tdc_to_update.update(drop_down_list_value: "--titre1--\noption\nupdated\n--titre2--\noption2\nv2\n")
|
||||
tdc_to_update.update(drop_down_options: ["--titre1--", "option", "updated", "--titre2--", "option2", "v2"])
|
||||
end
|
||||
|
||||
it { expect { subject }.to change { dossier.champs_public.first.value }.from('["v1","option2"]').to(nil) }
|
||||
|
@ -550,7 +550,7 @@ describe DossierRebaseConcern do
|
|||
|
||||
stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'l1')
|
||||
tdc_to_update = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id)
|
||||
tdc_to_update.update(drop_down_list_value: "--titre1--\nv1\nupdated\n--titre2--\noption2\nv2\n")
|
||||
tdc_to_update.update(drop_down_options: ["--titre1--", "v1", "updated", "--titre2--", "option2", "v2"])
|
||||
end
|
||||
|
||||
it { expect { subject }.not_to change { dossier.champs_public.first.value } }
|
||||
|
|
|
@ -397,7 +397,7 @@ describe Procedure do
|
|||
expect(procedure.errors.messages_for(:draft_types_de_champ_public)).to include(invalid_drop_down_error_message)
|
||||
|
||||
drop_down = procedure.draft_revision.types_de_champ_public.find(&:drop_down_list?)
|
||||
drop_down.update!(drop_down_list_value: "--title--\r\nsome value")
|
||||
drop_down.update!(drop_down_options: ["--title--", "some value"])
|
||||
procedure.reload.validate(:publication)
|
||||
expect(procedure.errors.messages_for(:draft_types_de_champ_public)).not_to include(invalid_drop_down_error_message)
|
||||
end
|
||||
|
|
|
@ -149,7 +149,7 @@ describe TypeDeChamp do
|
|||
let(:type_de_champ) { create(:type_de_champ_linked_drop_down_list) }
|
||||
|
||||
it 'should validate without label' do
|
||||
type_de_champ.drop_down_list_value = 'toto'
|
||||
type_de_champ.drop_down_options = ['toto']
|
||||
expect(type_de_champ.validate).to be_falsey
|
||||
messages = type_de_champ.errors.full_messages
|
||||
expect(messages.size).to eq(1)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
describe TypesDeChamp::LinkedDropDownListTypeDeChamp do
|
||||
let(:type_de_champ) { build(:type_de_champ_linked_drop_down_list, drop_down_list_value: menu_options) }
|
||||
let(:type_de_champ) { build(:type_de_champ_linked_drop_down_list, drop_down_options: menu_options) }
|
||||
|
||||
subject { type_de_champ.dynamic_type }
|
||||
|
||||
|
@ -9,26 +9,22 @@ describe TypesDeChamp::LinkedDropDownListTypeDeChamp do
|
|||
context 'It must start with one primary option' do
|
||||
context 'valid menu' do
|
||||
let(:menu_options) do
|
||||
<<~END_OPTIONS
|
||||
--Primary 1--
|
||||
secondary 1.1
|
||||
secondary 1.2
|
||||
--Primary 2--
|
||||
secondary 2.1
|
||||
secondary 2.2
|
||||
secondary 2.3
|
||||
END_OPTIONS
|
||||
[
|
||||
"--Primary 1--",
|
||||
"secondary 1.1",
|
||||
"secondary 1.2",
|
||||
"--Primary 2--",
|
||||
"secondary 2.1",
|
||||
"secondary 2.2",
|
||||
"secondary 2.3"
|
||||
]
|
||||
end
|
||||
|
||||
it { is_expected.to be_valid }
|
||||
end
|
||||
|
||||
context 'degenerate but valid menu' do
|
||||
let(:menu_options) do
|
||||
<<~END_OPTIONS
|
||||
--Primary 1--
|
||||
END_OPTIONS
|
||||
end
|
||||
let(:menu_options) { ["--Primary 1--"] }
|
||||
|
||||
it { is_expected.to be_valid }
|
||||
end
|
||||
|
@ -43,26 +39,21 @@ describe TypesDeChamp::LinkedDropDownListTypeDeChamp do
|
|||
end
|
||||
|
||||
context 'no primary option' do
|
||||
let(:menu_options) do
|
||||
<<~END_OPTIONS
|
||||
secondary 1.1
|
||||
secondary 1.2
|
||||
END_OPTIONS
|
||||
end
|
||||
let(:menu_options) { ["secondary 1.1", "secondary 1.2"] }
|
||||
|
||||
it_should_behave_like 'missing primary option'
|
||||
end
|
||||
|
||||
context 'starting with secondary options' do
|
||||
let(:menu_options) do
|
||||
<<~END_OPTIONS
|
||||
secondary 1.1
|
||||
secondary 1.2
|
||||
--Primary 2--
|
||||
secondary 2.1
|
||||
secondary 2.2
|
||||
secondary 2.3
|
||||
END_OPTIONS
|
||||
[
|
||||
"secondary 1.1",
|
||||
"secondary 1.2",
|
||||
"--Primary 2--",
|
||||
"secondary 2.1",
|
||||
"secondary 2.2",
|
||||
"secondary 2.3"
|
||||
]
|
||||
end
|
||||
|
||||
it_should_behave_like 'missing primary option'
|
||||
|
@ -73,22 +64,22 @@ describe TypesDeChamp::LinkedDropDownListTypeDeChamp do
|
|||
|
||||
describe '#unpack_options' do
|
||||
context 'with no options' do
|
||||
let(:menu_options) { '' }
|
||||
let(:menu_options) { [] }
|
||||
it { expect(subject.secondary_options).to eq({}) }
|
||||
it { expect(subject.primary_options).to eq([]) }
|
||||
end
|
||||
|
||||
context 'with two primary options' do
|
||||
let(:menu_options) do
|
||||
<<~END_OPTIONS
|
||||
--Primary 1--
|
||||
secondary 1.1
|
||||
secondary 1.2
|
||||
--Primary 2--
|
||||
secondary 2.1
|
||||
secondary 2.2
|
||||
secondary 2.3
|
||||
END_OPTIONS
|
||||
[
|
||||
"--Primary 1--",
|
||||
"secondary 1.1",
|
||||
"secondary 1.2",
|
||||
"--Primary 2--",
|
||||
"secondary 2.1",
|
||||
"secondary 2.2",
|
||||
"secondary 2.3"
|
||||
]
|
||||
end
|
||||
|
||||
context "mandatory tdc" do
|
||||
|
@ -106,7 +97,7 @@ describe TypesDeChamp::LinkedDropDownListTypeDeChamp do
|
|||
end
|
||||
|
||||
context "not mandatory" do
|
||||
let(:type_de_champ) { build(:type_de_champ_linked_drop_down_list, drop_down_list_value: menu_options, mandatory: false) }
|
||||
let(:type_de_champ) { build(:type_de_champ_linked_drop_down_list, drop_down_options: menu_options, mandatory: false) }
|
||||
|
||||
it do
|
||||
expect(subject.secondary_options).to eq(
|
||||
|
|
|
@ -71,7 +71,7 @@ describe 'shared/dossiers/edit', type: :view do
|
|||
|
||||
context 'when the list is long' do
|
||||
let(:value) { 'alpha' }
|
||||
let(:options) { [:long] }
|
||||
let(:options) { ['1', '2', '3', '4', '5', '6'] }
|
||||
|
||||
before { champ.update(value:) }
|
||||
|
||||
|
|
Loading…
Reference in a new issue