Skip to content

Commit

Permalink
Merge branch 'jerefrer/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
bouchard committed Sep 19, 2012
2 parents ea3a469 + 091281e commit 3fce588
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/acts_as_voteable.rb
Expand Up @@ -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}.*")
Expand Down
11 changes: 11 additions & 0 deletions test/test_helper.rb
Expand Up @@ -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'
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/thumbs_up_test.rb
Expand Up @@ -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.