Skip to content

Commit

Permalink
Set namespace by using constants
Browse files Browse the repository at this point in the history
there are only 2 possible namespaces.
  • Loading branch information
lpichler committed Feb 10, 2017
1 parent b61a0e7 commit b57d656
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/miq_expression/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ class MiqExpression::Tag < MiqExpression::Field
-(?<column>[a-z]+[_[:alnum:]]+)
/x

MANAGED_NAMESPACE = 'managed'.freeze
USER_NAMESPACE = 'user'.freeze

attr_reader :namespace

def self.parse(field)
match = TAG_REGEX.match(field) || return

associations = match[:associations].split(".")
namespace = match[:namespace] == 'user_tag' ? 'user' : match[:namespace]
new(match[:model_name].classify.safe_constantize, associations, "/#{namespace}/#{match[:column]}")
model = match[:model_name].classify.safe_constantize
new(model, associations, match[:column], match[:namespace] == MANAGED_NAMESPACE)
end

def initialize(model, associations, namespace)
super(model, associations, namespace.split("/").last)
@namespace = namespace
def initialize(model, associations, column, managed = true)
super(model, associations, column)
@namespace = "/#{managed ? MANAGED_NAMESPACE : USER_NAMESPACE}/#{column}"
end

def contains(value)
Expand Down

0 comments on commit b57d656

Please sign in to comment.