Sha256: 5c5e8f8d2eb36b7b488075f5d2903d4bf92fea83b2b5d304c9ace65df659b3a1
Contents?: true
Size: 1.68 KB
Versions: 36
Compression:
Stored size: 1.68 KB
Contents
module Noodall module Tagging def self.configure(model) model.class_eval do key :tags, Array, :index => true end end module ClassMethods 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}) if tags_map.count > 0 tags = tags_map.find({}, query.options.to_hash ).to_a.collect{ |hash| Tag.new(hash['_id'], hash['value']) } tags_map.drop # clean up tmp collection tags else [] end end def tag_cloud_map "function(){" + "this.tags.forEach(" + "function(z){" + "emit( z , 1 );" + "}" + ")}" end def tag_cloud_reduce "function( key , values ){" + "var total = 0;" + "for ( var i=0; i<values.length; i++ ){" + "total += values[i];" + "}" + "return total;" + "}" end end module InstanceMethods def tag_list=(string) self.tags = string.to_s.split(',').map{ |t| t.strip.downcase }.reject(&:blank?).compact.uniq end def tag_list tags.join(', ') end def related(options ={}) self.class.all(options.merge({:_id => {'$ne' => self._id}, :tags => /(#{self.tags.join('|')})/i})) end end class Tag attr_reader :name, :count def initialize(name, count) @name = name @count = count.to_i end end end end
Version data entries
36 entries across 36 versions & 1 rubygems