diff --git a/app/models/logic/binary_operator.rb b/app/models/logic/binary_operator.rb index 0a0d3e54c98..812fa0605b1 100644 --- a/app/models/logic/binary_operator.rb +++ b/app/models/logic/binary_operator.rb @@ -37,6 +37,8 @@ def compute(champs = []) l = @left.compute(champs) r = @right.compute(champs) + l = l[:value] if l.is_a?(Hash) + l&.send(operation, r) || false end diff --git a/app/models/logic/champ_value.rb b/app/models/logic/champ_value.rb index 5a096f3817b..a9638001b6f 100644 --- a/app/models/logic/champ_value.rb +++ b/app/models/logic/champ_value.rb @@ -51,19 +51,18 @@ def compute(champs) 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 diff --git a/app/models/logic/in_departement_operator.rb b/app/models/logic/in_departement_operator.rb index 47ab24b2c74..ee9532b36c5 100644 --- a/app/models/logic/in_departement_operator.rb +++ b/app/models/logic/in_departement_operator.rb @@ -4,12 +4,12 @@ def operation 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 = []) diff --git a/app/models/logic/in_region_operator.rb b/app/models/logic/in_region_operator.rb index 4e8221412dd..54625e98770 100644 --- a/app/models/logic/in_region_operator.rb +++ b/app/models/logic/in_region_operator.rb @@ -4,12 +4,12 @@ def operation 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 = []) diff --git a/app/models/logic/not_in_departement_operator.rb b/app/models/logic/not_in_departement_operator.rb index 83c598be6cf..472ef6c1807 100644 --- a/app/models/logic/not_in_departement_operator.rb +++ b/app/models/logic/not_in_departement_operator.rb @@ -4,11 +4,11 @@ def operation 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 diff --git a/app/models/logic/not_in_region_operator.rb b/app/models/logic/not_in_region_operator.rb index ac715d3978f..3bdac4d85e1 100644 --- a/app/models/logic/not_in_region_operator.rb +++ b/app/models/logic/not_in_region_operator.rb @@ -4,11 +4,11 @@ def operation 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 diff --git a/spec/models/logic/champ_value_spec.rb b/spec/models/logic/champ_value_spec.rb index 805e4436eca..254ac5bed12 100644 --- a/spec/models/logic/champ_value_spec.rb +++ b/spec/models/logic/champ_value_spec.rb @@ -93,7 +93,7 @@ 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 @@ 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 @@ 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