Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: keithepley/acts_as_votable
base: master
...
head fork: elado/acts_as_votable
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 1 commit
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Commits on Dec 26, 2012
@elado elado added cached_votes_score, which calculates upvotes - downvotes, for t…
…he ability to sort by good votes.
29c6934
View
3  README.markdown
@@ -213,15 +213,18 @@ 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
View
7 lib/acts_as_votable/votable.rb
@@ -144,6 +144,13 @@ def update_cached_votes
updates[:cached_votes_down] = count_votes_down(true)
end
+ if self.respond_to?(:cached_votes_score=)
+ updates[:cached_votes_score] = (
+ (updates[:cached_votes_up] || count_votes_up(true)) -
+ (updates[:cached_votes_down] || count_votes_down(true))
+ )
+ end
+
self.update_attributes(updates, :without_protection => true) if updates.size > 0
end
View
1  spec/spec_helper.rb
@@ -45,6 +45,7 @@
create_table :votable_caches do |t|
t.string :name
t.integer :cached_votes_total
+ t.integer :cached_votes_score
t.integer :cached_votes_up
t.integer :cached_votes_down
end
View
24 spec/votable_spec.rb
@@ -172,6 +172,30 @@
@votable_cache.cached_votes_total.should == 0
end
+ it "should update cached score votes if there is a score column" do
+ @votable_cache.cached_votes_score = 50
+ @votable_cache.vote :voter => @voter
+ @votable_cache.cached_votes_score.should == 1
+ @votable_cache.vote :voter => @voter2, :vote => 'false'
+ @votable_cache.cached_votes_score.should == 0
+ @votable_cache.vote :voter => @voter, :vote => 'false'
+ @votable_cache.cached_votes_score.should == -2
+ end
+
+ it "should update cached score votes when a vote up is removed" do
+ @votable_cache.vote :voter => @voter, :vote => 'true'
+ @votable_cache.cached_votes_score.should == 1
+ @votable_cache.unvote :voter => @voter
+ @votable_cache.cached_votes_score.should == 0
+ end
+
+ it "should update cached score votes when a vote down is removed" do
+ @votable_cache.vote :voter => @voter, :vote => 'false'
+ @votable_cache.cached_votes_score.should == -1
+ @votable_cache.unvote :voter => @voter
+ @votable_cache.cached_votes_score.should == 0
+ end
+
it "should update cached up votes if there is an up vote column" do
@votable_cache.cached_votes_up = 50
@votable_cache.vote :voter => @voter

No commit comments for this range

Something went wrong with that request. Please try again.