Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed bad cast, now with tests!

  • Loading branch information...
commit ea3a469e7dba00f95a7823976ae0e38b9918a8c8 1 parent a3e30f0
@bouchard authored
View
15 lib/acts_as_voteable.rb
@@ -28,10 +28,19 @@ def plusminus_tally(params = {})
t = t.order("plusminus_tally DESC")
t = t.group("#{self.table_name}.id")
t = t.select("#{self.table_name}.*")
- t = t.select("SUM(CASE WHEN #{Vote.table_name}.vote THEN 1 ELSE -1 END) AS plusminus_tally")
+ if mysql?
+ table = "CAST(#{Vote.table_name}.vote AS UNSIGNED)"
+ true_value = '1'
+ false_value = '0'
+ else
+ table = "#{Vote.table_name}.vote"
+ true_value = 'true'
+ false_value = 'false'
+ end
+ t = t.select("SUM(CASE #{table} WHEN #{true_value} THEN 1 WHEN #{false_value} THEN -1 ELSE 0 END) AS plusminus_tally")
if params[:separate_updown]
- t = t.select("SUM(CASE WHEN #{Vote.table_name}.vote THEN 1 ELSE 0 END) AS up")
- t = t.select("SUM(CASE WHEN #{Vote.table_name}.vote THEN 0 ELSE 1 END) AS down")
+ t = t.select("SUM(CASE #{table} WHEN #{true_value} THEN 1 WHEN #{false_value} THEN 0 ELSE 0 END) AS up")
+ t = t.select("SUM(CASE #{table} WHEN #{true_value} THEN 0 WHEN #{false_value} THEN 1 ELSE 0 END) AS down")
end
t = t.select("COUNT(#{Vote.table_name}.id) AS vote_count")
end
View
2  lib/thumbs_up/version.rb
@@ -1,3 +1,3 @@
module ThumbsUp
- VERSION = '0.6.0'
+ VERSION = '0.6.1'
end
View
36 test/thumbs_up_test.rb
@@ -237,6 +237,42 @@ def test_plusminus_tally_inclusion
end
end
+ def test_plusminus_tally_up
+ user = User.create(:name => 'david')
+ item1 = Item.create(:name => 'XBOX', :description => 'XBOX console')
+ item2 = Item.create(:name => 'Playstation', :description => 'Playstation console')
+ item3 = Item.create(:name => 'Wii', :description => 'Wii console')
+
+ assert_not_nil user.vote_for(item1)
+ assert_not_nil user.vote_against(item2)
+
+ assert_equal [1, 0, 0], Item.plusminus_tally(:separate_updown => true).map(&:up).map(&:to_i)
+ end
+
+ def test_plusminus_tally_down
+ user = User.create(:name => 'david')
+ item1 = Item.create(:name => 'XBOX', :description => 'XBOX console')
+ item2 = Item.create(:name => 'Playstation', :description => 'Playstation console')
+ item3 = Item.create(:name => 'Wii', :description => 'Wii console')
+
+ assert_not_nil user.vote_for(item1)
+ assert_not_nil user.vote_against(item2)
+
+ assert_equal [0, 0, 1], Item.plusminus_tally(:separate_updown => true).map(&:down).map(&:to_i)
+ end
+
+ def test_plusminus_tally_vote_count
+ user = User.create(:name => 'david')
+ item1 = Item.create(:name => 'XBOX', :description => 'XBOX console')
+ item2 = Item.create(:name => 'Playstation', :description => 'Playstation console')
+ item3 = Item.create(:name => 'Wii', :description => 'Wii console')
+
+ assert_not_nil user.vote_for(item1)
+ assert_not_nil user.vote_against(item2)
+
+ assert_equal [1, 0, -1], Item.plusminus_tally.map(&:plusminus_tally).map(&:to_i)
+ end
+
def test_plusminus_tally_voting_for
user1 = User.create(:name => 'david')
item = Item.create(:name => 'Playstation', :description => 'Playstation console')

1 comment on commit ea3a469

@pixelsonly

Loving the Postgres support, much appreciated!

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