Skip to content

Commit

Permalink
converge MiqExpression#lenient evaluate and evaluate
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Nov 6, 2024
1 parent 1b2f4e7 commit 12d5b91
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/models/custom_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,14 @@ def evaluate_enablement_expression_for(object)
return true unless enablement_expression
return false if enablement_expression && !object # list

enablement_expression.lenient_evaluate(object)
enablement_expression.evaluate(object, :lenient => true)
end

def evaluate_visibility_expression_for(object)
return true unless visibility_expression
return false if visibility_expression && !object # object == nil, method is called for list of objects

visibility_expression.lenient_evaluate(object)
visibility_expression.evaluate(object, :lenient => true)
end

# End - Helper methods to support moving automate columns to resource_actions table
Expand Down
2 changes: 1 addition & 1 deletion app/models/miq_report/generator/trend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def build_results_for_report_trend(options)

if conditions
tz = User.lookup_by_userid(options[:userid]).get_timezone if options[:userid]
results = results.reject { |obj| conditions.lenient_evaluate(obj, tz) }
results = results.reject { |obj| conditions.evaluate(obj, tz, :lenient => true) }
end
results = results[0...options[:limit]] if options[:limit]
[results]
Expand Down
9 changes: 2 additions & 7 deletions lib/miq_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -575,14 +575,9 @@ def self.get_col_info(field, options = {})
}
end

def lenient_evaluate(obj, timezone = nil, prune_sql: false)
def evaluate(obj, timezone = nil, prune_sql: false, lenient: false)
ruby_exp = to_ruby(timezone, :prune_sql => prune_sql)
ruby_exp.nil? || Condition.subst_matches?(ruby_exp, obj)
end

def evaluate(obj, tz = nil)
ruby_exp = to_ruby(tz)
Condition.subst_matches?(ruby_exp, obj)
ruby_exp.nil? ? lenient : Condition.subst_matches?(ruby_exp, obj)
end

def self.evaluate_atoms(exp, obj)
Expand Down
2 changes: 1 addition & 1 deletion lib/rbac/filterer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ def get_belongsto_matches_for_storage(blist)
end

def matches_search_filters?(obj, filter, timezone, prune_sql: true)
filter.nil? || filter.lenient_evaluate(obj, timezone, :prune_sql => prune_sql)
filter.nil? || filter.evaluate(obj, timezone, :prune_sql => prune_sql, :lenient => true)
end
end
end
8 changes: 4 additions & 4 deletions spec/lib/miq_expression_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@
end
end

describe "#lenient_evaluate" do
describe "#evaluate with lenient" do
describe "integration" do
it "with a find/checkany expression" do
host1, host2, host3, host4, host5, host6, host7, host8 = FactoryBot.create_list(:host, 8)
Expand All @@ -1122,7 +1122,7 @@
"checkany" => {"FROM" => {"field" => "Host.vms-last_scan_on",
"value" => ["2011-01-08 17:00", "2011-01-09 23:30:59"]}},
"search" => {"IS NOT NULL" => {"field" => "Host.vms-description"}}})
result = Host.all.to_a.select { |rec| filter.lenient_evaluate(rec) }
result = Host.all.to_a.select { |rec| filter.evaluate(rec, :lenient => true) }
expect(result).to contain_exactly(host3, host5)
end

Expand All @@ -1148,15 +1148,15 @@
"value" => ["2011-01-08 17:00", "2011-01-09 23:30:59"]}},
"checkall" => {"IS NOT NULL" => {"field" => "Host.vms-description"}}}
)
result = Host.all.to_a.select { |rec| filter.lenient_evaluate(rec) }
result = Host.all.to_a.select { |rec| filter.evaluate(rec, :lenient => true) }
expect(result).to eq([host2])
end

it "cannot execute non-attribute methods on target objects" do
vm = FactoryBot.create(:vm_vmware)

expect do
described_class.new("=" => {"field" => "Vm-destroy", "value" => true}).lenient_evaluate(vm)
described_class.new("=" => {"field" => "Vm-destroy", "value" => true}).evaluate(vm, :lenient => true)
end.not_to change(Vm, :count)
end
end
Expand Down

0 comments on commit 12d5b91

Please sign in to comment.