[#1421] replace master/slave by primary/secondary
This commit is contained in:
parent
dad3bc092b
commit
0cebf60c46
8 changed files with 84 additions and 84 deletions
|
@ -1,30 +1,30 @@
|
|||
document.addEventListener('turbolinks:load', function() {
|
||||
var masters, i, master, slave, slaveOptions;
|
||||
var primaries, i, primary, secondary, secondaryOptions;
|
||||
|
||||
masters = document.querySelectorAll('select[data-slave-options]');
|
||||
for (i = 0; i < masters.length; i++) {
|
||||
master = masters[i];
|
||||
slave = document.querySelector('select[data-slave-id="' + master.dataset.masterId + '"]');
|
||||
slaveOptions = JSON.parse(master.dataset.slaveOptions);
|
||||
primaries = document.querySelectorAll('select[data-secondary-options]');
|
||||
for (i = 0; i < primaries.length; i++) {
|
||||
primary = primaries[i];
|
||||
secondary = document.querySelector('select[data-secondary-id="' + primary.dataset.primaryId + '"]');
|
||||
secondaryOptions = JSON.parse(primary.dataset.secondaryOptions);
|
||||
|
||||
master.addEventListener('change', function(e) {
|
||||
primary.addEventListener('change', function(e) {
|
||||
var option, options, element;
|
||||
|
||||
while ((option = slave.firstChild)) {
|
||||
slave.removeChild(option);
|
||||
while ((option = secondary.firstChild)) {
|
||||
secondary.removeChild(option);
|
||||
}
|
||||
|
||||
options = slaveOptions[e.target.value];
|
||||
options = secondaryOptions[e.target.value];
|
||||
|
||||
for (i = 0; i < options.length; i++) {
|
||||
option = options[i];
|
||||
element = document.createElement("option");
|
||||
element.textContent = option;
|
||||
element.value = option;
|
||||
slave.appendChild(element);
|
||||
secondary.appendChild(element);
|
||||
}
|
||||
|
||||
slave.selectedIndex = 0;
|
||||
secondary.selectedIndex = 0;
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
@ -145,7 +145,7 @@ module NewUser
|
|||
def champs_params
|
||||
params.permit(dossier: {
|
||||
champs_attributes: [
|
||||
:id, :value, :master_value, :slave_value, :piece_justificative_file, value: [],
|
||||
:id, :value, :primary_value, :secondary_value, :piece_justificative_file, value: [],
|
||||
etablissement_attributes: Champs::SiretChamp::ETABLISSEMENT_ATTRIBUTES
|
||||
]
|
||||
})
|
||||
|
|
|
@ -1,36 +1,36 @@
|
|||
class Champs::LinkedDropDownListChamp < Champ
|
||||
attr_reader :master_value, :slave_value
|
||||
delegate :master_options, :slave_options, to: :type_de_champ
|
||||
attr_reader :primary_value, :secondary_value
|
||||
delegate :primary_options, :secondary_options, to: :type_de_champ
|
||||
|
||||
after_initialize :unpack_value
|
||||
|
||||
def unpack_value
|
||||
if value.present?
|
||||
master, slave = JSON.parse(value)
|
||||
primary, secondary = JSON.parse(value)
|
||||
else
|
||||
master = slave = ''
|
||||
primary = secondary = ''
|
||||
end
|
||||
@master_value ||= master
|
||||
@slave_value ||= slave
|
||||
@primary_value ||= primary
|
||||
@secondary_value ||= secondary
|
||||
end
|
||||
|
||||
def master_value=(value)
|
||||
@master_value = value
|
||||
def primary_value=(value)
|
||||
@primary_value = value
|
||||
pack_value
|
||||
end
|
||||
|
||||
def slave_value=(value)
|
||||
@slave_value = value
|
||||
def secondary_value=(value)
|
||||
@secondary_value = value
|
||||
pack_value
|
||||
end
|
||||
|
||||
def main_value_name
|
||||
:master_value
|
||||
:primary_value
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def pack_value
|
||||
self.value = JSON.generate([ master_value, slave_value ])
|
||||
self.value = JSON.generate([ primary_value, secondary_value ])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypeDeChamp
|
||||
MASTER_PATTERN = /^--(.*)--$/
|
||||
PRIMARY_PATTERN = /^--(.*)--$/
|
||||
|
||||
def master_options
|
||||
master_options = unpack_options.map(&:first)
|
||||
if master_options.present?
|
||||
master_options.unshift('')
|
||||
def primary_options
|
||||
primary_options = unpack_options.map(&:first)
|
||||
if primary_options.present?
|
||||
primary_options.unshift('')
|
||||
end
|
||||
master_options
|
||||
primary_options
|
||||
end
|
||||
|
||||
def slave_options
|
||||
slave_options = unpack_options.to_h
|
||||
if slave_options.present?
|
||||
slave_options[''] = []
|
||||
def secondary_options
|
||||
secondary_options = unpack_options.to_h
|
||||
if secondary_options.present?
|
||||
secondary_options[''] = []
|
||||
end
|
||||
slave_options
|
||||
secondary_options
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def unpack_options
|
||||
_, *options = drop_down_list.options
|
||||
chunked = options.slice_before(MASTER_PATTERN)
|
||||
chunked = options.slice_before(PRIMARY_PATTERN)
|
||||
chunked.map do |chunk|
|
||||
master, *slave = chunk
|
||||
slave.unshift('')
|
||||
[MASTER_PATTERN.match(master)[1], slave]
|
||||
primary, *secondary = chunk
|
||||
secondary.unshift('')
|
||||
[PRIMARY_PATTERN.match(primary)[1], secondary]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
- if champ.drop_down_list && champ.drop_down_list.options.any?
|
||||
- champ_id = champ.object_id
|
||||
= form.select :master_value,
|
||||
champ.master_options,
|
||||
= form.select :primary_value,
|
||||
champ.primary_options,
|
||||
{ required: champ.mandatory? },
|
||||
{ data: { "slave-options" => champ.slave_options, "master-id" => champ_id } }
|
||||
= form.select :slave_value,
|
||||
champ.slave_options[champ.master_value],
|
||||
{ data: { "secondary-options" => champ.secondary_options, "primary-id" => champ_id } }
|
||||
= form.select :secondary_value,
|
||||
champ.secondary_options[champ.primary_value],
|
||||
{ required: champ.mandatory? },
|
||||
{ data: { "slave-id" => champ_id } }
|
||||
{ data: { "secondary-id" => champ_id } }
|
||||
|
|
|
@ -6,13 +6,13 @@ feature 'linked dropdown lists' do
|
|||
|
||||
let(:list_items) do
|
||||
<<~END_OF_LIST
|
||||
--Master 1--
|
||||
Slave 1.1
|
||||
Slave 1.2
|
||||
--Master 2--
|
||||
Slave 2.1
|
||||
Slave 2.2
|
||||
Slave 2.3
|
||||
--Primary 1--
|
||||
Secondary 1.1
|
||||
Secondary 1.2
|
||||
--Primary 2--
|
||||
Secondary 2.1
|
||||
Secondary 2.2
|
||||
Secondary 2.3
|
||||
END_OF_LIST
|
||||
end
|
||||
let(:drop_down_list) { create(:drop_down_list, value: list_items) }
|
||||
|
@ -26,22 +26,22 @@ feature 'linked dropdown lists' do
|
|||
|
||||
let(:user_dossier) { user.dossiers.first }
|
||||
|
||||
scenario 'change master value, slave options are updated', js: true do
|
||||
scenario 'change primary value, secondary options are updated', js: true do
|
||||
log_in(user.email, password, procedure)
|
||||
|
||||
fill_individual
|
||||
|
||||
# Select a master value
|
||||
select('Master 2', from: master_id_for('linked dropdown'))
|
||||
# Select a primary value
|
||||
select('Primary 2', from: primary_id_for('linked dropdown'))
|
||||
|
||||
# Slave menu reflects chosen master value
|
||||
expect(page).to have_select(slave_id_for('linked dropdown'), options: ['', 'Slave 2.1', 'Slave 2.2', 'Slave 2.3'])
|
||||
# Secondary menu reflects chosen primary value
|
||||
expect(page).to have_select(secondary_id_for('linked dropdown'), options: ['', 'Secondary 2.1', 'Secondary 2.2', 'Secondary 2.3'])
|
||||
|
||||
# Select another master value
|
||||
select('Master 1', from: master_id_for('linked dropdown'))
|
||||
# Select another primary value
|
||||
select('Primary 1', from: primary_id_for('linked dropdown'))
|
||||
|
||||
# Slave menu gets updated
|
||||
expect(page).to have_select(slave_id_for('linked dropdown'), options: ['', 'Slave 1.1', 'Slave 1.2'])
|
||||
# Secondary menu gets updated
|
||||
expect(page).to have_select(secondary_id_for('linked dropdown'), options: ['', 'Secondary 1.1', 'Secondary 1.2'])
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -64,13 +64,13 @@ feature 'linked dropdown lists' do
|
|||
expect(page).to have_current_path(modifier_dossier_path(user_dossier))
|
||||
end
|
||||
|
||||
def master_id_for(libelle)
|
||||
def primary_id_for(libelle)
|
||||
find(:xpath, ".//label[contains(text()[normalize-space()], '#{libelle}')]")[:for]
|
||||
end
|
||||
|
||||
def slave_id_for(libelle)
|
||||
master_id = master_id_for(libelle)
|
||||
link = find("\##{master_id}")['data-master-id']
|
||||
find("[data-slave-id=\"#{link}\"]")['id']
|
||||
def secondary_id_for(libelle)
|
||||
primary_id = primary_id_for(libelle)
|
||||
link = find("\##{primary_id}")['data-primary-id']
|
||||
find("[data-secondary-id=\"#{link}\"]")['id']
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,12 +4,12 @@ describe Champs::LinkedDropDownListChamp do
|
|||
describe '#unpack_value' do
|
||||
let(:champ) { described_class.new(value: '["tata", "tutu"]') }
|
||||
|
||||
it { expect(champ.master_value).to eq('tata') }
|
||||
it { expect(champ.slave_value).to eq('tutu') }
|
||||
it { expect(champ.primary_value).to eq('tata') }
|
||||
it { expect(champ.secondary_value).to eq('tutu') }
|
||||
end
|
||||
|
||||
describe '#pack_value' do
|
||||
let(:champ) { described_class.new(master_value: 'tata', slave_value: 'tutu') }
|
||||
let(:champ) { described_class.new(primary_value: 'tata', secondary_value: 'tutu') }
|
||||
|
||||
before { champ.save }
|
||||
|
||||
|
|
|
@ -7,34 +7,34 @@ describe TypesDeChamp::LinkedDropDownListTypeDeChamp do
|
|||
|
||||
context 'with no options' do
|
||||
let(:menu_options) { '' }
|
||||
it { expect(type_de_champ.slave_options).to eq({}) }
|
||||
it { expect(type_de_champ.master_options).to eq([]) }
|
||||
it { expect(type_de_champ.secondary_options).to eq({}) }
|
||||
it { expect(type_de_champ.primary_options).to eq([]) }
|
||||
end
|
||||
|
||||
context 'with two master options' do
|
||||
context 'with two primary options' do
|
||||
let(:menu_options) do
|
||||
<<~END_OPTIONS
|
||||
--Master 1--
|
||||
slave 1.1
|
||||
slave 1.2
|
||||
--Master 2--
|
||||
slave 2.1
|
||||
slave 2.2
|
||||
slave 2.3
|
||||
--Primary 1--
|
||||
secondary 1.1
|
||||
secondary 1.2
|
||||
--Primary 2--
|
||||
secondary 2.1
|
||||
secondary 2.2
|
||||
secondary 2.3
|
||||
END_OPTIONS
|
||||
end
|
||||
|
||||
it do
|
||||
expect(type_de_champ.slave_options).to eq(
|
||||
expect(type_de_champ.secondary_options).to eq(
|
||||
{
|
||||
'' => [],
|
||||
'Master 1' => [ '', 'slave 1.1', 'slave 1.2'],
|
||||
'Master 2' => [ '', 'slave 2.1', 'slave 2.2', 'slave 2.3']
|
||||
'Primary 1' => [ '', 'secondary 1.1', 'secondary 1.2'],
|
||||
'Primary 2' => [ '', 'secondary 2.1', 'secondary 2.2', 'secondary 2.3']
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
it { expect(type_de_champ.master_options).to eq([ '', 'Master 1', 'Master 2' ]) }
|
||||
it { expect(type_de_champ.primary_options).to eq([ '', 'Primary 1', 'Primary 2' ]) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue