Module: Noodall::Tagging::ClassMethods
- Defined in:
- lib/noodall/tagging.rb
Instance Method Summary collapse
Instance Method Details
#tag_cloud(options = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/noodall/tagging.rb', line 10 def tag_cloud( = {}) return [] if self.count == 0 # Don't bother if there is nothing in this collection query = query(.reverse_merge( :order => 'value DESC' )) = collection.map_reduce(tag_cloud_map, tag_cloud_reduce, {:query => query.criteria.to_hash, :out => "#{self.collection_name}_tags" }) if .count > 0 # Strange scoping issue means tags needs to be defined outside of the find block = nil .find({}, query..merge(:timeout => false).to_hash) do |cursor| = cursor.map { |hash| Tag.new(hash['_id'], hash['value']) } end else [] end end |
#tag_cloud_map ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/noodall/tagging.rb', line 28 def tag_cloud_map "function(){" + "this.tags.forEach(" + "function(z){" + "emit( z , 1 );" + "}" + ")}" end |
#tag_cloud_reduce ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/noodall/tagging.rb', line 37 def tag_cloud_reduce "function( key , values ){" + "var total = 0;" + "for ( var i=0; i<values.length; i++ ){" + "total += values[i];" + "}" + "return total;" + "}" end |