module ActiveRecord::Acts::Rateable::ClassMethods

Public Instance Methods

acts_as_rateable(options = {}) click to toggle source
# File lib/acts_as_rateable.rb, line 9
def acts_as_rateable(options = {})
  has_one :rating, :as => :rateable, :dependent => :destroy

  unless respond_to?(:max_rating)
    class_inheritable_accessor :max_rating
    attr_protected :max_rating
    self.max_rating = options[:max_rating] || 5
  end

  include ActiveRecord::Acts::Rateable::ClassMethods
  include ActiveRecord::Acts::Rateable::InstanceMethods
end
find_top_rated(params = {}) click to toggle source

Returns the top rated records, ordered by their average rating. You can use the usual finder parameters to narrow down the records to return. The finder limits the number of returned records to 20 by default. Specify :limit to change it.

# File lib/acts_as_rateable.rb, line 29
def find_top_rated(params = {})
  find_params = params.merge(:include => :rating)
  find_params[:order] = ['ratings.average_rating DESC', find_params.delete(:order)].compact.join(", ")
  find_params[:limit] = 20 unless find_params.key?(:limit)
  find(:all, find_params)
end