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

New and improved Field.is_field?() #17801

Merged
merged 1 commit into from
Aug 9, 2018
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
15 changes: 3 additions & 12 deletions lib/miq_expression/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,16 @@ def self.parse(field)
end

def self.is_field?(field)
return false unless field.kind_of?(String)
match = REGEX.match(field)
return false unless match
model =
begin
match[:model_name].safe_constantize
rescue LoadError
nil
end
return false unless model
!!(model < ApplicationRecord)
parse(field)&.valid? || false
end

def to_s
"#{[model, *associations].join(".")}-#{column}"
end

def valid?
target.column_names.include?(column) || virtual_attribute? || custom_attribute_column?
(target < ApplicationRecord) &&
(target.column_names.include?(column) || virtual_attribute? || custom_attribute_column?)
end

def attribute_supported_by_sql?
Expand Down
7 changes: 6 additions & 1 deletion lib/miq_expression/target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ def self.parse!(field)
# returns hash:
# {:model_name => Host<ApplicationRecord> , :associations => ['vms']<Array>, :column_name => 'host_name' <String>}
def self.parse_params(field)
return unless field.kind_of?(String)
match = self::REGEX.match(field) || return
# convert matches to hash to format
# {:model_name => 'User', :associations => ...}
parsed_params = Hash[match.names.map(&:to_sym).zip(match.to_a[1..-1])]
parsed_params[:model_name] = parsed_params[:model_name].classify.safe_constantize
begin
parsed_params[:model_name] = parsed_params[:model_name].classify.safe_constantize
rescue LoadError # issues for case sensitivity (e.g.: VM vs vm)
parsed_params[:model_name] = nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought safe_constantize's purpose is to never raise a LoadError, but to return nil instead. How would the LoadError path ever occur?

Copy link
Member Author

@kbrock kbrock Aug 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fryguy

"Vm".safe_constantize # good
nil.safe_constantize # good
"XYZ".safe_constantize # good
"VM".safe_constantize
LoadError: Unable to autoload constant VM, expected app/models/vm.rb to define it

Copy link
Member Author

@kbrock kbrock Aug 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fryguy "Vm" fails for me all the time, but having trouble reproducing this for a rails PR.

safe_constantize does not catch LoadError - so it makes some sense.

This may be an auto_load path issue

end
parsed_params[:associations] = parsed_params[:associations].to_s.split(".")
parsed_params
end
Expand Down
6 changes: 5 additions & 1 deletion spec/lib/miq_expression/field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,11 @@

it "does not detect a string to looks like a field but isn't" do
expect(MiqExpression::Field.is_field?("NetworkManager-team")).to be_falsey
expect(described_class.is_field?("ManageIQ-name")).to be(false)
expect(described_class.is_field?("ManageIQ-name")).to be_falsey
end

it "handles regular expression" do
expect(MiqExpression::Field.is_field?(/x/)).to be_falsey
end
end
end