Toll free (US) : +1 (888) 838-3532 | NYC: +1 (646) 491-6866

Acts_As_Votable is ruby gem specifically written for Rails/ActiveRecord models and This gem allows any model to be voted on upvote/downvote like/dislike, etc.
It allows any model to be voted under arbitrary scopes using this gem we can vote any model. votes do not have to come from a user, they can come from any model (such as a Group or Team) and it provide an easy to write/read syntax.

Gem Installation

gem ‘acts_as_votable’

Add above line in Gemfile and run bundle install

Supported ruby and rails versions

Ruby 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1.0
Rails 3.0, 3.1, 3.2, 4.0, 4.1+

This Gem uses vote table to save all voting information. To generate vote migration run below commands

rails generate acts_as_votable:migration
rake db:migrate

To rate any model just use “acts_as_votable” in model

Example:

class Article < ActiveRecord::Base
acts_as_votable
end

@article = Article.new(:name => ‘my new article’)
@article.save

@article.liked_by @user
@article.votes_for.size # => 1

Below are the some voting examples, All of these calls are valid and acceptable

@article.liked_by @user1
@article.downvote_from @user2
@article.vote_by :voter => @user3
@article.vote_by :voter => @user4, :vote => ‘like’
@article.vote_by :voter => @user5, :vote => ‘Dislike’

By default all votes are positive, so @user3 has cast a ‘good’ vote for @article.

@user1, @user3, and @user4 all voted in favor of @article.

@user2 and @user5 on the other had has voted against @article.

Any word works for casting a vote in favor or against post like Positive/Negative, Up/Down, Like/Dislike.. etc, Boolean flags true and false are also applicable.

Examples with scopes:

Using this gem we can add a scope to our vote

# positive/like votes
@article.liked_by @user1, :vote_scope => ‘rank’
@article.vote_by :voter => @user3, :vote_scope => ‘rank’
@article.vote_by :voter => @user5, :vote => ‘like’, :vote_scope => ‘rank’

# negative/Dislike votes
@article.downvote_from @user2, :vote_scope => ‘rank’
@article.vote_by :voter => @user2, :vote => ‘Dislike’, :vote_scope => ‘rank’

# tally them up!
@article.find_votes_for(:vote_scope => ‘rank’).size # => 5
@article.get_likes(:vote_scope => ‘rank’).size # => 3
@article.get_upvotes(:vote_scope => ‘rank’).size # => 3
@article.get_dislikes(:vote_scope => ‘rank’).size # => 2
@article.get_downvotes(:vote_scope => ‘rank’).size # => 2

# votable model can be voted under different scopes by the same user
@article.vote_by :voter => @user1, :vote_scope => ‘week’
@article.vote_by :voter => @user1, :vote_scope => ‘month’

@article.votes_for.size # => 2
@article.find_votes_for(:vote_scope => ‘week’).size # => 1
@article.find_votes_for(:vote_scope => ‘month’).size # => 1

Adding weights to our votes

we can add weight to our vote. The default value is 1.

# positive/like votes
@article.liked_by @user1, :vote_weight => 1
@article.vote_by :voter => @user3, :vote_weight => 2
@article.vote_by :voter => @user5, :vote => ‘like’, :vote_scope => ‘rank’, :vote_weight => 3

# negative/Dislike votes
@article.downvote_from @user2, :vote_scope => ‘rank’, :vote_weight => 1
@article.vote_by :voter => @user2, :vote => ‘Dislike’, :vote_scope => ‘rank’, :vote_weight => 3

# tally them up!
@article.find_votes_for(:vote_scope => ‘rank’).sum(:vote_weight) # => 6
@article.get_likes(:vote_scope => ‘rank’).sum(:vote_weight) # => 6
@article.get_upvotes(:vote_scope => ‘rank’).sum(:vote_weight) # => 6
@article.get_dislikes(:vote_scope => ‘rank’).sum(:vote_weight) # => 4
@article.get_downvotes(:vote_scope => ‘rank’).sum(:vote_weight) # => 4

The Voter

we can have our voters acts_as_voter to provide some reserve functionality.

For Example

class User < ActiveRecord::Base
acts_as_voter
end

@user.likes @article

@article.votes.size # => 1
@article.likes.size # => 1
@article.dislikes.size # => 0

To check if a voter has voted on a model, we can use voted_for?. we can check how the voter voted by using voted_as_when_voted_for, we can also check whether the voter has voted up or down.

Aliases for methods voted_up_on? is voted_up_for? , liked? and voted_down_on? is voted_down_for?, disliked? also we can obtain a list of all the objects a user has voted for. This returns the actual objects instead of instances of the Vote model. All objects are eager loaded

Registered Votes:

Voters can only vote once per model. In this example the 2nd vote does not count because @user has already voted for @post.

@user.likes @post
@user.likes @post

@post.votes # => 1
@post.likes # => 1

To check if a vote counted, or registered, use vote_registered? on our model after voting. For example:

@product.liked_by @user
@product.vote_registered? # => true

@product.liked_by => @user
@product.vote_registered? # => false, because @user has already voted this way

@product.disliked_by @user
@product.vote_registered? # => true, because user changed their vote

To check if a vote registered or counted, use vote_registered? on our model after voting. To permit duplicates entries of a same voter, use option duplicate. Also notice that this will limit some other methods that didn’t deal with multiples votes, in this case, the last vote will be considered.

@post.vote_by voter: @user, :duplicate => true

Caching:

To speed up perform we can add cache columns to our votable model’s table. These columns will automatically be updated after each vote.

Contact Us or comment below to know more about us.

footer-icon

Pin It on Pinterest

Share This