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

[EUWE] correct field names in reports #15498

Merged
merged 2 commits into from
Jul 7, 2017
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
2 changes: 1 addition & 1 deletion app/controllers/report_controller/reports/editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def gfv_key_style(key, value)
f = @edit[:new][:field_order][f_idx] # Get the field element
field_sub_type = MiqExpression.get_col_info(f.last)[:format_sub_type]
field_data_type = MiqExpression.get_col_info(f.last)[:data_type]
field_name = f.last.include?(".") ? f.last.split(".").last.tr("-", ".") : f.last.split("-").last
field_name = MiqExpression.parse_field_or_tag(f.last).report_column
case parm
when "style" # New CSS class chosen
if value.blank?
Expand Down
2 changes: 1 addition & 1 deletion app/views/report/_form_styling.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
%tbody
- @edit[:new][:field_order].each_with_index do |f, f_idx|
- field_type = MiqExpression.get_col_info(f.last.split("__").first)[:format_sub_type]
- col_name = f.last.include?(".") ? f.last.split(".").last.gsub("-", ".") : f.last.split("-").last
- col_name = MiqExpression.parse_field_or_tag(f.last).report_column
- styles = @edit.fetch_path(:new, :col_options, col_name, :style) || []
%tr
%td
Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this to fix a rubocop complaint? I wish it wouldn't complain about that! If it's not directly related, I would leave this change out

(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