Star rating for Mongoid 4 - MongoidRating
Currenty this gem supports only Mongoid 4
Features
- Multiple rating fields per model
- Float rating marks (users can give 4.5 stars)
Installation
Add this line to your application's Gemfile:
gem 'mongoid_rating'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mongoid_rating
Usage
make model rateable:
class Post
include Mongoid::Document
rateable :rate
# needed if you use rails_admin so it would not complain
accepts_nested_attributes_for :rate_data
end
ps = Post.create()
user = User.create()
rate and unrate:
ps.rate 5, user
ps.unrate, user
Get current rating
ps.rate
=> 5.0
ps.rate_by(user)
=> 5
Check if user rated:
ps.rate_by?(user)
=> true
Scopes:
Post.highest_rate
Post.rate_in(2..5)
Post.rate_in(2..5).first
=> #<Post rate_count: 1, rate_sum: 5.0, rate_average: 5.0>
Post.rate_in(2..3).first
=> nil
Posts rated by user:
Post.rate_by(user).first
=> #<Post rate_count: 1, rate_sum: 5.0, rate_average: 5.0>
Credits
(c) 2013 glebtv, MIT license
Partially based on mongoid-rateable which is Copyright (c) 2011 Peter Savichev (proton), MIT license
Contributing
- 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