Module: ActiveRecord::Acts::Rated
- Defined in:
- lib/acts_as_rated.rb
Overview
acts_as_rated
Adds rating capabilities to any ActiveRecord object. It has the ability to work with objects that have or don’t special fields to keep a tally of the ratings and number of votes for each object. In addition it will by default use the User model as the rater object and keep the ratings per-user. It can be configured to use another class, or not use a rater at all, just keeping a global rating
Special methods are provided to create the ratings table and if needed, to add the special fields needed to keep per-objects ratings fast for access to rated objects. Can be easily used in migrations.
Example of usage:
class Book < ActiveRecord::Base
acts_as_rated
end
bill = User.find_by_name 'bill'
jill = User.find_by_name 'jill'
catch22 = Book.find_by_title 'Catch 22'
hobbit = Book.find_by_title 'Hobbit'
catch22.rate 5, bill
hobbit.rate 3, bill
catch22.rate 1, jill
hobbit.rate 5, jill
hobbit. # => 4
hobbit.rated_total # => 8
hobbit.rated_count # => 2
hobbit.unrate bill
hobbit. # => 5
hobbit.rated_total # => 5
hobbit.rated_count # => 1
bks = Book. 5 # => [hobbit]
bks = Book. 1..5 # => [catch22, hobbit]
usr = Book.find_rated_by jill # => [catch22, hobbit]
Defined Under Namespace
Modules: ClassMethods, RateMethods Classes: RateError
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Class Method Details
.included(base) ⇒ Object
:nodoc:
48 49 50 |
# File 'lib/acts_as_rated.rb', line 48 def self.included(base) #:nodoc: base.extend(ClassMethods) end |