README.markdown in acts_as_votable-0.7.1 vs README.markdown in acts_as_votable-0.8.0

- old
+ new

@@ -156,11 +156,32 @@ @post.votes.size # => 2 @post.find_votes(:vote_scope => 'week').size # => 1 @post.find_votes(:vote_scope => 'month').size # => 1 ``` +### Adding weights to your votes +You can add weight to your vote. The default value is 1. + +```ruby +# positive votes +@post.liked_by @user1, :vote_weight => 1 +@post.vote :voter => @user3, :vote_weight => 2 +@post.vote :voter => @user5, :vote => 'like', :vote_scope => 'rank', :vote_weight => 3 + +# negative votes +@post.downvote_from @user2, :vote_scope => 'rank', :vote_weight => 1 +@post.vote :voter => @user2, :vote => 'bad', :vote_scope => 'rank', :vote_weight => 3 + +# tally them up! +@post.find_votes(:vote_scope => 'rank').sum(:vote_weight) # => 6 +@post.likes(:vote_scope => 'rank').sum(:vote_weight) # => 6 +@post.upvotes(:vote_scope => 'rank').sum(:vote_weight) # => 6 +@post.dislikes(:vote_scope => 'rank').sum(:vote_weight) # => 4 +@post.downvotes(:vote_scope => 'rank').sum(:vote_weight) # => 4 +``` + ### The Voter You can have your voters `acts_as_voter` to provide some reserve functionality. ```ruby @@ -265,10 +286,17 @@ @hat.votes.size # => 1 @hat.positives.size # => 0 @hat.negatives.size # => 1 ``` +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. + +```ruby +@hat.vote voter: @user, :duplicate => true +``` + ## Caching To speed up perform you can add cache columns to your votable model's table. These columns will automatically be updated after each vote. For example, if we wanted to speed up @post we would use the following migration: @@ -278,20 +306,26 @@ 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_column :posts, :cached_weighted_score, :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 + add_index :posts, :cached_weighted_score + + # Uncomment this line to force caching of existing votes + # Post.find_each(&:update_cached_votes) 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 + remove_column :posts, :cached_weighted_score end end ``` ## Testing