Module: Mongoid::Taggable::ClassMethods
- Defined in:
- lib/mongoid/taggable.rb
Instance Method Summary collapse
-
#aggregate_tags ⇒ Object
Execute map/reduce operation to aggregate tag counts for document class.
-
#taggable(*args) ⇒ Object
Macro to declare a document class as taggable, specify field name for tags, and set options for tagging behavior.
-
#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.
-
#tags ⇒ Object
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.
-
#tags_aggregation_collection ⇒ Object
Collection name for storing results of tag count aggregation.
-
#tags_with_weight ⇒ Object
retrieve the list of tags with weight(count), this is useful for creating tag clouds.
Instance Method Details
#aggregate_tags ⇒ Object
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 return unless tag_aggregation map = "function() { if (!this.#{}) { return; } for (index in this.#{}) { emit(this.#{}[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 => ) end |
#taggable(*args) ⇒ Object
Macro to declare a document class as taggable, specify field name for tags, and set options for tagging 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) = args. .reverse_merge!( :separator => ',', :aggregation => false ) write_inheritable_attribute(:tags_field, args.blank? ? :tags : args.shift) self. = [:separator] self.tag_aggregation = [:aggregation] field , :type => Array index define_tag_field_accessors() 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.
90 91 92 93 |
# File 'lib/mongoid/taggable.rb', line 90 def tagged_with() = () if .is_a? String criteria.all_in( => ) end |
#tags ⇒ Object
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 db.collection().find.to_a.map{ |r| r["_id"] } end |
#tags_aggregation_collection ⇒ Object
Collection name for storing results of tag count aggregation
96 97 98 |
# File 'lib/mongoid/taggable.rb', line 96 def @tags_aggregation_collection ||= "#{collection_name}_tags_aggregation" end |
#tags_with_weight ⇒ Object
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 db.collection().find.to_a.map{ |r| [r["_id"], r["value"]] } end |