Skip to content

Commit

Permalink
Merge pull request #16608 from imtayadeway/bug/miq-expression-field-v…
Browse files Browse the repository at this point in the history
…alues

Handle autoload error not caught by safe_constantize
(cherry picked from commit 7a5fd37)

https://bugzilla.redhat.com/show_bug.cgi?id=1522846
  • Loading branch information
Fryguy authored and simaishi committed Dec 11, 2017
1 parent 7a01394 commit b175209
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/miq_expression/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ def self.is_field?(field)
return false unless field.kind_of?(String)
match = REGEX.match(field)
return false unless match
model = match[:model_name].safe_constantize
model =
begin
match[:model_name].safe_constantize
rescue LoadError
nil
end
return false unless model
!!(model < ApplicationRecord)
end
Expand Down
5 changes: 5 additions & 0 deletions spec/lib/miq_expression_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@
expect(sql).to eq("\"vms\".\"name\" = \"vms\".\"name\"")
end

it "will handle values that look like they contain MiqExpression-encoded constants but cannot be loaded" do
sql, * = described_class.new("=" => {"field" => "Vm-name", "value" => "VM-name"}).to_sql
expect(sql).to eq(%q("vms"."name" = 'VM-name'))
end

it "generates the SQL for a < expression" do
sql, * = described_class.new("<" => {"field" => "Vm.hardware-cpu_sockets", "value" => "2"}).to_sql
expect(sql).to eq("\"hardwares\".\"cpu_sockets\" < 2")
Expand Down

0 comments on commit b175209

Please sign in to comment.