chore(conditional): add not_in_departement and not_in_region operators
This commit is contained in:
parent
3db21b633c
commit
e40d1107ac
8 changed files with 115 additions and 6 deletions
|
@ -100,13 +100,16 @@ class Conditions::ConditionsComponent < ApplicationComponent
|
|||
when ChampValue::CHAMP_VALUE_TYPE.fetch(:commune_enum), ChampValue::CHAMP_VALUE_TYPE.fetch(:epci_enum)
|
||||
[
|
||||
[t(InDepartementOperator.name, scope: 'logic.operators'), InDepartementOperator.name],
|
||||
[t(InRegionOperator.name, scope: 'logic.operators'), InRegionOperator.name]
|
||||
[t(NotInDepartementOperator.name, scope: 'logic.operators'), NotInDepartementOperator.name],
|
||||
[t(InRegionOperator.name, scope: 'logic.operators'), InRegionOperator.name],
|
||||
[t(NotInRegionOperator.name, scope: 'logic.operators'), NotInRegionOperator.name]
|
||||
]
|
||||
when ChampValue::CHAMP_VALUE_TYPE.fetch(:departement_enum)
|
||||
[
|
||||
[t('is', scope: 'logic'), Eq.name],
|
||||
[t('is_not', scope: 'logic'), NotEq.name],
|
||||
[t(InRegionOperator.name, scope: 'logic.operators'), InRegionOperator.name]
|
||||
[t(InRegionOperator.name, scope: 'logic.operators'), InRegionOperator.name],
|
||||
[t(NotInRegionOperator.name, scope: 'logic.operators'), NotInRegionOperator.name]
|
||||
]
|
||||
when ChampValue::CHAMP_VALUE_TYPE.fetch(:enums)
|
||||
[
|
||||
|
|
|
@ -8,8 +8,26 @@ module Logic
|
|||
end
|
||||
|
||||
def self.class_from_name(name)
|
||||
[ChampValue, Constant, Empty, LessThan, LessThanEq, Eq, NotEq, GreaterThanEq, GreaterThan, EmptyOperator, IncludeOperator, ExcludeOperator, And, Or, InDepartementOperator, InRegionOperator]
|
||||
.find { |c| c.name == name }
|
||||
[
|
||||
ChampValue,
|
||||
Constant,
|
||||
Empty,
|
||||
LessThan,
|
||||
LessThanEq,
|
||||
Eq,
|
||||
NotEq,
|
||||
GreaterThanEq,
|
||||
GreaterThan,
|
||||
EmptyOperator,
|
||||
IncludeOperator,
|
||||
ExcludeOperator,
|
||||
And,
|
||||
Or,
|
||||
InDepartementOperator,
|
||||
NotInDepartementOperator,
|
||||
InRegionOperator,
|
||||
NotInRegionOperator
|
||||
].find { |c| c.name == name }
|
||||
end
|
||||
|
||||
def self.ensure_compatibility_from_left(condition, type_de_champs)
|
||||
|
@ -94,8 +112,12 @@ module Logic
|
|||
|
||||
def ds_in_departement(left, right) = Logic::InDepartementOperator.new(left, right)
|
||||
|
||||
def ds_not_in_departement(left, right) = Logic::NotInDepartementOperator.new(left, right)
|
||||
|
||||
def ds_in_region(left, right) = Logic::InRegionOperator.new(left, right)
|
||||
|
||||
def ds_not_in_region(left, right) = Logic::NotInRegionOperator.new(left, right)
|
||||
|
||||
def ds_exclude(left, right) = Logic::ExcludeOperator.new(left, right)
|
||||
|
||||
def constant(value) = Logic::Constant.new(value)
|
||||
|
|
|
@ -120,9 +120,9 @@ class Logic::ChampValue < Logic::Term
|
|||
def options(type_de_champs, operator_name = nil)
|
||||
tdc = type_de_champ(type_de_champs)
|
||||
|
||||
if operator_name == Logic::InRegionOperator.name || tdc.type_champ == MANAGED_TYPE_DE_CHAMP.fetch(:regions)
|
||||
if operator_name.in?([Logic::InRegionOperator.name, Logic::NotInRegionOperator.name]) || tdc.type_champ == MANAGED_TYPE_DE_CHAMP.fetch(:regions)
|
||||
APIGeoService.regions.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] }
|
||||
elsif operator_name == Logic::InDepartementOperator.name || tdc.type_champ.in?([MANAGED_TYPE_DE_CHAMP.fetch(:communes), MANAGED_TYPE_DE_CHAMP.fetch(:epci), MANAGED_TYPE_DE_CHAMP.fetch(:departements)])
|
||||
elsif operator_name.in?([Logic::InDepartementOperator.name, Logic::NotInDepartementOperator.name]) || tdc.type_champ.in?([MANAGED_TYPE_DE_CHAMP.fetch(:communes), MANAGED_TYPE_DE_CHAMP.fetch(:epci), MANAGED_TYPE_DE_CHAMP.fetch(:departements)])
|
||||
APIGeoService.departements.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] }
|
||||
else
|
||||
tdc.drop_down_list_enabled_non_empty_options(other: true).map { _1.is_a?(Array) ? _1 : [_1, _1] }
|
||||
|
|
14
app/models/logic/not_in_departement_operator.rb
Normal file
14
app/models/logic/not_in_departement_operator.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
class Logic::NotInDepartementOperator < Logic::InDepartementOperator
|
||||
def operation
|
||||
:n_est_pas_dans_le_departement
|
||||
end
|
||||
|
||||
def compute(champs = [])
|
||||
l = @left.compute_value_json(champs)
|
||||
r = @right.compute(champs)
|
||||
|
||||
return false if l.nil?
|
||||
|
||||
l.fetch("code_departement") != r
|
||||
end
|
||||
end
|
14
app/models/logic/not_in_region_operator.rb
Normal file
14
app/models/logic/not_in_region_operator.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
class Logic::NotInRegionOperator < Logic::InRegionOperator
|
||||
def operation
|
||||
:n_est_pas_dans_la_region
|
||||
end
|
||||
|
||||
def compute(champs)
|
||||
l = @left.compute_value_json(champs)
|
||||
r = @right.compute(champs)
|
||||
|
||||
return false if l.nil?
|
||||
|
||||
l.fetch("code_region") != r
|
||||
end
|
||||
end
|
|
@ -16,4 +16,6 @@ fr:
|
|||
'Logic::IncludeOperator': Contient
|
||||
'Logic::ExcludeOperator': Ne contient pas
|
||||
'Logic::InDepartementOperator': Est dans le département
|
||||
'Logic::NotInDepartementOperator': N'est pas dans le département
|
||||
'Logic::InRegionOperator': Est dans la région
|
||||
'Logic::NotInRegionOperator': N'est pas dans la région
|
||||
|
|
23
spec/models/logic/not_in_departement_operator_spec.rb
Normal file
23
spec/models/logic/not_in_departement_operator_spec.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
describe Logic::NotInDepartementOperator do
|
||||
include Logic
|
||||
|
||||
let(:champ_commune) { create(:champ_communes, code_postal: '92500', external_id: '92063') }
|
||||
let(:champ_epci) { create(:champ_epci, code_departement: '02', code_region: "32") }
|
||||
|
||||
describe '#compute' do
|
||||
context 'commune' do
|
||||
it do
|
||||
expect(ds_not_in_departement(champ_value(champ_commune.stable_id), constant('93')).compute([champ_commune])).to be(true)
|
||||
expect(ds_not_in_departement(champ_value(champ_commune.stable_id), constant('92')).compute([champ_commune])).to be(false)
|
||||
end
|
||||
end
|
||||
|
||||
context 'epci' do
|
||||
it do
|
||||
champ_epci.update_columns(external_id: "200071991", value: "CC Retz en Valois")
|
||||
expect(ds_not_in_departement(champ_value(champ_epci.stable_id), constant('02')).compute([champ_epci])).to be(false)
|
||||
expect(ds_not_in_departement(champ_value(champ_epci.stable_id), constant('03')).compute([champ_epci])).to be(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
31
spec/models/logic/not_in_region_operator_spec.rb
Normal file
31
spec/models/logic/not_in_region_operator_spec.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
describe Logic::NotInRegionOperator do
|
||||
include Logic
|
||||
|
||||
let(:champ_commune) { create(:champ_communes, code_postal: '92500', external_id: '92063') }
|
||||
let(:champ_epci) { create(:champ_epci, code_departement: '02', code_region: "32") }
|
||||
let(:champ_departement) { create(:champ_departements, value: '01', code_region: '84') }
|
||||
|
||||
describe '#compute' do
|
||||
context 'commune' do
|
||||
it do
|
||||
expect(ds_not_in_region(champ_value(champ_commune.stable_id), constant('11')).compute([champ_commune])).to be(false)
|
||||
expect(ds_not_in_region(champ_value(champ_commune.stable_id), constant('32')).compute([champ_commune])).to be(true)
|
||||
end
|
||||
end
|
||||
|
||||
context 'epci' do
|
||||
it do
|
||||
champ_epci.update_columns(external_id: "200071991", value: "CC Retz en Valois")
|
||||
expect(ds_not_in_region(champ_value(champ_epci.stable_id), constant('32')).compute([champ_epci])).to be(false)
|
||||
expect(ds_not_in_region(champ_value(champ_epci.stable_id), constant('11')).compute([champ_epci])).to be(true)
|
||||
end
|
||||
end
|
||||
|
||||
context 'departement' do
|
||||
it do
|
||||
expect(ds_not_in_region(champ_value(champ_departement.stable_id), constant('84')).compute([champ_departement])).to be(false)
|
||||
expect(ds_not_in_region(champ_value(champ_departement.stable_id), constant('32')).compute([champ_departement])).to be(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue