Module: Mongoid::TaggableWithContext::ClassMethods

Defined in:
lib/mongoid/taggable_with_context.rb

Instance Method Summary collapse

Instance Method Details

#format_tags_for(context, value) ⇒ Object

Helper method to convert a a tag input value of unknown type to a formatted array.

Raises:



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mongoid/taggable_with_context.rb', line 106

def format_tags_for(context, value)
  return nil if value.nil?
  # 0) Tags must be an array or a string
  raise InvalidTagsFormat unless value.is_a?(Array) || value.is_a?(String)
  # 1) convert String to Array
  value = value.split(get_tag_separator_for(context)) if value.is_a? String
  # 2) remove all nil values
  # 3) strip all white spaces. Could leave blank strings (e.g. foo, , bar, baz)
  # 4) remove all blank strings
  # 5) remove duplicate
  value.compact.map(&:strip).reject(&:blank?).uniq
end

#get_tag_group_by_field_for(context) ⇒ Object



83
84
85
# File 'lib/mongoid/taggable_with_context.rb', line 83

def get_tag_group_by_field_for(context)
  self.taggable_with_context_options[context][:group_by_field]
end

#get_tag_separator_for(context) ⇒ Object



79
80
81
# File 'lib/mongoid/taggable_with_context.rb', line 79

def get_tag_separator_for(context)
  self.taggable_with_context_options[context][:separator]
end

#tag_contextsObject



57
58
59
# File 'lib/mongoid/taggable_with_context.rb', line 57

def tag_contexts
  self.taggable_with_context_options.keys
end

#tag_database_fieldsObject



61
62
63
64
65
# File 'lib/mongoid/taggable_with_context.rb', line 61

def tag_database_fields
  self.taggable_with_context_options.keys.map do |context|
    tag_options_for(context)[:db_field]
  end
end

#tag_options_for(context) ⇒ Object



67
68
69
# File 'lib/mongoid/taggable_with_context.rb', line 67

def tag_options_for(context)
  self.taggable_with_context_options[context]
end

#taggable(*args) ⇒ Object

Macro to declare a document class as taggable, specify field name for tags, and set options for tagging behavior.

Examples:

Define a taggable document.


class Article
  include Mongoid::Document
  include Mongoid::TaggableWithContext
  taggable :keywords, separator: ' ', default: ['foobar']
end

Parameters:

  • field (Symbol)

    The name of the field for tags. Defaults to :tags if not specified.

  • options (Hash)

    Options for taggable behavior.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mongoid/taggable_with_context.rb', line 45

def taggable(*args)
  # init variables
  options = args.extract_options!
  field = args.present? ? args.shift.to_sym : DEFAULT_FIELD
  added = add_taggable(field, options)
  # TODO: test if this is needed
  # descendants.each do |subclass|
  #   subclass.add_taggable(field, options)
  # end
  added
end

#tagged_with(context, tags) ⇒ Criteria

Find documents tagged with all tags passed as a parameter, given as an Array or a String using the configured separator.

Examples:

Find matching all tags in an Array.

Article.tagged_with(['ruby', 'mongodb'])

Find matching all tags in a String.

Article.tagged_with('ruby, mongodb')

Parameters:

  • :field (String)

    The field name of the tag.

  • :tags (Array<String, Symbol>, String)

    Tags to match.

Returns:

  • (Criteria)

    A new criteria.



98
99
100
101
102
# File 'lib/mongoid/taggable_with_context.rb', line 98

def tagged_with(context, tags)
  tags = format_tags_for(context, tags)
  field = tag_options_for(context)[:field]
  all_in(field => tags)
end

#tags_for(context, group_by = nil, conditions = {}) ⇒ Object



71
72
73
# File 'lib/mongoid/taggable_with_context.rb', line 71

def tags_for(context, group_by=nil, conditions={})
  raise AggregationStrategyMissing
end

#tags_with_weight_for(context, group_by = nil, conditions = {}) ⇒ Object



75
76
77
# File 'lib/mongoid/taggable_with_context.rb', line 75

def tags_with_weight_for(context, group_by=nil, conditions={})
  raise AggregationStrategyMissing
end