Provides fields and methods for the rating manipulation on Mongoid documents
<img src=“https://secure.travis-ci.org/proton/mongoid_rateable.png” />
Lastest version of Mongoid:Rateable requires mongoid 3. If you need a mongoid 2 support, look at mongoid_rateable 0.1.7.
Add to Gemfile:
gem 'mongoid_rateable'
Include the module in models where you want it:
class Post include Mongoid::Document include Mongoid::Rateable RATING_RANGE = (1..5) # Optional (if you need restriction) ... end class User include Mongoid::Document ... end
You can rate by passing an integer and a rater model to the “rate” method:
@post = Post.first @user = User.where(:name => 'Bill') # or more likely, current_user @post.rate 1, @user # I like this! @post.rate -1, @user # I don't like this! @post.rate 5, @user # I LOVE this! @post.rate -10, @user # Delete it from the Internet!
Rates have weight (1 by defaut)
@post.rate 5, @user, 3 # Rate @post with weight 3 (@user has high karma) @post.rate 3, @user, 1 # Rate @post with weight 1 (@user has low karma)
You can unrate by using “unrate” method:
@post.unrate @user
And don’t forget to save rateable object:
@post.save
Sure, you can rate and save in one fuction:
@post.rate_and_save(3, @user) @post.unrate_and_save(@user)
You’ll often want to know if a user already rated post. Simple:
@post.rated_by? @user # True if it rated by user
And if someone rated it:
@post.rated? # True if it rated by someone
You can get user mark:
@post.user_mark(@user) # Mark or nil (if not rated by user)
You can also get a tally of the number of rates cast:
@post.rate_count # Just one so far!
You can get a total weight of post rates:
@post.rate_weight # Just one so far!
And you can get the average rating:
@post.rating # rates / rate_weight
And you can get the average rating without weights (It calculates realtime, so it can be slow):
@post.unweighted_rating # rates without weights / rate_count
And you can get the previous rating and delta:
@post.previous_rating @post.rating_delta # rating - previous_rating
You can get rated or unrated posts:
Post.rated Post.unrated
You can get posts rated by someone:
Post.rated_by(@user)
You can get posts with some rating:
Post.with_rating(2..5) Post.with_rating(0..10) Post.with_rating(-2..2)
You can get most rated and highest rated posts: (Sorry, this method doesn’t work with embedded documents)
Post.highest_rated # 10 (or less) highest rated posts Post.highest_rated(5) # 5 (or less) highest rated posts
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
-
Fork the project
-
Start a feature/bugfix branch
-
Commit and push until you are happy with your contribution
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright © 2011 Peter Savichev (proton). See LICENSE.txt for further details.