thumbs_up 0.6.2 → 0.6.3

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.
data/Rakefile CHANGED
@@ -30,12 +30,14 @@ task :release => :build do
30
30
  system "rm thumbs_up-#{ThumbsUp::VERSION}.gem"
31
31
  end
32
32
 
33
- task :test_both_databases do
34
- # Test both MySQL and Postgres.
33
+ task :test_all_databases do
34
+ # Test MySQL, Postgres and SQLite3
35
35
  ENV['DB'] = 'mysql'
36
36
  Rake::Task['test'].execute
37
37
  ENV['DB'] = 'postgres'
38
38
  Rake::Task['test'].execute
39
+ ENV['DB'] = 'sqlite3'
40
+ Rake::Task['test'].execute
39
41
  end
40
42
 
41
- task :default => :test_both_databases
43
+ task :default => :test_all_databases
@@ -26,21 +26,12 @@ module ThumbsUp
26
26
  def plusminus_tally(params = {})
27
27
  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}'")
28
28
  t = t.order("plusminus_tally DESC")
29
- t = t.group("#{self.table_name}.id")
29
+ t = t.group(column_names_for_tally)
30
30
  t = t.select("#{self.table_name}.*")
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")
31
+ t = t.select("SUM(CASE #{Vote.table_name}.vote WHEN #{quoted_true} THEN 1 WHEN #{quoted_false} THEN -1 ELSE 0 END) AS plusminus_tally")
41
32
  if params[:separate_updown]
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")
33
+ t = t.select("SUM(CASE #{Vote.table_name}.vote WHEN #{quoted_true} THEN 1 WHEN #{quoted_false} THEN 0 ELSE 0 END) AS up")
34
+ t = t.select("SUM(CASE #{Vote.table_name}.vote WHEN #{quoted_true} THEN 0 WHEN #{quoted_false} THEN 1 ELSE 0 END) AS down")
44
35
  end
45
36
  t = t.select("COUNT(#{Vote.table_name}.id) AS vote_count")
46
37
  end
@@ -57,7 +48,7 @@ module ThumbsUp
57
48
  def tally(*args)
58
49
  t = self.joins("LEFT OUTER JOIN #{Vote.table_name} ON #{self.table_name}.id = #{Vote.table_name}.voteable_id")
59
50
  t = t.order("vote_count DESC")
60
- t = t.group("#{self.table_name}.id")
51
+ t = t.group(column_names_for_tally)
61
52
  t = t.select("#{self.table_name}.*")
62
53
  t = t.select("COUNT(#{Vote.table_name}.id) AS vote_count")
63
54
  end
@@ -115,6 +106,14 @@ module ThumbsUp
115
106
  votes.map(&:voter).uniq
116
107
  end
117
108
 
109
+ def voters_who_voted_for
110
+ votes.where(:vote => true).map(&:voter).uniq
111
+ end
112
+
113
+ def voters_who_voted_against
114
+ votes.where(:vote => false).map(&:voter).uniq
115
+ end
116
+
118
117
  def voted_by?(voter)
119
118
  0 < Vote.where(
120
119
  :voteable_id => self.id,
data/lib/has_karma.rb CHANGED
@@ -19,11 +19,11 @@ module ThumbsUp #:nodoc:
19
19
 
20
20
  module SingletonMethods
21
21
 
22
- ## Not yet implemented. Don't use it!
22
+ # Not yet implemented. Don't use it!
23
23
  # Find the most popular users
24
- def find_most_karmic
25
- self.all
26
- end
24
+ # def find_most_karmic
25
+ # self.all
26
+ # end
27
27
 
28
28
  end
29
29
 
@@ -1,3 +1,3 @@
1
1
  module ThumbsUp
2
- VERSION = '0.6.2'
2
+ VERSION = '0.6.3'
3
3
  end
data/lib/thumbs_up.rb CHANGED
@@ -4,9 +4,12 @@ require 'has_karma'
4
4
 
5
5
  module ThumbsUp
6
6
  module Base
7
- # Check if we're connected to a MySQL database.
8
- def mysql?
9
- ActiveRecord::Base.connection.adapter_name == 'MySQL'
7
+ def quoted_true
8
+ ActiveRecord::Base.connection.quoted_true
9
+ end
10
+
11
+ def quoted_false
12
+ ActiveRecord::Base.connection.quoted_false
10
13
  end
11
14
  end
12
15
  end
data/test/test_helper.rb CHANGED
@@ -7,14 +7,13 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
7
 
8
8
  require 'active_record'
9
9
 
10
- if ENV['DB'] == 'mysql'
11
- if ENV['TRAVIS']
12
- config = {
13
- :adapter => 'mysql2',
14
- :database => 'thumbs_up_test',
15
- :username => 'root'
16
- }
17
- else
10
+ config = {
11
+ :database => 'thumbs_up_test',
12
+ :username => 'test'
13
+ }
14
+
15
+ case ENV['DB']
16
+ when 'mysql'
18
17
  config = {
19
18
  :adapter => 'mysql2',
20
19
  :database => 'thumbs_up_test',
@@ -22,33 +21,42 @@ if ENV['DB'] == 'mysql'
22
21
  :password => 'test',
23
22
  :socket => '/tmp/mysql.sock'
24
23
  }
25
- end
26
-
27
- ActiveRecord::Base.establish_connection(config)
28
- ActiveRecord::Base.connection.drop_database config[:database] rescue nil
29
- ActiveRecord::Base.connection.create_database config[:database]
30
- ActiveRecord::Base.establish_connection(config)
31
- else
32
- if ENV['TRAVIS']
24
+ if ENV['TRAVIS']
25
+ config = {
26
+ :adapter => 'mysql2',
27
+ :database => 'thumbs_up_test',
28
+ :username => 'root'
29
+ }
30
+ end
31
+ ActiveRecord::Base.establish_connection(config)
32
+ ActiveRecord::Base.connection.drop_database(config[:database]) rescue nil
33
+ ActiveRecord::Base.connection.create_database(config[:database])
34
+ when 'postgres'
33
35
  config = {
34
36
  :adapter => 'postgresql',
35
37
  :database => 'thumbs_up_test',
36
- :username => 'postgres'
38
+ :username => 'test',
37
39
  }
38
- else
40
+ if ENV['TRAVIS']
41
+ config = {
42
+ :adapter => 'postgresql',
43
+ :database => 'thumbs_up_test',
44
+ :username => 'postgres',
45
+ }
46
+ end
47
+ ActiveRecord::Base.establish_connection(config.merge({ :database => 'postgres' }))
48
+ ActiveRecord::Base.connection.drop_database(config[:database])
49
+ ActiveRecord::Base.connection.create_database(config[:database])
50
+ when 'sqlite3'
39
51
  config = {
40
- :adapter => 'postgresql',
41
- :database => 'thumbs_up_test',
42
- :username => 'test'
52
+ :adapter => 'sqlite3',
53
+ :database => 'test.sqlite3',
54
+ :username => 'test',
43
55
  }
44
- end
45
-
46
- ActiveRecord::Base.establish_connection(config.merge({ :database => 'postgres' }))
47
- ActiveRecord::Base.connection.drop_database config[:database]
48
- ActiveRecord::Base.connection.create_database config[:database]
49
- ActiveRecord::Base.establish_connection(config)
50
56
  end
51
57
 
58
+ ActiveRecord::Base.establish_connection(config)
59
+
52
60
  ActiveRecord::Migration.verbose = false
53
61
 
54
62
  ActiveRecord::Schema.define do
@@ -64,13 +64,22 @@ class TestThumbsUp < Test::Unit::TestCase
64
64
  end
65
65
 
66
66
  def test_acts_as_voteable_instance_methods
67
+ item = Item.create(:name => 'XBOX', :description => 'XBOX console')
68
+
69
+ assert_equal 0, item.ci_plusminus
70
+
67
71
  user_for = User.create(:name => 'david')
68
72
  another_user_for = User.create(:name => 'name')
69
73
  user_against = User.create(:name => 'brady')
70
- item = Item.create(:name => 'XBOX', :description => 'XBOX console')
74
+ another_user_against = User.create(:name => 'name')
71
75
 
72
76
  user_for.vote_for(item)
73
77
  another_user_for.vote_for(item)
78
+ # Use #reload to force reloading of votes from the database,
79
+ # otherwise these tests fail after "assert_equal 0, item.ci_plusminus" caches
80
+ # the votes. We hack this as caching is the correct behavious, per-request,
81
+ # in production.
82
+ item.reload
74
83
 
75
84
  assert_equal 2, item.votes_for
76
85
  assert_equal 0, item.votes_against
@@ -94,6 +103,18 @@ class TestThumbsUp < Test::Unit::TestCase
94
103
  assert voters_who_voted.include?(another_user_for)
95
104
  assert voters_who_voted.include?(user_against)
96
105
 
106
+ voters_who_voted_for = item.voters_who_voted_for
107
+ assert_equal 2, voters_who_voted_for.size
108
+ assert voters_who_voted_for.include?(user_for)
109
+ assert voters_who_voted_for.include?(another_user_for)
110
+
111
+ another_user_against.vote_against(item)
112
+
113
+ voters_who_voted_against = item.voters_who_voted_against
114
+ assert_equal 2, voters_who_voted_against.size
115
+ assert voters_who_voted_against.include?(user_against)
116
+ assert voters_who_voted_against.include?(another_user_against)
117
+
97
118
  non_voting_user = User.create(:name => 'random')
98
119
 
99
120
  assert_equal true, item.voted_by?(user_for)
@@ -334,6 +355,18 @@ class TestThumbsUp < Test::Unit::TestCase
334
355
  assert_equal item_against, Item.plusminus_tally.reorder('plusminus_tally ASC')[0]
335
356
  end
336
357
 
358
+ def test_plusminus_tally_limit_with_where_and_having
359
+ users = (0..9).map{ |u| User.create(:name => "User #{u}") }
360
+ items = (0..9).map{ |u| Item.create(:name => "Item #{u}", :description => "Item #{u}") }
361
+ users.each{ |u| items[0..8].each { |i| u.vote_for(i) } }
362
+
363
+ # Postgresql doesn't accept aliases in HAVING clauses, so you'll need to copy and paste the whole statement from the #plusminus_tally method if you want to use HAVING('plusminus_tally > 10'), for example.
364
+ assert_equal 0, Item.plusminus_tally.limit(5).where('created_at > ?', 2.days.ago).having("SUM(CASE #{Vote.table_name}.vote WHEN #{ActiveRecord::Base.connection.quoted_true} THEN 1 WHEN #{ActiveRecord::Base.connection.quoted_false} THEN -1 ELSE 0 END) > 10").length
365
+ assert_equal 5, Item.plusminus_tally.limit(5).where('created_at > ?', 2.days.ago).having("SUM(CASE #{Vote.table_name}.vote WHEN #{ActiveRecord::Base.connection.quoted_true} THEN 1 WHEN #{ActiveRecord::Base.connection.quoted_false} THEN -1 ELSE 0 END) > 9").length
366
+ assert_equal 9, Item.plusminus_tally.limit(10).where('created_at > ?', 2.days.ago).having("SUM(CASE #{Vote.table_name}.vote WHEN #{ActiveRecord::Base.connection.quoted_true} THEN 1 WHEN #{ActiveRecord::Base.connection.quoted_false} THEN -1 ELSE 0 END) > 9").length
367
+ assert_equal 0, Item.plusminus_tally.limit(10).where('created_at > ?', 1.day.from_now).having("SUM(CASE #{Vote.table_name}.vote WHEN #{ActiveRecord::Base.connection.quoted_true} THEN 1 WHEN #{ActiveRecord::Base.connection.quoted_false} THEN -1 ELSE 0 END) > 9").length
368
+ end
369
+
337
370
  def test_plusminus_tally_count
338
371
  Item.plusminus_tally.except(:order).count
339
372
  end
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.2
4
+ version: 0.6.3
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-09-19 00:00:00.000000000 Z
17
+ date: 2013-01-20 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: activerecord
@@ -112,6 +112,22 @@ dependencies:
112
112
  - - ! '>='
113
113
  - !ruby/object:Gem::Version
114
114
  version: '0'
115
+ - !ruby/object:Gem::Dependency
116
+ name: sqlite3
117
+ requirement: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ type: :development
124
+ prerelease: false
125
+ version_requirements: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
115
131
  - !ruby/object:Gem::Dependency
116
132
  name: rake
117
133
  requirement: !ruby/object:Gem::Requirement
@@ -166,7 +182,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
166
182
  version: '0'
167
183
  segments:
168
184
  - 0
169
- hash: 173536367113367291
185
+ hash: 2682672596544266431
170
186
  required_rubygems_version: !ruby/object:Gem::Requirement
171
187
  none: false
172
188
  requirements:
@@ -175,7 +191,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
191
  version: '0'
176
192
  segments:
177
193
  - 0
178
- hash: 173536367113367291
194
+ hash: 2682672596544266431
179
195
  requirements: []
180
196
  rubyforge_project:
181
197
  rubygems_version: 1.8.23