Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Ratings for your model.
Ruby
tree: 45147d99a4

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
generators/rateable
lib
tasks
test
MIT-LICENSE
README.textile
Rakefile
init.rb
install.rb
uninstall.rb

README.textile

is_rateable

Currently star buttons are a put request, not a post request like they should be due to time constraints. Should be fixed in a future version.

Patches welcome! I do test my own stuff but until I find a nice and easy way to include rspec in plugins, they won’t come with the specs.

Usage

To install, type script/generate rateable [--by_user] [--with-categories]

Options

—by-user will associate your ratings to a user model. Hopefully you’ve got one already.

—with-categories will add multiple rating categories. Specify them in your model as shown.

In your model.

<pre> is_rateable :upto => 5 # it defaults :upto => 5

  1. is_rateable :categories => [:writing, :artwork]

In your view.

<pre> render_rating(@book, :type => :interactive_stars) render_rating(@book) # If you want to also see 1/5 Stars </pre>

If a user rates more than twice (or twice in the same category), it will replace their older rating with a new rating.

Add a method to your books_controller like so:

<pre> def rate @book = Book.find(params[:id]) unless @book.rate(params[:rating].to_i, :category => params[:category], :ip => request.remote_ip) flash[:notice] = "You have already voted on this book" end redirect_to @book end </pre>

If you’re using multiple categories (writing, artwork, editing) you can supply the :category option to the render_rating view helper.
This will display the category rating of the model. Then calling render_rating without a :category option will display the average rating
across all categories.

<pre> render_rating(@book, :type => :interactive_stars, :category => :writing) render_rating(@book, :type => :interactive_stars, :category => :artwork) render_rating(@book) # Overall rating is average of two category ratings </pre>

If you want to see ratings for a specific user rather than global averages, you can also specify the :user option and pass it the
user for whom you wish to obtain ratings.

<pre> render_rating(@book, :type => :interactive_stars, :user => user) </pre>

Credits

Copyright © 2008 Zach Inglis, released under the MIT license

Something went wrong with that request. Please try again.