Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle autoload error not caught by safe_constantize #16608

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -190,6 +190,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