public
Description: Ratings for your model.
Homepage: http://zachinglis.com
Clone URL: git://github.com/zachinglis/is_rateable.git
Click here to lend your support to: is_rateable and make a donation at www.pledgie.com !
Willian Molinari (author)
Tue Jun 09 13:13:15 -0700 2009
zachinglis (committer)
Tue Jun 09 13:28:33 -0700 2009
commit  a81f1a0119b39afbe42d432153b07ee5897a3668
tree    0eaaaf237bd551255cfead608cd11f850f081a06
parent  7c3fe0090cf8522334b3c35eb86a9fcccef150a9
name age message
file MIT-LICENSE Thu Jul 17 08:50:36 -0700 2008 initial commit [zachinglis]
file README.textile Loading commit data...
file Rakefile Thu Jul 17 08:50:36 -0700 2008 initial commit [zachinglis]
directory generators/
file init.rb Thu Jul 17 11:02:00 -0700 2008 Started work on the view helpers [zachinglis]
file install.rb Thu Jul 17 08:50:36 -0700 2008 initial commit [zachinglis]
directory lib/
directory tasks/ Thu Jul 17 08:50:36 -0700 2008 initial commit [zachinglis]
directory test/ Thu Jul 17 08:50:36 -0700 2008 initial commit [zachinglis]
file uninstall.rb Thu Jul 17 08:50:36 -0700 2008 initial commit [zachinglis]
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]

Options

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

Without Ajax
In your model.
<pre> is_rateable :upto => 5 # it defaults :upto => 5 </pre>

In your view.
<pre> render_rating(@book, :interactive_stars) render_rating(@book) # If you want to also see 1/5 Stars </pre>

If a user rates more than twice, it will delete their older rating and put up a new rating.

Add a method to your books_controller like so:
<pre> def rate @book = Book.find(params[:id]) unless @book.add_rating(params[:rating].to_i, :ip => request.remote_ip) flash[:notice] = "You have already voted on this book" end redirect_to @book end </pre>

Using Ajax
You can use the same code for your model
<pre> is_rateable :upto => 5 # it defaults :upto => 5 </pre>

On your view, you need to call a partial
<pre> #Using html.erb <div id="rating"> <%= render :partial => "books/rate", :locals => { :rateable => @book } %>

#Using haml #rating render :partial => “books/rate”, :locals => { :rateable => @book }

Your partial will have a spinner and a div to be replaced when ajax is completed
<pre> #Using html.erb (views/books/_rate.html.erb) <%= render_ajax_rating rateable %> <%= image_tag "icons/spinner.gif", :id => :spinner, :style => "display: none;" %> <div id="success_rating" style="display:none;"> Rated!

#Using haml (views/books/_rate.html.haml) = render_ajax_rating rateable = image_tag “icons/spinner.gif”, :id => :spinner, :style => “display: none;” #success_rating{:style => “display:none;”} Rated!

On your controller:
<pre> def rate @book = Book.find(params[:id]) @book.rate params[:rating].to_i render :update do |page| page.replace_html("rating", {:partial => "rate", :locals => {:rateable => @book}}) page.show "success_rating" end end </pre>

Todo

Credits

Copyright © 2008 Zach Inglis, released under the MIT license