This commit is contained in:
simon lehericey 2022-09-26 21:07:43 +02:00
parent 823585dd33
commit 70022450cf
12 changed files with 32 additions and 31 deletions

View file

@ -105,7 +105,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
end
def compatibles_operators_for_select(left)
case left.type
case left.type(@upper_tdcs)
when ChampValue::CHAMP_VALUE_TYPE.fetch(:boolean)
[
[t('is', scope: 'logic'), Eq.name]
@ -135,7 +135,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
def right_operand_tag(left, right, row_index)
right_invalid = !current_right_valid?(left, right)
case left.type
case left.type(@upper_tdcs)
when :boolean
booleans_for_select = [[t('utils.yes'), constant(true).to_json], [t('utils.no'), constant(false).to_json]]

View file

@ -39,7 +39,7 @@ class ConditionForm
def row_to_condition(row)
left = Logic.from_json(row[:targeted_champ])
right = parse_value(left.type, row[:value])
right = parse_value(left.type(upper_tdcs), row[:value])
Logic.class_from_name(row[:operator_name]).new(left, right)
end

View file

@ -17,7 +17,7 @@ module Logic
right = condition.right
operator_class = condition.class
case [left.type, condition]
case [left.type(type_de_champs), condition]
in [:boolean, _]
operator_class = Eq
in [:empty, _]
@ -32,7 +32,7 @@ module Logic
end
if !compatible_type?(left, right)
right = case left.type
right = case left.type(type_de_champs)
when :boolean
Constant.new(true)
when :empty
@ -48,7 +48,7 @@ module Logic
end
def self.compatible_type?(left, right)
case [left.type, right.type]
case [left.type(type_de_champs), right.type(type_de_champs)]
in [a, ^a] # syntax for same type
true
in [:enum, :string] | [:enums, :string]

View file

@ -20,14 +20,14 @@ class Logic::BinaryOperator < Logic::Term
def errors(stable_ids = [])
errors = []
if @left.type != :number || @right.type != :number
if @left.type(type_de_champs) != :number || @right.type(type_de_champs) != :number
errors << { type: :required_number, operator_name: self.class.name }
end
errors + @left.errors(stable_ids) + @right.errors(stable_ids)
end
def type = :boolean
def type(type_de_champs = []) = :boolean
def compute(champs = [])
l = @left.compute(champs)

View file

@ -29,7 +29,7 @@ class Logic::ChampValue < Logic::Term
return nil if !targeted_champ.visible?
return nil if targeted_champ.blank?
case type_de_champ.type_champ
case type_de_champ(champs.map(&:type_de_champ)).type_champ
when MANAGED_TYPE_DE_CHAMP.fetch(:yes_no),
MANAGED_TYPE_DE_CHAMP.fetch(:checkbox)
targeted_champ.true?
@ -44,8 +44,8 @@ class Logic::ChampValue < Logic::Term
def to_s = type_de_champ&.libelle # TODO: gerer le cas ou un tdc est supprimé
def type
case type_de_champ&.type_champ # TODO: gerer le cas ou un tdc est supprimé
def type(type_de_champs)
case type_de_champ(type_de_champs)&.type_champ # TODO: gerer le cas ou un tdc est supprimé
when MANAGED_TYPE_DE_CHAMP.fetch(:yes_no),
MANAGED_TYPE_DE_CHAMP.fetch(:checkbox)
CHAMP_VALUE_TYPE.fetch(:boolean)
@ -83,9 +83,10 @@ class Logic::ChampValue < Logic::Term
self.class == other.class && @stable_id == other.stable_id
end
def options
opts = type_de_champ.drop_down_list_enabled_non_empty_options.map { |option| [option, option] }
if type_de_champ.drop_down_other?
def options(type_de_champs)
tdc = type_de_champ(type_de_champs)
opts = tdc.drop_down_list_enabled_non_empty_options.map { |option| [option, option] }
if tdc.drop_down_other?
opts + [["Autre", Champs::DropDownListChamp::OTHER]]
else
opts
@ -94,8 +95,8 @@ class Logic::ChampValue < Logic::Term
private
def type_de_champ
TypeDeChamp.find_by(stable_id: stable_id)
def type_de_champ(type_de_champs)
type_de_champs.find { |c| c.stable_id == stable_id }
end
def champ(champs)

View file

@ -18,7 +18,7 @@ class Logic::Constant < Logic::Term
end
end
def type
def type(_type_de_champs = [])
case @value
when TrueClass, FalseClass
:boolean

View file

@ -1,7 +1,7 @@
class Logic::Empty < Logic::Term
def to_s = I18n.t('logic.empty')
def type = :empty
def type(_type_de_champs = []) = :empty
def errors(_stable_ids = nil) = ['empty']

View file

@ -1,7 +1,7 @@
class Logic::EmptyOperator < Logic::BinaryOperator
def to_s = "empty operator"
def type = :empty
def type(_type_de_champs = []) = :empty
def errors(_stable_ids = nil) = []

View file

@ -3,7 +3,7 @@ class Logic::Eq < Logic::BinaryOperator
def errors(stable_ids = [])
errors = [@left, @right]
.filter { |term| term.type == :unmanaged }
.filter { |term| term.type(type_de_champs) == :unmanaged }
.map { |term| { type: :unmanaged, stable_id: term.stable_id } }
if !Logic.compatible_type?(@left, @right)
@ -13,7 +13,7 @@ class Logic::Eq < Logic::BinaryOperator
right: @right,
operator_name: self.class.name
}
elsif @left.type == :enum &&
elsif @left.type(type_de_champs) == :enum &&
!left.options.map(&:second).include?(right.value)
errors << {
type: :not_included,

View file

@ -4,7 +4,7 @@ class Logic::IncludeOperator < Logic::BinaryOperator
def errors(stable_ids = [])
result = []
if left_not_a_list?
if left_not_a_list?(type_de_champs)
result << { type: :required_list }
elsif right_value_not_in_list?
result << {
@ -19,8 +19,8 @@ class Logic::IncludeOperator < Logic::BinaryOperator
private
def left_not_a_list?
@left.type != :enums
def left_not_a_list?(type_de_champs)
@left.type(type_de_champs) != :enums
end
def right_value_not_in_list?

View file

@ -23,7 +23,7 @@ class Logic::NAryOperator < Logic::Term
errors += ["opérateur '#{operator_name}' vide"]
end
not_booleans = @operands.filter { |operand| operand.type != :boolean }
not_booleans = @operands.filter { |operand| operand.type(type_de_champs) != :boolean }
if not_booleans.present?
errors += ["'#{operator_name}' ne contient pas que des booléens : #{not_booleans.map(&:to_s).join(', ')}"]
end
@ -31,7 +31,7 @@ class Logic::NAryOperator < Logic::Term
errors + @operands.flat_map { |operand| operand.errors(stable_ids) }
end
def type = :boolean
def type(_type_de_champs = []) = :boolean
def ==(other)
self.class == other.class &&

View file

@ -7,7 +7,7 @@ describe Logic::ChampValue do
let(:value) { 'true' }
let(:champ) { create(:champ_yes_no, value: value) }
it { expect(champ_value(champ.stable_id).type).to eq(:boolean) }
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:boolean) }
context 'with true value' do
it { is_expected.to be(true) }
@ -31,7 +31,7 @@ describe Logic::ChampValue do
context 'integer tdc' do
let(:champ) { create(:champ_integer_number, value: '42') }
it { expect(champ_value(champ.stable_id).type).to eq(:number) }
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:number) }
it { is_expected.to eq(42) }
context 'with a blank value' do
@ -44,14 +44,14 @@ describe Logic::ChampValue do
context 'decimal tdc' do
let(:champ) { create(:champ_decimal_number, value: '42.01') }
it { expect(champ_value(champ.stable_id).type).to eq(:number) }
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:number) }
it { is_expected.to eq(42.01) }
end
context 'dropdown tdc' do
let(:champ) { create(:champ_drop_down_list, value: 'val1') }
it { expect(champ_value(champ.stable_id).type).to eq(:enum) }
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:enum) }
it { is_expected.to eq('val1') }
it { expect(champ_value(champ.stable_id).options).to match_array([["val1", "val1"], ["val2", "val2"], ["val3", "val3"]]) }
@ -72,7 +72,7 @@ describe Logic::ChampValue do
context 'checkbox tdc' do
let(:champ) { create(:champ_checkbox, value: 'on') }
it { expect(champ_value(champ.stable_id).type).to eq(:boolean) }
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:boolean) }
it { is_expected.to eq(true) }
end