Star rating gem for Rails application using jQuery plugin jRating
Demo application, requires to sign up before rating
Add this line to your application's Gemfile:
gem 'seems_rateable'
And then execute:
$ bundle
Or install it yourself as:
$ gem install seems_rateable
$ rails g seems_rateable User
Generator takes one argument which is the name of an existing user model e.g User, Client, Player ...
The generator creates necessary model, controller and asset files. It also creates route and migration files that are already being migrated
Include Javascript files adding these lines to application.js
#application.js
//= require rateable/jRating.jquery
//= require rateable/rateable.jquery
Include CSS file adding <%= seems_rateable_style %>
to your layaut head tag
Also make sure you have an existing current_user
object. If not, add something like this to your application controller
#application_controller.rb
helper_method :current_user
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
To prepare model be rateable add seems_rateable
to your model file. You can also pass a hash of options to
customize the functionality
:dimensions
Array of dimensions e.g.:dimensions => [:quality, :price, :performance]
:allow_update
Allowing user to re-rate his own ratings, default set to false e.g:allow_update=> true
class Computer < ActiveRecord::Base
seems_rateable :dimensions => [:quality, :price, :performance], :allow_update => true
end
Each object of the model now gain these methods :
@computer.rates_without_dimension
returns array of ratings given to the object@computer.raters_without_dimension
returns array of users that rated the object@computer.rate_average_without_dimension
returns cached database object containing average rate and quantity of given ratings of the object
Depending on the passed dimensions your object also gain these methods :
@computer.dimension_rates
e.g@computer.quality_rates
@computer.dimension_raters
e.g@computer.price_raters
@computer.dimension_average
e.g@computer.performance_average
To track user's given ratings add seems_rateable_rater
to your user model.
Now you can access user's ratings by @user.ratings_given
To display star rating use helper method rating_for
in your view
#index.html.erb
rating_for @computer
rating_for @computer, :dimension => :price, :id => "storage"
rating_for @computer, :static => true
You can specify these options :
:dimension
The dimension of the object:static
Set to true to display static star rating, default false:id
ID of the div e.g:id => "info"
, default nil
To edit the javascript options locate rateable.jquery.js file in /vendor/assets/javascripts/rateable/. The javascript options are explained directly in the file
To remove gem files simply execute rails g seems_rateable_destroy
in terminal.
Neither migration files nor database tables will be destroyed.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request