Sha256: 894d2c0f0ce63eeb74902ba6a1b4f9af9f2319567c8dd4d46224cbdb0a18a8d1

Contents?: true

Size: 1.64 KB

Versions: 5

Compression:

Stored size: 1.64 KB

Contents

module Noodall
  module Tagging
    extend ActiveSupport::Concern

    included do
      key :tags, Array, :index => true
    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, :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

      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

5 entries across 5 versions & 1 rubygems

Version Path
noodall-core-0.8.1 lib/noodall/tagging.rb
noodall-core-0.8.0 lib/noodall/tagging.rb
noodall-core-0.7.5 lib/noodall/tagging.rb
noodall-core-0.7.3 lib/noodall/tagging.rb
noodall-core-0.7.2 lib/noodall/tagging.rb