Merge pull request #9714 from demarches-simplifiees/add-not-include-operator

ETQ admin je peux conditionner / router à partir d'un champ de type choix multiple avec l'opérateur "Ne contient pas"
This commit is contained in:
Eric Leroy-Terquem 2023-11-22 16:29:24 +00:00 committed by GitHub
commit d92155a96a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 4 deletions

View file

@ -99,7 +99,8 @@ class Conditions::ConditionsComponent < ApplicationComponent
]
when ChampValue::CHAMP_VALUE_TYPE.fetch(:enums)
[
[t(IncludeOperator.name, scope: 'logic.operators'), IncludeOperator.name]
[t(IncludeOperator.name, scope: 'logic.operators'), IncludeOperator.name],
[t(ExcludeOperator.name, scope: 'logic.operators'), ExcludeOperator.name]
]
when ChampValue::CHAMP_VALUE_TYPE.fetch(:number)
[Eq, LessThan, GreaterThan, LessThanEq, GreaterThanEq]

View file

@ -92,7 +92,7 @@ class GroupeInstructeur < ApplicationRecord
end
def valid_rule_line?(rule)
([rule.left, rule, rule.right] in [ChampValue, (LessThan | LessThanEq | Eq | NotEq | GreaterThanEq | GreaterThan | IncludeOperator), Constant]) && routing_rule_matches_tdc?(rule)
!rule.is_a?(EmptyOperator) && routing_rule_matches_tdc?(rule)
end
def non_unique_rule?

View file

@ -8,7 +8,7 @@ module Logic
end
def self.class_from_name(name)
[ChampValue, Constant, Empty, LessThan, LessThanEq, Eq, NotEq, GreaterThanEq, GreaterThan, EmptyOperator, IncludeOperator, And, Or]
[ChampValue, Constant, Empty, LessThan, LessThanEq, Eq, NotEq, GreaterThanEq, GreaterThan, EmptyOperator, IncludeOperator, ExcludeOperator, And, Or]
.find { |c| c.name == name }
end
@ -88,6 +88,8 @@ module Logic
def ds_include(left, right) = Logic::IncludeOperator.new(left, right)
def ds_exclude(left, right) = Logic::ExcludeOperator.new(left, right)
def constant(value) = Logic::Constant.new(value)
def champ_value(stable_id) = Logic::ChampValue.new(stable_id)

View file

@ -0,0 +1,3 @@
class Logic::ExcludeOperator < Logic::IncludeOperator
def operation = :exclude?
end

View file

@ -14,3 +14,4 @@ fr:
'Logic::Or': Ou
'Logic::NotEq': Nest pas
'Logic::IncludeOperator': Contient
'Logic::ExcludeOperator': Ne contient pas

View file

@ -0,0 +1,29 @@
describe Logic::ExcludeOperator do
include Logic
let(:champ) { create(:champ_multiple_drop_down_list, value: '["val1", "val2"]') }
describe '#compute' do
it { expect(ds_exclude(champ_value(champ.stable_id), constant('val1')).compute([champ])).to be(false) }
it { expect(ds_exclude(champ_value(champ.stable_id), constant('something else')).compute([champ])).to be(true) }
end
describe '#errors' do
it { expect(ds_exclude(champ_value(champ.stable_id), constant('val1')).errors([champ.type_de_champ])).to be_empty }
it do
expected = {
right: constant('something else'),
stable_id: champ.stable_id,
type: :not_included
}
expect(ds_exclude(champ_value(champ.stable_id), constant('something else')).errors([champ.type_de_champ])).to eq([expected])
end
it { expect(ds_exclude(constant(1), constant('val1')).errors([])).to eq([{ type: :required_list }]) }
end
describe '#==' do
it { expect(ds_include(champ_value(champ.stable_id), constant('val1'))).to eq(ds_include(champ_value(champ.stable_id), constant('val1'))) }
end
end

View file

@ -28,7 +28,7 @@ describe RoutingEngine, type: :model do
context 'without any matching rules' do
before do
procedure.groupe_instructeurs.each do |gi|
gi.update(routing_rule: ds_eq(constant(false), constant(false)))
gi.update(routing_rule: ds_eq(constant(false), constant(true)))
end
end