You are here: Browse Railsplugins Rateableplugin
To install, simply run:
script/plugin install svn://rubyforge.org/var/svn/rateableplugin/trunk
You’ll then need to make the ratings table. You should probably make a migration to do so, which you can do with the following command:
script/generate migration add_ratings
Then edit the resulting migration source that it puts in db/migrate (it will be named something like 015_add_ratings.rb, the number at the beginning of the filename will probably be different in your instance).
The table needed by the plugin needs to be named “ratings” and have three columns in addition to the primary key.
They are:
rating integer rateable_id integer rateable_type string
You can do this in your migration with the following code:
def self.up create_table :ratings do |t| t.column :rating, :integer # You can add a default value here if you wish t.column :rateable_id :integer, :null => false t.column :rateable_type, :string, :null => false end add_index :ratings, [:rateable_id, :rating] # Not required, but should help more than it hurts end
def self.down drop_table :ratings end
After saving the migration run the following from the top-level directory of your rails application: $ rake migrate
After that, you should have everything you need setup and ready to go.
It probably wouldn’t hurt to run through the included unit tests quickly, but you don’t have to:
$ rake test:plugins
Pretty simple, really. You just need to add “acts_as_rateable” at the top of your ActiveRecord model, and everything should magically become available to you :)
Any model that acts_as_rateable has the following methods available to it: (see also the rdoc for FortiusOne::Acts::Rateable::ClassMethods#acts_as_rateable)
rate(a_rating) : Rate the object with a_rating (integer)
rating= : Alias for rate
rating : Return the object's rating
find_all_by_rating : Find all objects matching the rating criteria
find_by_rating : Find the first object matching the rating criteria
You can specify ratings as single integers, a list of integers, a range (setting one end of
the range to "-1" will match all lower or higher values, depending on which end of the range
it's on), or a mix of ranges and integers in a list (see examples below)
class SillyWalk < ActiveRecord::Base
# attributes: name, inventor
acts_as_rateable
end
SillyWalk.new(:name => “Not Very Silly”, :inventor => “Mr. Pudey”).save
SillyWalk.new(:name => “A Bit Silly”, :inventor => “Mrs. Two-Lumps”, :rating => 3).save
SillyWalk.new(:name => “Quite Silly”, :inventor => “Mr. Teabag”, :rating => 5).save
NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly