From 0f977e10629641da5ca858a5cd650537e22e05dd Mon Sep 17 00:00:00 2001 From: Jillian Tullo Date: Mon, 3 Jul 2017 09:25:16 -0400 Subject: [PATCH 1/2] correct field names in reports --- lib/miq_expression/field.rb | 6 +++- lib/miq_expression/tag.rb | 41 +++++++++++++++++++++------ spec/lib/miq_expression/field_spec.rb | 7 +++++ spec/lib/miq_expression/tag_spec.rb | 8 ++++++ 4 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 spec/lib/miq_expression/tag_spec.rb diff --git a/lib/miq_expression/field.rb b/lib/miq_expression/field.rb index 084dc0ed191..1739b12f18f 100644 --- a/lib/miq_expression/field.rb +++ b/lib/miq_expression/field.rb @@ -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 @@ -95,6 +95,10 @@ def column_type end end + def report_column + (associations + [column]).join('.') + end + private def custom_attribute_column_name diff --git a/lib/miq_expression/tag.rb b/lib/miq_expression/tag.rb index 2a1efa0d211..8328a1fcdb1 100644 --- a/lib/miq_expression/tag.rb +++ b/lib/miq_expression/tag.rb @@ -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 = / +(?([[:alnum:]]*(::)?)+) +\.(?([a-z_]+\.)*) +(?\bmanaged|user_tag\b) +-(?[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 diff --git a/spec/lib/miq_expression/field_spec.rb b/spec/lib/miq_expression/field_spec.rb index 1b55f9b4f9c..3345448d3d1 100644 --- a/spec/lib/miq_expression/field_spec.rb +++ b/spec/lib/miq_expression/field_spec.rb @@ -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") diff --git a/spec/lib/miq_expression/tag_spec.rb b/spec/lib/miq_expression/tag_spec.rb new file mode 100644 index 00000000000..22f1f2ab67b --- /dev/null +++ b/spec/lib/miq_expression/tag_spec.rb @@ -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 From ced36f06cbdbc9a53bde6cbc104852d1a61f3284 Mon Sep 17 00:00:00 2001 From: Jillian Tullo Date: Mon, 3 Jul 2017 11:49:08 -0400 Subject: [PATCH 2/2] ui fixes --- app/controllers/report_controller/reports/editor.rb | 2 +- app/views/report/_form_styling.html.haml | 2 +- lib/miq_expression.rb | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/controllers/report_controller/reports/editor.rb b/app/controllers/report_controller/reports/editor.rb index 208b431721f..fcca8e1ac41 100644 --- a/app/controllers/report_controller/reports/editor.rb +++ b/app/controllers/report_controller/reports/editor.rb @@ -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? diff --git a/app/views/report/_form_styling.html.haml b/app/views/report/_form_styling.html.haml index dc9614a28f3..7de1c3ca3d8 100644 --- a/app/views/report/_form_styling.html.haml +++ b/app/views/report/_form_styling.html.haml @@ -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 diff --git a/lib/miq_expression.rb b/lib/miq_expression.rb index 0712d957e86..4fee1265940 100644 --- a/lib/miq_expression.rb +++ b/lib/miq_expression.rb @@ -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)