[#1421] Extract contents of both drop down lists
This commit is contained in:
parent
0d85997eca
commit
2214fc6b86
3 changed files with 70 additions and 0 deletions
|
@ -1,2 +1,3 @@
|
|||
class Champs::LinkedDropDownListChamp < Champ
|
||||
delegate :master_options, :slave_options, to: :type_de_champ
|
||||
end
|
||||
|
|
|
@ -1,2 +1,31 @@
|
|||
class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypeDeChamp
|
||||
MASTER_PATTERN = /^--(.*)--$/
|
||||
|
||||
def master_options
|
||||
master_options = unpack_options.map(&:first)
|
||||
if master_options.present?
|
||||
master_options.unshift('')
|
||||
end
|
||||
master_options
|
||||
end
|
||||
|
||||
def slave_options
|
||||
slave_options = unpack_options.to_h
|
||||
if slave_options.present?
|
||||
slave_options[''] = []
|
||||
end
|
||||
slave_options
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def unpack_options
|
||||
_, *options = drop_down_list.options
|
||||
chunked = options.slice_before(MASTER_PATTERN)
|
||||
chunked.map do |chunk|
|
||||
master, *slave = chunk
|
||||
slave.unshift('')
|
||||
[MASTER_PATTERN.match(master)[1], slave]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe TypesDeChamp::LinkedDropDownListTypeDeChamp do
|
||||
describe '#unpack_options' do
|
||||
let(:drop_down_list) { build(:drop_down_list, value: menu_options) }
|
||||
let(:type_de_champ) { described_class.new(drop_down_list: drop_down_list) }
|
||||
|
||||
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([]) }
|
||||
end
|
||||
|
||||
context 'with two master 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
|
||||
END_OPTIONS
|
||||
end
|
||||
|
||||
it do
|
||||
expect(type_de_champ.slave_options).to eq(
|
||||
{
|
||||
'' => [],
|
||||
'Master 1' => [ '', 'slave 1.1', 'slave 1.2'],
|
||||
'Master 2' => [ '', 'slave 2.1', 'slave 2.2', 'slave 2.3']
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
it { expect(type_de_champ.master_options).to eq([ '', 'Master 1', 'Master 2' ]) }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue