Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(logic): compute sub champ values #9988

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/models/logic/binary_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 11 additions & 12 deletions app/models/logic/champ_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions app/models/logic/in_departement_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
Expand Down
4 changes: 2 additions & 2 deletions app/models/logic/in_region_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
Expand Down
4 changes: 2 additions & 2 deletions app/models/logic/not_in_departement_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions app/models/logic/not_in_region_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
47 changes: 18 additions & 29 deletions spec/models/logic/champ_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) }

Expand Down Expand Up @@ -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
Loading