Skip to content

Commit

Permalink
Merge pull request #15498 from jntullo/euwe_correct_field_names
Browse files Browse the repository at this point in the history
[EUWE] correct field names in reports
  • Loading branch information
simaishi authored Jul 7, 2017
2 parents e89b90e + ced36f0 commit fee815e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
4 changes: 4 additions & 0 deletions lib/miq_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ def initialize(exp, ctype = nil)
@context_type = ctype
end

def self.parse_field_or_tag(str)
MiqExpression::Field.parse(str) || MiqExpression::CountField.parse(str) || MiqExpression::Tag.parse(str)
end

def self.proto?
return @proto if defined?(@proto)
@proto = VMDB::Config.new("vmdb").config.fetch_path(:product, :proto)
Expand Down
6 changes: 5 additions & 1 deletion lib/miq_expression/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MiqExpression::Field
ParseError = Class.new(StandardError)

def self.parse(field)
match = FIELD_REGEX.match(field) or raise ParseError, field
(match = FIELD_REGEX.match(field)) || raise(ParseError, field)
new(match[:model_name].constantize, match[:associations].to_s.split("."), match[:virtual_custom_column] ||
match[:column])
end
Expand Down Expand Up @@ -95,6 +95,10 @@ def column_type
end
end

def report_column
(associations + [column]).join('.')
end

private

def custom_attribute_column_name
Expand Down
41 changes: 33 additions & 8 deletions lib/miq_expression/tag.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
class MiqExpression::Tag
def self.parse(tag)
klass, ns = tag.split(".")
ns = "/" + ns.split("-").join("/")
ns = ns.sub(/(\/user_tag\/)/, "/user/") # replace with correct namespace for user tags
new(klass.constantize, ns)
REGEX = /
(?<model_name>([[:alnum:]]*(::)?)+)
\.(?<associations>([a-z_]+\.)*)
(?<namespace>\bmanaged|user_tag\b)
-(?<column>[a-z]+[_[:alnum:]]+)
/x

def self.parse(field)
parsed_params = parse_params(field) || return
managed = parsed_params[:namespace] == self::MANAGED_NAMESPACE
new(parsed_params[:model_name], parsed_params[:associations], parsed_params[:column], managed)
end

attr_reader :model, :namespace
MANAGED_NAMESPACE = 'managed'.freeze
USER_NAMESPACE = 'user'.freeze

attr_reader :model, :namespace, :column

def initialize(model, namespace)
def initialize(model, _associations, column, managed = true)
@model = model
@namespace = namespace
@column = column
@base_namespace = managed ? MANAGED_NAMESPACE : USER_NAMESPACE
@namespace = "/#{@base_namespace}/#{column}"
end

def contains(value)
ids = model.find_tagged_with(:any => value, :ns => namespace).pluck(:id)
model.arel_attribute(:id).in(ids)
end

def report_column
"#{@base_namespace}.#{column}"
end

def self.parse_params(field)
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 || return
parsed_params[:associations] = parsed_params[:associations].to_s.split(".")
parsed_params
end
end
7 changes: 7 additions & 0 deletions spec/lib/miq_expression/field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@
end
end

describe '#report_column' do
it 'returns the correct format for a field' do
field = MiqExpression::Field.parse('Vm.miq_provision.miq_request-requester_name')
expect(field.report_column).to eq('miq_provision.miq_request.requester_name')
end
end

describe "#date?" do
it "returns false for fields of column type other than :date" do
field = described_class.new(Vm, [], "name")
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/miq_expression/tag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
RSpec.describe MiqExpression::Tag do
describe '#report_column' do
it 'returns the correct format for a tag' do
tag = MiqExpression::Tag.parse('Vm.managed-environment')
expect(tag.report_column).to eq('managed.environment')
end
end
end

0 comments on commit fee815e

Please sign in to comment.