README.markdown in acts_as_votable-0.4.0 vs README.markdown in acts_as_votable-0.5.0

- old
+ new

@@ -2,21 +2,22 @@ Acts As Votable is a Ruby Gem specifically written for Rails/ActiveRecord models. The main goals of this gem are: - Allow any model to be voted on, like/dislike, upvote/downvote, etc. +- Allow any model to be voted under arbitrary scopes. - Allow any model to vote. In other words, votes do not have to come from a user, they can come from any model (such as a Group or Team). - Provide an easy to write/read syntax. ## Installation -### Rails 3 +### Rails 3+ Just add the following to your Gemfile. - gem 'acts_as_votable', '~> 0.4.0' + gem 'acts_as_votable', '~> 0.5.0' And follow that up with a ``bundle install``. ### Database Migrations @@ -103,17 +104,46 @@ @user.votes.up.votables You can also 'unvote' a model to remove a previous vote. @post.liked_by @user1 - @post.unlike_by @user1 + @post.unliked_by @user1 @post.disliked_by @user1 @post.undisliked_by @user1 Unvoting works for both positive and negative votes. +### Examples with scopes + +You can add an scope to your vote + + # positive votes + @post.liked_by @user1, :vote_scope => 'rank' + @post.vote :voter => @user3, :vote_scope => 'rank' + @post.vote :voter => @user5, :vote => 'like', :vote_scope => 'rank' + + # negative votes + @post.downvote_from @user2, :vote_scope => 'rank' + @post.vote :voter => @user2, :vote => 'bad', :vote_scope => 'rank' + + # tally them up! + @post.find_votes(:vote_scope => 'rank').size # => 5 + @post.likes(:vote_scope => 'rank').size # => 3 + @post.upvotes(:vote_scope => 'rank').size # => 3 + @post.dislikes(:vote_scope => 'rank').size # => 2 + @post.downvotes(:vote_scope => 'rank').size # => 2 + + # votable model can be voted under different scopes + # by the same user + @post.vote :voter => @user1, :vote_scope => 'week' + @post.vote :voter => @user1, :vote_scope => 'month' + + @post.votes.size # => 2 + @post.find_votes(:vote_scope => 'week').size # => 1 + @post.find_votes(:vote_scope => 'month').size # => 1 + ### The Voter You can have your voters ``acts_as_voter`` to provide some reserve functionality. class User < ActiveRecord::Base @@ -154,10 +184,12 @@ @user.voted_up_on? @comment2 # => false @user.voted_up_on? @comment3 # => false @user.voted_down_on? @comment3 # => false +Aliases for methods ``voted_up_on?`` and ``voted_down_on?`` are: ``voted_up_for?``, ``voted_down_for?``, ``liked?`` and ``disliked?``. + Also, you 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 @user.find_voted_items @@ -211,18 +243,21 @@ to speed up @post we would use the following migration: class AddCachedVotesToPosts < ActiveRecord::Migration def self.up add_column :posts, :cached_votes_total, :integer, :default => 0 + add_column :posts, :cached_votes_score, :integer, :default => 0 add_column :posts, :cached_votes_up, :integer, :default => 0 add_column :posts, :cached_votes_down, :integer, :default => 0 add_index :posts, :cached_votes_total + add_index :posts, :cached_votes_score add_index :posts, :cached_votes_up add_index :posts, :cached_votes_down end def self.down remove_column :posts, :cached_votes_total + remove_column :posts, :cached_votes_score remove_column :posts, :cached_votes_up remove_column :posts, :cached_votes_down end end