Module: Mongoid::Taggable::ClassMethods

Defined in:
lib/mongoid/taggable.rb

Instance Method Summary collapse

Instance Method Details

#disable_tags_index!Object



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

def disable_tags_index!
  @do_tags_index = false
end

#enable_tags_index!Object



54
55
56
# File 'lib/mongoid/taggable.rb', line 54

def enable_tags_index!
  @do_tags_index = true
end

#save_tags_index!Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mongoid/taggable.rb', line 67

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

#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



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

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

#tags_index_collectionObject



63
64
65
# File 'lib/mongoid/taggable.rb', line 63

def tags_index_collection
  "#{collection_name}_tags_index"
end

#tags_separator(separator = nil) ⇒ Object



58
59
60
61
# File 'lib/mongoid/taggable.rb', line 58

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

#tags_with_weightObject

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



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

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