Module: Mongoid::Taggable::ClassMethods
- Defined in:
- lib/mongoid/taggable.rb
Instance Method Summary collapse
- #disable_tags_index! ⇒ Object
- #enable_tags_index! ⇒ Object
- #save_tags_index! ⇒ Object
-
#tagged_with(tag) ⇒ Object
returns an array of distinct ordered list of tags defined in all documents.
- #tagged_with_all(*tags) ⇒ Object
- #tagged_with_any(*tags) ⇒ Object
- #tags ⇒ Object
- #tags_index_collection ⇒ Object
- #tags_separator(separator = nil) ⇒ Object
-
#tags_with_weight ⇒ Object
retrieve the list of tags with weight (i.e. count), this is useful for creating tag clouds.
Instance Method Details
#disable_tags_index! ⇒ Object
61 62 63 |
# File 'lib/mongoid/taggable.rb', line 61 def @do_tags_index = false end |
#enable_tags_index! ⇒ Object
65 66 67 |
# File 'lib/mongoid/taggable.rb', line 65 def @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 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 => ) 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(*) self.all_in(:tags_array => .flatten) end |
#tagged_with_any(*tags) ⇒ Object
45 46 47 |
# File 'lib/mongoid/taggable.rb', line 45 def tagged_with_any(*) self.any_in(:tags_array => .flatten) end |
#tags ⇒ Object
49 50 51 52 |
# File 'lib/mongoid/taggable.rb', line 49 def db = Mongoid::Config.master db.collection().find.to_a.map{ |r| r["_id"] } end |
#tags_index_collection ⇒ Object
74 75 76 |
# File 'lib/mongoid/taggable.rb', line 74 def "#{collection_name}_tags_index" end |
#tags_separator(separator = nil) ⇒ Object
69 70 71 72 |
# File 'lib/mongoid/taggable.rb', line 69 def (separator = nil) @tags_separator = separator if separator @tags_separator || ',' end |
#tags_with_weight ⇒ Object
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 db = Mongoid::Config.master db.collection().find.to_a.map{ |r| [r["_id"], r["value"]] } end |