Merge pull request #7746 from betagouv/fix_condition_on_number_list

fix(conditionnal): corrige les conditions sur les listes de nombre
This commit is contained in:
LeSim 2022-09-09 11:35:10 +02:00 committed by GitHub
commit 93f6f22c9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 15 deletions

View file

@ -178,14 +178,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
end end
def current_right_valid?(left, right) def current_right_valid?(left, right)
case [left.type, right.type] Logic.compatible_type?(left, right)
in [:boolean, :boolean] | [:number, :number] | [:empty, :empty]
true
in [:enum, :string]
left.options.map(&:second).include?(right.value)
else
false
end
end end
def add_condition_tag def add_condition_tag

View file

@ -39,20 +39,27 @@ class ConditionForm
def row_to_condition(row) def row_to_condition(row)
left = Logic.from_json(row[:targeted_champ]) left = Logic.from_json(row[:targeted_champ])
right = parse_value(row[:value]) right = parse_value(left.type, row[:value])
Logic.class_from_name(row[:operator_name]).new(left, right) Logic.class_from_name(row[:operator_name]).new(left, right)
end end
def parse_value(value) def parse_value(left_type, value)
return empty if value.blank? return empty if value.blank?
number = Integer(value) rescue nil if left_type == :number
return constant(number) if number.present? # in this special case, we try to cast as Integer
# but it can still be a previous string value or a mistap
number = Integer(value) rescue nil
return constant(number) if number
end
json = JSON.parse(value) rescue nil # otherwise it can be a serialized Constant(true | false) term
return Logic.from_json(value) if json.present? # or a serialized Empty term
term = Logic.from_json(value) rescue nil
return term if term.present?
# if anything else, save it as a constant of string
constant(value) constant(value)
end end
end end

View file

@ -32,7 +32,7 @@ describe Administrateurs::ConditionsController, type: :controller do
end end
it do it do
expect(second_tdc.reload.condition).to eq(ds_eq(champ_value(1), constant(2))) expect(second_tdc.reload.condition).to eq(ds_eq(champ_value(1), constant('2')))
expect(assigns(:coordinate)).to eq(procedure.draft_revision.coordinate_for(second_tdc)) expect(assigns(:coordinate)).to eq(procedure.draft_revision.coordinate_for(second_tdc))
expect(assigns(:upper_tdcs)).to eq([first_coordinate.type_de_champ]) expect(assigns(:upper_tdcs)).to eq([first_coordinate.type_de_champ])
end end

View file

@ -3,12 +3,23 @@ describe ConditionForm, type: :model do
describe 'to_condition' do describe 'to_condition' do
let(:top_operator_name) { '' } let(:top_operator_name) { '' }
let(:champ_value_type) { :number }
subject { ConditionForm.new(rows: rows, top_operator_name: top_operator_name).to_condition } subject { ConditionForm.new(rows: rows, top_operator_name: top_operator_name).to_condition }
before do
allow_any_instance_of(Logic::ChampValue).to receive(:type).and_return(champ_value_type)
end
context 'when a row is added' do context 'when a row is added' do
let(:rows) { [{ targeted_champ: champ_value(1).to_json, operator_name: Logic::Eq.name, value: '1' }] } let(:rows) { [{ targeted_champ: champ_value(1).to_json, operator_name: Logic::Eq.name, value: '1' }] }
it { is_expected.to eq(ds_eq(champ_value(1), constant(1))) } it { is_expected.to eq(ds_eq(champ_value(1), constant(1))) }
context 'with a targeted enum' do
let(:champ_value_type) { :enum }
it { is_expected.to eq(ds_eq(champ_value(1), constant('1'))) }
end
end end
context 'when two rows are added' do context 'when two rows are added' do