-
Notifications
You must be signed in to change notification settings - Fork 896
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15498 from jntullo/euwe_correct_field_names
[EUWE] correct field names in reports
- Loading branch information
Showing
5 changed files
with
57 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |