Skip to content
Browse files

Merge branch 'jerefrer/master'

  • Loading branch information...
2 parents ea3a469 + 091281e commit 3fce588aca00b14101ffd289027758a90f265dc3 @bouchard committed Sep 19, 2012
Showing with 25 additions and 1 deletion.
  1. +1 −1 lib/acts_as_voteable.rb
  2. +11 −0 test/test_helper.rb
  3. +13 −0 test/thumbs_up_test.rb
View
2 lib/acts_as_voteable.rb
@@ -24,7 +24,7 @@ module SingletonMethods
# You can also have the upvotes and downvotes returned separately in the same query:
# Post.plusminus_tally(:separate_updown => true)
def plusminus_tally(params = {})
- t = self.joins("LEFT OUTER JOIN #{Vote.table_name} ON #{self.table_name}.id = #{Vote.table_name}.voteable_id")
+ t = self.joins("LEFT OUTER JOIN #{Vote.table_name} ON #{self.table_name}.id = #{Vote.table_name}.voteable_id AND #{Vote.table_name}.voteable_type = '#{self.name}'")
t = t.order("plusminus_tally DESC")
t = t.group("#{self.table_name}.id")
t = t.select("#{self.table_name}.*")
View
11 test/test_helper.rb
@@ -75,6 +75,12 @@
t.string :name
t.string :description
end
+
+ create_table :other_items, :force => true do |t|
+ t.integer :user_id
+ t.string :name
+ t.string :description
+ end
end
require 'thumbs_up'
@@ -100,6 +106,11 @@ class Item < ActiveRecord::Base
belongs_to :user
end
+class OtherItem < ActiveRecord::Base
+ acts_as_voteable
+ belongs_to :user
+end
+
class User < ActiveRecord::Base
acts_as_voter
has_many :items
View
13 test/thumbs_up_test.rb
@@ -354,4 +354,17 @@ def test_karma
assert_equal 4, users[0].karma
assert_equal 0, users[1].karma
end
+
+ def test_plusminus_tally_scopes_by_voteable_type
+ user = User.create(:name => 'david')
+ item = Item.create(:name => 'XBOX', :description => 'XBOX console')
+ another_item = OtherItem.create(:name => 'Playstation', :description => 'Playstation console')
+
+ user.vote_for(item)
+ user.vote_for(another_item)
+
+ assert_equal 1, Item.plusminus_tally.sum(&:plusminus_tally).to_i
+ assert_equal 1, OtherItem.plusminus_tally.sum(&:plusminus_tally).to_i
+ end
+
end

0 comments on commit 3fce588

Please sign in to comment.
Something went wrong with that request. Please try again.