Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Add up / down voteability to Mongoid and MongoMapper documents. Optimized for speed by using only ONE request to validate, update, and retrieve updated data.

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .watchr
Octocat-spinner-32 CHANGELOG.rdoc
Octocat-spinner-32 Gemfile
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 TODO
Octocat-spinner-32 voteable_mongoid.gemspec
README.rdoc

Voteable Mongoid

Voteable Mongoid allows you to make your Mongoid::Document objects voteable (up or down). For instance, in a forum, a user can vote up (or down) on a post or a comment.

Voteable Mongoid is built for speed. It uses only one database request per collection to do both data validation and data update.

Sample app at github.com/vinova/simple_qa

Installation

Rails 3.0.x

To install the gem, add this to your Gemfile

gem 'voteable_mongoid'

After that, remember to run “bundle install”

Usage

Making Post and Comment voteable, User being the voter

post.rb

class Post
  include Mongoid::Document
  include Mongoid::Voteable

  # set points for each vote
  voteable self, :up => +1, :down => -1

  references_many :comments
end

comment.rb

require 'post'

class Comment
  include Mongoid::Document
  include Mongoid::Voteable

  referenced_in :post

  voteable self, :up => +1, :down => -3

  # each vote on a comment can affect votes count and point of the related post as well
  voteable Post, :up => +2, :down => -1
end

user.rb

class User
  include Mongoid::Document
  include Mongoid::Voter
end

Making a vote

@user.vote(@post, :up)
# is equivalent to
@user.vote(:votee => @post, :value => :up)
@post.vote(:voter => @user, :value => :up)

# In case you don't need to init voter and / or votee objects you can    
@user.vote(:votee_type => 'Post', :votee_id => post_id, :value => :down)
@post.vote(:voter_id => user_id, :value => :up)
Post.vote(:voter_id => user_id, :votee_id => post_id, :value => :up)

Undo a vote

@user.unvote(@comment)

If have full information you do not need to init voter and votee object

Post.vote(:voter_id => user_id, :votee_id => post_id, :value => :up, :revote => true)
Post.vote(:voter_id => user_id, :votee_id => post_id, :value => :up, :unvote => true)

Getting vote_value

@user.vote_value(@post)
@user.vote_value(:class_type => 'Post', :votee_id => post_id)
@post.vote_value(@user)
@post.vote_value(user_id)

Check if voted?

@user.voted?(@post)
@user.voted?(:class_type => 'Post', :votee_id => post_id)
@post.voted_by?(@user)
@post.voted_by?(user_id)

Getting votes counts and points

puts @post.votes_point
puts @post.votes_count
puts @post.up_votes_count
puts @post.down_votes_count

Getting the list of voted objects of a class

Post.voted_by(@user)
Post.up_voted_by(@user)
Post.down_voted_by(@user)

Utilities

Re-generate counters and vote points in case you change :up / :down vote points

Rails

rake db:mongoid:voteable:remake_stats

Ruby

Mongoid::Voteable::Tasks.remake_stats

Set counters and point to 0 for uninitialized voteable objects in order sort and query

Rails

rake db:mongoid:voteable:init_stats

Ruby

Mongoid::Voteable::Tasks::init_stats

Migrate from version < 0.7.0

Rails

rake db:mongoid:voteable:migrate_old_votes

Ruby

Mongoid::Voteable::Tasks.migrate_old_votes

Credits

  • Alex N. - Author

  • Stefan N. - Unvoting

Copyright © 2010-2011 Vinova Pte Ltd (vinova.sg)

Licensed under the MIT license.

Something went wrong with that request. Please try again.