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
# File 'lib/noodall/tagging.rb', line 10

def tag_cloud(options = {})
  return [] if self.count == 0 # Don't bother if there is nothing in this collection
  query = query(options.reverse_merge(
    :order => 'value DESC'
  ))
  tags_map = collection.map_reduce(tag_cloud_map, tag_cloud_reduce, {:query => query.criteria.to_hash, :out => "#{self.collection_name}_tags" })
  if tags_map.count > 0
    tags = tags_map.find({}, query.options.to_hash ).to_a.collect{ |hash| Tag.new(hash['_id'], hash['value']) }
    tags
  else
    []
  end
end

#tag_cloud_mapObject



24
25
26
27
28
29
30
31
# File 'lib/noodall/tagging.rb', line 24

def tag_cloud_map
 "function(){" +
  "this.tags.forEach(" +
  "function(z){" +
  "emit( z , 1 );" +
  "}" +
  ")}"
end

#tag_cloud_reduceObject



33
34
35
36
37
38
39
40
41
# File 'lib/noodall/tagging.rb', line 33

def tag_cloud_reduce
  "function( key , values ){" +
  "var total = 0;" +
  "for ( var i=0; i<values.length; i++ ){" +
  "total += values[i];" +
  "}" +
  "return total;" +
  "}"
end