Module: Mongoid::Taggable::ClassMethods

Defined in:
lib/mongoid/taggable.rb

Instance Method Summary collapse

Instance Method Details

#aggregate_tagsObject

Execute map/reduce operation to aggregate tag counts for document class



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/mongoid/taggable.rb', line 102

def aggregate_tags
  return unless tag_aggregation

  map = "function() {
    if (!this.#{tags_field}) {
      return;
    }

    for (index in this.#{tags_field}) {
      emit(this.#{tags_field}[index], 1);
    }
  }"

  reduce = "function(previous, current) {
    var count = 0;

    for (index in current) {
      count += current[index]
    }

    return count;
  }"

  collection.master.map_reduce(map, reduce, :out => tags_aggregation_collection)
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::Taggable
  taggable :keywords, :separator => ' ', :aggregation => true
end

Parameters:

  • field (Symbol)

    The name of the field for tags.

  • options (Hash)

    Options for taggable behavior.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mongoid/taggable.rb', line 50

def taggable(*args)
  options = args.extract_options!
  options.reverse_merge!(
    :separator => ',',
    :aggregation => false
  )

  write_inheritable_attribute(:tags_field, args.blank? ? :tags : args.shift)
  self.tags_separator  = options[:separator]
  self.tag_aggregation = options[:aggregation]

  field tags_field, :type => Array
  index tags_field

  define_tag_field_accessors(tags_field)
end

#tagged_with(_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:

  • _tags (Array<String, Symbol>, String)

    Tags to match.

Returns:

  • (Criteria)

    A new criteria.



90
91
92
93
# File 'lib/mongoid/taggable.rb', line 90

def tagged_with(_tags)
  _tags = convert_string_tags_to_array(_tags) if _tags.is_a? String
  criteria.all_in(tags_field => _tags)
end

#tagsObject

get an array with all defined tags for this model, this list returns an array of distinct ordered list of tags defined in all documents of this model



70
71
72
# File 'lib/mongoid/taggable.rb', line 70

def tags
  db.collection(tags_aggregation_collection).find.to_a.map{ |r| r["_id"] }
end

#tags_aggregation_collectionObject

Collection name for storing results of tag count aggregation



96
97
98
# File 'lib/mongoid/taggable.rb', line 96

def tags_aggregation_collection
  @tags_aggregation_collection ||= "#{collection_name}_tags_aggregation"
end

#tags_with_weightObject

retrieve the list of tags with weight(count), this is useful for creating tag clouds



76
77
78
# File 'lib/mongoid/taggable.rb', line 76

def tags_with_weight
  db.collection(tags_aggregation_collection).find.to_a.map{ |r| [r["_id"], r["value"]] }
end