is_rateable
is_rateable copied to clipboard
Ratings for your model.
h1. 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.
h2. Usage
To install, type script/generate rateable [--by_user]
h3. Options
--by-user will associate your ratings to a user model. Hopefully you've got one already.
h4. Without Ajax
In your model.
is_rateable :upto => 5 # it defaults :upto => 5
In your view.
render_rating(@book, :interactive_stars) render_rating(@book) # If you want to also see 1/5 Stars
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:
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
h4. Using Ajax
You can use the same code for your model
is_rateable :upto => 5 # it defaults :upto => 5
On your view, you need to call a partial
#Using html.erb"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
#Using html.erb (views/books/_rate.html.erb) :spinner, :style => "display: none;" %>#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:
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
h2. Todo
- Annotate methods
h2. Credits
- "Zach Inglis":http://zachinglis.com
- "Nathan Arnold":http://shorepound.net
- "Les Hill":http://blog.leshill.org
- "Matt Remsik":http://veez.us
- "Tim Pope":http://www.tpope.net
- "Rogie King":http://komodomedia.com - CSS for Stars
- Some code from restful_authentication by "Rick Olson":http://activereload.com
h6. Ajax Rating
- "Willian Molinari":http://pothix.com/blog
- "Mateus Linhares":http://github.com/mateuslinhares
Copyright (c) 2008 Zach Inglis, released under the MIT license