Module: ActiveRecord::Acts::Rateable::ClassMethods

Defined in:
lib/acts_as_rateable.rb,
lib/acts_as_rateable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_rateable(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# 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 = {}) ⇒ Object

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.



29
30
31
32
33
34
# 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