Module: Mongoid::Taggable::ClassMethods

Defined in:
lib/mongoid/taggable.rb

Instance Method Summary collapse

Instance Method Details

#disable_tags_index!Object



61
62
63
# File 'lib/mongoid/taggable.rb', line 61

def disable_tags_index!
  @do_tags_index = false
end

#enable_tags_index!Object



65
66
67
# File 'lib/mongoid/taggable.rb', line 65

def enable_tags_index!
  @do_tags_index = true
end

#save_tags_index!Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/mongoid/taggable.rb', line 78

def save_tags_index!
  return unless @do_tags_index

  db = Mongoid::Config.master
  coll = db.collection(collection_name)

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

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

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

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

    return count;
  }"

  coll.map_reduce(map, reduce, :out => tags_index_collection)
end

#tagged_with(tag) ⇒ Object

returns an array of distinct ordered list of tags defined in all documents



37
38
39
# File 'lib/mongoid/taggable.rb', line 37

def tagged_with(tag)
  self.any_in(:tags_array => [tag])
end

#tagged_with_all(*tags) ⇒ Object



41
42
43
# File 'lib/mongoid/taggable.rb', line 41

def tagged_with_all(*tags)
  self.all_in(:tags_array => tags.flatten)
end

#tagged_with_any(*tags) ⇒ Object



45
46
47
# File 'lib/mongoid/taggable.rb', line 45

def tagged_with_any(*tags)
  self.any_in(:tags_array => tags.flatten)
end

#tagsObject



49
50
51
52
# File 'lib/mongoid/taggable.rb', line 49

def tags
  db = Mongoid::Config.master
  db.collection(tags_index_collection).find.to_a.map{ |r| r["_id"] }
end

#tags_index_collectionObject



74
75
76
# File 'lib/mongoid/taggable.rb', line 74

def tags_index_collection
  "#{collection_name}_tags_index"
end

#tags_separator(separator = nil) ⇒ Object



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

def tags_separator(separator = nil)
  @tags_separator = separator if separator
  @tags_separator || ','
end

#tags_with_weightObject

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



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

def tags_with_weight
  db = Mongoid::Config.master
  db.collection(tags_index_collection).find.to_a.map{ |r| [r["_id"], r["value"]] }
end