Merge pull request #9988 from tchak/refactor-logic-compute-sub-values
refactor(logic): compute sub champ values
This commit is contained in:
commit
7d9cb4d497
7 changed files with 39 additions and 49 deletions
|
@ -37,6 +37,8 @@ class Logic::BinaryOperator < Logic::Term
|
|||
l = @left.compute(champs)
|
||||
r = @right.compute(champs)
|
||||
|
||||
l = l[:value] if l.is_a?(Hash)
|
||||
|
||||
l&.send(operation, r) || false
|
||||
end
|
||||
|
||||
|
|
|
@ -51,19 +51,18 @@ class Logic::ChampValue < Logic::Term
|
|||
targeted_champ.selected
|
||||
when "Champs::MultipleDropDownListChamp"
|
||||
targeted_champ.selected_options
|
||||
when "Champs::DepartementChamp", "Champs::RegionChamp"
|
||||
when "Champs::RegionChamp"
|
||||
targeted_champ.code
|
||||
end
|
||||
end
|
||||
|
||||
def compute_value_json(champs)
|
||||
targeted_champ = champ(champs)
|
||||
|
||||
return nil if !targeted_champ.visible?
|
||||
return nil if targeted_champ.blank? & !targeted_champ.drop_down_other?
|
||||
|
||||
if targeted_champ.type.in?(["Champs::CommuneChamp", "Champs::EpciChamp", "Champs::DepartementChamp"])
|
||||
targeted_champ.value_json
|
||||
when "Champs::DepartementChamp"
|
||||
{
|
||||
value: targeted_champ.code,
|
||||
code_region: targeted_champ.code_region
|
||||
}
|
||||
when "Champs::CommuneChamp", "Champs::EpciChamp"
|
||||
{
|
||||
code_departement: targeted_champ.code_departement,
|
||||
code_region: targeted_champ.code_region
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -4,12 +4,12 @@ class Logic::InDepartementOperator < Logic::BinaryOperator
|
|||
end
|
||||
|
||||
def compute(champs = [])
|
||||
l = @left.compute_value_json(champs)
|
||||
l = @left.compute(champs)
|
||||
r = @right.compute(champs)
|
||||
|
||||
return false if l.nil?
|
||||
|
||||
l.fetch("code_departement") == r
|
||||
l.fetch(:code_departement) == r
|
||||
end
|
||||
|
||||
def errors(type_de_champs = [])
|
||||
|
|
|
@ -4,12 +4,12 @@ class Logic::InRegionOperator < Logic::BinaryOperator
|
|||
end
|
||||
|
||||
def compute(champs)
|
||||
l = @left.compute_value_json(champs)
|
||||
l = @left.compute(champs)
|
||||
r = @right.compute(champs)
|
||||
|
||||
return false if l.nil?
|
||||
|
||||
l.fetch("code_region") == r
|
||||
l.fetch(:code_region) == r
|
||||
end
|
||||
|
||||
def errors(type_de_champs = [])
|
||||
|
|
|
@ -4,11 +4,11 @@ class Logic::NotInDepartementOperator < Logic::InDepartementOperator
|
|||
end
|
||||
|
||||
def compute(champs = [])
|
||||
l = @left.compute_value_json(champs)
|
||||
l = @left.compute(champs)
|
||||
r = @right.compute(champs)
|
||||
|
||||
return false if l.nil?
|
||||
|
||||
l.fetch("code_departement") != r
|
||||
l.fetch(:code_departement) != r
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,11 +4,11 @@ class Logic::NotInRegionOperator < Logic::InRegionOperator
|
|||
end
|
||||
|
||||
def compute(champs)
|
||||
l = @left.compute_value_json(champs)
|
||||
l = @left.compute(champs)
|
||||
r = @right.compute(champs)
|
||||
|
||||
return false if l.nil?
|
||||
|
||||
l.fetch("code_region") != r
|
||||
l.fetch(:code_region) != r
|
||||
end
|
||||
end
|
||||
|
|
|
@ -93,7 +93,7 @@ describe Logic::ChampValue do
|
|||
let(:champ) { create(:champ_departements, value: '02') }
|
||||
|
||||
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:departement_enum) }
|
||||
it { is_expected.to eq('02') }
|
||||
it { is_expected.to eq({ value: '02', code_region: '32' }) }
|
||||
end
|
||||
|
||||
context 'region tdc' do
|
||||
|
@ -102,6 +102,23 @@ describe Logic::ChampValue do
|
|||
it { is_expected.to eq('04') }
|
||||
end
|
||||
|
||||
context 'commune tdc' do
|
||||
let(:champ) { create(:champ_communes, code_postal: '92500', external_id: '92063') }
|
||||
|
||||
it { is_expected.to eq({ code_departement: '92', code_region: '11' }) }
|
||||
end
|
||||
|
||||
context 'epci tdc' do
|
||||
let(:champ) { build(:champ_epci, code_departement: '43') }
|
||||
|
||||
before do
|
||||
champ.save!
|
||||
champ.update_columns(external_id: '244301016', value: 'CC des Sucs')
|
||||
end
|
||||
|
||||
it { is_expected.to eq({ code_departement: '43', code_region: '84' }) }
|
||||
end
|
||||
|
||||
describe 'errors' do
|
||||
let(:champ) { create(:champ) }
|
||||
|
||||
|
@ -140,32 +157,4 @@ describe Logic::ChampValue do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#compute_value_json' do
|
||||
subject { champ_value(champ.stable_id).compute_value_json([champ]) }
|
||||
|
||||
context 'commune tdc' do
|
||||
let(:champ) { create(:champ_communes, code_postal: '92500', external_id: '92063') }
|
||||
|
||||
it { is_expected.to eq({ 'code_departement' => '92', 'code_postal' => '92500', 'code_region' => '11' }) }
|
||||
end
|
||||
|
||||
context 'epci tdc' do
|
||||
let(:champ) { build(:champ_epci, code_departement: '43') }
|
||||
|
||||
before do
|
||||
champ.save!
|
||||
champ.update_columns(external_id: '244301016', value: 'CC des Sucs')
|
||||
end
|
||||
|
||||
it { is_expected.to eq({ 'code_departement' => '43', 'code_region' => '84' }) }
|
||||
end
|
||||
|
||||
context 'departement tdc' do
|
||||
let(:champ) { create(:champ_departements, value: '02') }
|
||||
|
||||
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:departement_enum) }
|
||||
it { is_expected.to eq({ 'code_region' => '32' }) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue