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 +5 -3
- data/lib/acts_as_voteable.rb +13 -14
- data/lib/has_karma.rb +4 -4
- data/lib/thumbs_up/version.rb +1 -1
- data/lib/thumbs_up.rb +6 -3
- data/test/test_helper.rb +35 -27
- data/test/thumbs_up_test.rb +34 -1
- metadata +20 -4
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 :
|
|
34
|
-
# Test
|
|
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 => :
|
|
43
|
+
task :default => :test_all_databases
|
data/lib/acts_as_voteable.rb
CHANGED
|
@@ -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(
|
|
29
|
+
t = t.group(column_names_for_tally)
|
|
30
30
|
t = t.select("#{self.table_name}.*")
|
|
31
|
-
|
|
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 #{
|
|
43
|
-
t = t.select("SUM(CASE #{
|
|
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(
|
|
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
|
-
|
|
22
|
+
# Not yet implemented. Don't use it!
|
|
23
23
|
# Find the most popular users
|
|
24
|
-
def find_most_karmic
|
|
25
|
-
|
|
26
|
-
end
|
|
24
|
+
# def find_most_karmic
|
|
25
|
+
# self.all
|
|
26
|
+
# end
|
|
27
27
|
|
|
28
28
|
end
|
|
29
29
|
|
data/lib/thumbs_up/version.rb
CHANGED
data/lib/thumbs_up.rb
CHANGED
|
@@ -4,9 +4,12 @@ require 'has_karma'
|
|
|
4
4
|
|
|
5
5
|
module ThumbsUp
|
|
6
6
|
module Base
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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 => '
|
|
38
|
+
:username => 'test',
|
|
37
39
|
}
|
|
38
|
-
|
|
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 => '
|
|
41
|
-
:database => '
|
|
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
|
data/test/thumbs_up_test.rb
CHANGED
|
@@ -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
|
-
|
|
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.
|
|
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:
|
|
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:
|
|
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:
|
|
194
|
+
hash: 2682672596544266431
|
|
179
195
|
requirements: []
|
|
180
196
|
rubyforge_project:
|
|
181
197
|
rubygems_version: 1.8.23
|