fix(conditional): only parse user input as int if compared with a number

This commit is contained in:
simon lehericey 2022-09-09 10:37:31 +02:00
parent 478a7e68ab
commit c43bf2e5c9
3 changed files with 25 additions and 7 deletions

View file

@ -39,20 +39,27 @@ class ConditionForm
def row_to_condition(row)
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)
end
def parse_value(value)
def parse_value(left_type, value)
return empty if value.blank?
number = Integer(value) rescue nil
return constant(number) if number.present?
if left_type == :number
# 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
return Logic.from_json(value) if json.present?
# otherwise it can be a serialized Constant(true | false) term
# 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)
end
end