thumbs_up 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,10 +28,19 @@ module ThumbsUp
28
28
  t = t.order("plusminus_tally DESC")
29
29
  t = t.group("#{self.table_name}.id")
30
30
  t = t.select("#{self.table_name}.*")
31
- t = t.select("SUM(CASE WHEN #{Vote.table_name}.vote THEN 1 ELSE -1 END) AS plusminus_tally")
31
+ if mysql?
32
+ table = "CAST(#{Vote.table_name}.vote AS UNSIGNED)"
33
+ true_value = '1'
34
+ false_value = '0'
35
+ else
36
+ table = "#{Vote.table_name}.vote"
37
+ true_value = 'true'
38
+ false_value = 'false'
39
+ end
40
+ t = t.select("SUM(CASE #{table} WHEN #{true_value} THEN 1 WHEN #{false_value} THEN -1 ELSE 0 END) AS plusminus_tally")
32
41
  if params[:separate_updown]
33
- t = t.select("SUM(CASE WHEN #{Vote.table_name}.vote THEN 1 ELSE 0 END) AS up")
34
- t = t.select("SUM(CASE WHEN #{Vote.table_name}.vote THEN 0 ELSE 1 END) AS down")
42
+ t = t.select("SUM(CASE #{table} WHEN #{true_value} THEN 1 WHEN #{false_value} THEN 0 ELSE 0 END) AS up")
43
+ t = t.select("SUM(CASE #{table} WHEN #{true_value} THEN 0 WHEN #{false_value} THEN 1 ELSE 0 END) AS down")
35
44
  end
36
45
  t = t.select("COUNT(#{Vote.table_name}.id) AS vote_count")
37
46
  end
@@ -1,3 +1,3 @@
1
1
  module ThumbsUp
2
- VERSION = '0.6.0'
2
+ VERSION = '0.6.1'
3
3
  end
@@ -237,6 +237,42 @@ class TestThumbsUp < Test::Unit::TestCase
237
237
  end
238
238
  end
239
239
 
240
+ def test_plusminus_tally_up
241
+ user = User.create(:name => 'david')
242
+ item1 = Item.create(:name => 'XBOX', :description => 'XBOX console')
243
+ item2 = Item.create(:name => 'Playstation', :description => 'Playstation console')
244
+ item3 = Item.create(:name => 'Wii', :description => 'Wii console')
245
+
246
+ assert_not_nil user.vote_for(item1)
247
+ assert_not_nil user.vote_against(item2)
248
+
249
+ assert_equal [1, 0, 0], Item.plusminus_tally(:separate_updown => true).map(&:up).map(&:to_i)
250
+ end
251
+
252
+ def test_plusminus_tally_down
253
+ user = User.create(:name => 'david')
254
+ item1 = Item.create(:name => 'XBOX', :description => 'XBOX console')
255
+ item2 = Item.create(:name => 'Playstation', :description => 'Playstation console')
256
+ item3 = Item.create(:name => 'Wii', :description => 'Wii console')
257
+
258
+ assert_not_nil user.vote_for(item1)
259
+ assert_not_nil user.vote_against(item2)
260
+
261
+ assert_equal [0, 0, 1], Item.plusminus_tally(:separate_updown => true).map(&:down).map(&:to_i)
262
+ end
263
+
264
+ def test_plusminus_tally_vote_count
265
+ user = User.create(:name => 'david')
266
+ item1 = Item.create(:name => 'XBOX', :description => 'XBOX console')
267
+ item2 = Item.create(:name => 'Playstation', :description => 'Playstation console')
268
+ item3 = Item.create(:name => 'Wii', :description => 'Wii console')
269
+
270
+ assert_not_nil user.vote_for(item1)
271
+ assert_not_nil user.vote_against(item2)
272
+
273
+ assert_equal [1, 0, -1], Item.plusminus_tally.map(&:plusminus_tally).map(&:to_i)
274
+ end
275
+
240
276
  def test_plusminus_tally_voting_for
241
277
  user1 = User.create(:name => 'david')
242
278
  item = Item.create(:name => 'Playstation', :description => 'Playstation console')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thumbs_up
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2012-07-29 00:00:00.000000000 Z
17
+ date: 2012-07-30 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: activerecord
@@ -166,7 +166,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
166
166
  version: '0'
167
167
  segments:
168
168
  - 0
169
- hash: -1660029981921768377
169
+ hash: 2774307803948239525
170
170
  required_rubygems_version: !ruby/object:Gem::Requirement
171
171
  none: false
172
172
  requirements:
@@ -175,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
175
  version: '0'
176
176
  segments:
177
177
  - 0
178
- hash: -1660029981921768377
178
+ hash: 2774307803948239525
179
179
  requirements: []
180
180
  rubyforge_project:
181
181
  rubygems_version: 1.8.24