[#1421] Extract values for both drop down lists

This commit is contained in:
Frederic Merizen 2018-06-21 09:50:34 +02:00
parent 2214fc6b86
commit 7b1e8c2110
2 changed files with 47 additions and 0 deletions

View file

@ -1,3 +1,32 @@
class Champs::LinkedDropDownListChamp < Champ class Champs::LinkedDropDownListChamp < Champ
attr_reader :master_value, :slave_value
delegate :master_options, :slave_options, to: :type_de_champ delegate :master_options, :slave_options, to: :type_de_champ
after_initialize :unpack_value
def unpack_value
if value.present?
master, slave = JSON.parse(value)
else
master = slave = ''
end
@master_value ||= master
@slave_value ||= slave
end
def master_value=(value)
@master_value = value
pack_value
end
def slave_value=(value)
@slave_value = value
pack_value
end
private
def pack_value
self.value = JSON.generate([ master_value, slave_value ])
end
end end

View file

@ -0,0 +1,18 @@
require 'spec_helper'
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') }
end
describe '#pack_value' do
let(:champ) { described_class.new(master_value: 'tata', slave_value: 'tutu') }
before { champ.save }
it { expect(champ.value).to eq('["tata","tutu"]') }
end
end