thumbs_up 0.6.4 → 0.6.5
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.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/README.md +18 -2
- data/lib/acts_as_voter.rb +20 -1
- data/lib/generators/thumbs_up/templates/vote.rb +2 -2
- data/lib/thumbs_up.rb +1 -1
- data/lib/thumbs_up/version.rb +1 -1
- data/test/test_helper.rb +2 -2
- data/test/thumbs_up_test.rb +28 -17
- metadata +5 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 11003465d42393c944f234e11e53ad2d772e4089
|
|
4
|
+
data.tar.gz: 6ad0d8e91290c87792123ea3337c714fc5f5ee16
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fd79c8576b6e65baf65ac5da1921700ac5a07740a43b325f239c90dc4adc11f83480a48f1d3b49def1ff78983722d14609f3df219afa0f9fdd3ff25ce141cce1
|
|
7
|
+
data.tar.gz: 7029585f3bbe640e98469557c0bfd5088f7f379d8e403ca73428ddf7d3dc6accb66952a287fa41a88bf4db87002bdfd8814e6bff2499f23f89ac3ac6ffbaba50
|
data/Gemfile
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
source
|
|
1
|
+
source 'http://rubygems.org'
|
|
2
2
|
gemspec
|
data/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
ThumbsUp
|
|
2
2
|
=======
|
|
3
3
|
|
|
4
|
-
[](http://travis-ci.org/bouchard/thumbs_up)
|
|
4
|
+
[](http://travis-ci.org/bouchard/thumbs_up) [](https://codeclimate.com/github/bouchard/thumbs_up)
|
|
5
5
|
|
|
6
6
|
**Note: Version 0.5.x is a breaking change for #plusminus_tally and #tally, with > 50% speedups.**
|
|
7
7
|
|
|
@@ -71,7 +71,7 @@ Usage
|
|
|
71
71
|
voter.vote_exclusively_for(voteable) # Removes any previous votes by that particular voter, and votes for.
|
|
72
72
|
voter.vote_exclusively_against(voteable) # Removes any previous votes by that particular voter, and votes against.
|
|
73
73
|
|
|
74
|
-
|
|
74
|
+
voter.unvote_for(voteable) # Clears all votes for that user
|
|
75
75
|
|
|
76
76
|
### Querying votes
|
|
77
77
|
|
|
@@ -88,6 +88,22 @@ Did the first user vote for or against the Car with id = 2?
|
|
|
88
88
|
u.voted_for?(Car.find(2)) #=> true
|
|
89
89
|
u.voted_against?(Car.find(2)) #=> false
|
|
90
90
|
|
|
91
|
+
Or check directly!
|
|
92
|
+
|
|
93
|
+
u = User.first
|
|
94
|
+
u.vote_for(Car.find(2))
|
|
95
|
+
u.voted_how?(Car.find(2)) #=> true, if voted_for
|
|
96
|
+
|
|
97
|
+
u.vote_against(Car.find(3))
|
|
98
|
+
u.voted_how?(Car.find(3)) #=> false, if voted_against
|
|
99
|
+
|
|
100
|
+
u.vote_for(Car.find(4))
|
|
101
|
+
u.voted_how?(Car.find(4)) #=> nil, if didn't vote for it
|
|
102
|
+
|
|
103
|
+
in case you use `--unique-voting false` (documented below):
|
|
104
|
+
|
|
105
|
+
u.voted_how?(Car.find(2)) #=> [false, true, true, false]
|
|
106
|
+
|
|
91
107
|
#### Tallying Votes
|
|
92
108
|
|
|
93
109
|
You can easily retrieve voteable object collections based on the properties of their votes:
|
data/lib/acts_as_voter.rb
CHANGED
|
@@ -88,7 +88,10 @@ module ThumbsUp #:nodoc:
|
|
|
88
88
|
self.unvote_for(voteable)
|
|
89
89
|
end
|
|
90
90
|
direction = (options[:direction].to_sym == :up)
|
|
91
|
-
|
|
91
|
+
# create! does not return the created object
|
|
92
|
+
v = Vote.new(:vote => direction, :voteable => voteable, :voter => self)
|
|
93
|
+
v.save!
|
|
94
|
+
v
|
|
92
95
|
end
|
|
93
96
|
|
|
94
97
|
def unvote_for(voteable)
|
|
@@ -113,6 +116,22 @@ module ThumbsUp #:nodoc:
|
|
|
113
116
|
).count
|
|
114
117
|
end
|
|
115
118
|
|
|
119
|
+
def voted_how?(voteable)
|
|
120
|
+
votes = Vote.where(
|
|
121
|
+
:voter_id => self.id,
|
|
122
|
+
:voter_type => self.class.base_class.name,
|
|
123
|
+
:voteable_id => voteable.id,
|
|
124
|
+
:voteable_type => voteable.class.base_class.name
|
|
125
|
+
).map(&:vote) #in case votes is premitted to be duplicated
|
|
126
|
+
if votes.count == 1
|
|
127
|
+
votes.first
|
|
128
|
+
elsif votes.count == 0
|
|
129
|
+
nil
|
|
130
|
+
else
|
|
131
|
+
votes
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
116
135
|
end
|
|
117
136
|
end
|
|
118
137
|
end
|
|
@@ -3,12 +3,12 @@ class Vote < ActiveRecord::Base
|
|
|
3
3
|
scope :for_voter, lambda { |*args| where(["voter_id = ? AND voter_type = ?", args.first.id, args.first.class.base_class.name]) }
|
|
4
4
|
scope :for_voteable, lambda { |*args| where(["voteable_id = ? AND voteable_type = ?", args.first.id, args.first.class.base_class.name]) }
|
|
5
5
|
scope :recent, lambda { |*args| where(["created_at > ?", (args.first || 2.weeks.ago)]) }
|
|
6
|
-
scope :descending, order("created_at DESC")
|
|
6
|
+
scope :descending, lambda { order("created_at DESC") }
|
|
7
7
|
|
|
8
8
|
belongs_to :voteable, :polymorphic => true
|
|
9
9
|
belongs_to :voter, :polymorphic => true
|
|
10
10
|
|
|
11
|
-
attr_accessible :vote, :voter, :voteable
|
|
11
|
+
attr_accessible :vote, :voter, :voteable if ActiveRecord::VERSION::MAJOR < 4
|
|
12
12
|
|
|
13
13
|
<% if options[:unique_voting] == true %>
|
|
14
14
|
# Comment out the line below to allow multiple votes per user.
|
data/lib/thumbs_up.rb
CHANGED
data/lib/thumbs_up/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
|
@@ -98,12 +98,12 @@ class Vote < ActiveRecord::Base
|
|
|
98
98
|
scope :for_voter, lambda { |*args| where(["voter_id = ? AND voter_type = ?", args.first.id, args.first.class.name]) }
|
|
99
99
|
scope :for_voteable, lambda { |*args| where(["voteable_id = ? AND voteable_type = ?", args.first.id, args.first.class.name]) }
|
|
100
100
|
scope :recent, lambda { |*args| where(["created_at > ?", (args.first || 2.weeks.ago)]) }
|
|
101
|
-
scope :descending, order("created_at DESC")
|
|
101
|
+
scope :descending, lambda { order("created_at DESC") }
|
|
102
102
|
|
|
103
103
|
belongs_to :voteable, :polymorphic => true
|
|
104
104
|
belongs_to :voter, :polymorphic => true
|
|
105
105
|
|
|
106
|
-
attr_accessible :vote, :voter, :voteable
|
|
106
|
+
attr_accessible :vote, :voter, :voteable if ActiveRecord::VERSION::MAJOR < 4
|
|
107
107
|
|
|
108
108
|
# Comment out the line below to allow multiple votes per user.
|
|
109
109
|
validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id]
|
data/test/thumbs_up_test.rb
CHANGED
|
@@ -11,6 +11,7 @@ class TestThumbsUp < Test::Unit::TestCase
|
|
|
11
11
|
user_for = User.create(:name => 'david')
|
|
12
12
|
user_against = User.create(:name => 'brady')
|
|
13
13
|
item = Item.create(:name => 'XBOX', :description => 'XBOX console')
|
|
14
|
+
item2= Item.create(:name => 'PS3', :description => 'Playstation 3')
|
|
14
15
|
|
|
15
16
|
assert_not_nil user_for.vote_for(item)
|
|
16
17
|
assert_raises(ActiveRecord::RecordInvalid) do
|
|
@@ -24,6 +25,7 @@ class TestThumbsUp < Test::Unit::TestCase
|
|
|
24
25
|
assert_equal 0, user_for.vote_count(:down)
|
|
25
26
|
assert_equal true, user_for.voted_which_way?(item, :up)
|
|
26
27
|
assert_equal false, user_for.voted_which_way?(item, :down)
|
|
28
|
+
assert_equal true, user_for.voted_how?(item)
|
|
27
29
|
assert_equal 1, user_for.votes.where(:voteable_type => 'Item').count
|
|
28
30
|
assert_equal 0, user_for.votes.where(:voteable_type => 'AnotherItem').count
|
|
29
31
|
assert_raises(ArgumentError) do
|
|
@@ -36,6 +38,7 @@ class TestThumbsUp < Test::Unit::TestCase
|
|
|
36
38
|
end
|
|
37
39
|
assert_equal false, user_against.voted_for?(item)
|
|
38
40
|
assert_equal true, user_against.voted_against?(item)
|
|
41
|
+
assert_equal false, user_against.voted_how?(item)
|
|
39
42
|
assert_equal true, user_against.voted_on?(item)
|
|
40
43
|
assert_equal 1, user_against.vote_count
|
|
41
44
|
assert_equal 0, user_against.vote_count(:up)
|
|
@@ -61,6 +64,18 @@ class TestThumbsUp < Test::Unit::TestCase
|
|
|
61
64
|
assert_raises(ArgumentError) do
|
|
62
65
|
user_for.vote(item, {:direction => :foo})
|
|
63
66
|
end
|
|
67
|
+
|
|
68
|
+
vote = user_against.vote(item, :exclusive => true, :direction => :down)
|
|
69
|
+
assert_equal true, user_against.voted_against?(item)
|
|
70
|
+
# Make sure the vote record was returned by the :vote method
|
|
71
|
+
assert_equal true, vote.is_a?(Vote)
|
|
72
|
+
|
|
73
|
+
vote = user_for.vote(item, :exclusive => true, :direction => :up)
|
|
74
|
+
assert_equal true, user_for.voted_for?(item)
|
|
75
|
+
# Make sure the vote record was returned by the :vote method
|
|
76
|
+
assert_equal true, vote.is_a?(Vote)
|
|
77
|
+
|
|
78
|
+
assert_nil user_for.voted_how?(item2)
|
|
64
79
|
end
|
|
65
80
|
|
|
66
81
|
def test_acts_as_voteable_instance_methods
|
|
@@ -125,7 +140,9 @@ class TestThumbsUp < Test::Unit::TestCase
|
|
|
125
140
|
|
|
126
141
|
def test_tally_empty
|
|
127
142
|
item = Item.create(:name => 'XBOX', :description => 'XBOX console')
|
|
128
|
-
|
|
143
|
+
# COUNT(#{Vote.table_name}.id) is equivalent to aliased column `vote_count` - Postgres
|
|
144
|
+
# requires the non-aliased name in a HAVING clause.
|
|
145
|
+
assert_equal 0, Item.tally.having("COUNT(#{Vote.table_name}.id) > 0").length
|
|
129
146
|
end
|
|
130
147
|
|
|
131
148
|
def test_tally_has_id
|
|
@@ -135,7 +152,7 @@ class TestThumbsUp < Test::Unit::TestCase
|
|
|
135
152
|
|
|
136
153
|
user.vote_for(item2)
|
|
137
154
|
|
|
138
|
-
assert_not_nil Item.tally.
|
|
155
|
+
assert_not_nil Item.tally.first.id
|
|
139
156
|
end
|
|
140
157
|
|
|
141
158
|
def test_tally_starts_at
|
|
@@ -187,10 +204,6 @@ class TestThumbsUp < Test::Unit::TestCase
|
|
|
187
204
|
Item.tally.except(:order).any?
|
|
188
205
|
end
|
|
189
206
|
|
|
190
|
-
def test_tally_empty
|
|
191
|
-
Item.tally.except(:order).empty?
|
|
192
|
-
end
|
|
193
|
-
|
|
194
207
|
def test_plusminus_tally_not_empty_without_conditions
|
|
195
208
|
item = Item.create(:name => 'XBOX', :description => 'XBOX console')
|
|
196
209
|
assert_equal 1, Item.plusminus_tally.length
|
|
@@ -198,7 +211,9 @@ class TestThumbsUp < Test::Unit::TestCase
|
|
|
198
211
|
|
|
199
212
|
def test_plusminus_tally_empty
|
|
200
213
|
item = Item.create(:name => 'XBOX', :description => 'XBOX console')
|
|
201
|
-
|
|
214
|
+
# COUNT(#{Vote.table_name}.id) is equivalent to aliased column `vote_count` - Postgres
|
|
215
|
+
# requires the non-aliased name in a HAVING clause.
|
|
216
|
+
assert_equal 0, Item.plusminus_tally.having("COUNT(#{Vote.table_name}.id) > 0").length
|
|
202
217
|
end
|
|
203
218
|
|
|
204
219
|
def test_plusminus_tally_starts_at
|
|
@@ -250,11 +265,11 @@ class TestThumbsUp < Test::Unit::TestCase
|
|
|
250
265
|
assert_not_nil user.vote_for(item)
|
|
251
266
|
|
|
252
267
|
if ActiveRecord::Base.connection.adapter_name == 'MySQL'
|
|
253
|
-
assert
|
|
254
|
-
assert
|
|
268
|
+
assert(Item.plusminus_tally.having('vote_count > 0').include?(item))
|
|
269
|
+
assert(!Item.plusminus_tally.having('vote_count > 0').include?(item_not_included))
|
|
255
270
|
else
|
|
256
|
-
assert
|
|
257
|
-
assert
|
|
271
|
+
assert(Item.plusminus_tally.having('COUNT(votes.id) > 0').include?(item))
|
|
272
|
+
assert(!Item.plusminus_tally.having('COUNT(votes.id) > 0').include?(item_not_included))
|
|
258
273
|
end
|
|
259
274
|
end
|
|
260
275
|
|
|
@@ -375,10 +390,6 @@ class TestThumbsUp < Test::Unit::TestCase
|
|
|
375
390
|
Item.plusminus_tally.except(:order).any?
|
|
376
391
|
end
|
|
377
392
|
|
|
378
|
-
def test_plusminus_tally_empty
|
|
379
|
-
Item.plusminus_tally.except(:order).empty?
|
|
380
|
-
end
|
|
381
|
-
|
|
382
393
|
def test_karma
|
|
383
394
|
users = (0..1).map{ |u| User.create(:name => "User #{u}") }
|
|
384
395
|
items = (0..1).map{ |u| users[0].items.create(:name => "Item #{u}", :description => "Item #{u}") }
|
|
@@ -396,8 +407,8 @@ class TestThumbsUp < Test::Unit::TestCase
|
|
|
396
407
|
user.vote_for(item)
|
|
397
408
|
user.vote_for(another_item)
|
|
398
409
|
|
|
399
|
-
assert_equal 1, Item.plusminus_tally.sum(&:plusminus_tally).to_i
|
|
400
|
-
assert_equal 1, OtherItem.plusminus_tally.sum(&:plusminus_tally).to_i
|
|
410
|
+
assert_equal 1, Item.plusminus_tally.to_a.sum(&:plusminus_tally).to_i
|
|
411
|
+
assert_equal 1, OtherItem.plusminus_tally.to_a.sum(&:plusminus_tally).to_i
|
|
401
412
|
end
|
|
402
413
|
|
|
403
414
|
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.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Brady Bouchard
|
|
@@ -13,7 +13,7 @@ authors:
|
|
|
13
13
|
autorequire:
|
|
14
14
|
bindir: bin
|
|
15
15
|
cert_chain: []
|
|
16
|
-
date: 2013-
|
|
16
|
+
date: 2013-07-05 00:00:00.000000000 Z
|
|
17
17
|
dependencies:
|
|
18
18
|
- !ruby/object:Gem::Dependency
|
|
19
19
|
name: activerecord
|
|
@@ -154,7 +154,8 @@ files:
|
|
|
154
154
|
- README.md
|
|
155
155
|
- Rakefile
|
|
156
156
|
homepage: http://github.com/bouchard/thumbs_up
|
|
157
|
-
licenses:
|
|
157
|
+
licenses:
|
|
158
|
+
- MIT
|
|
158
159
|
metadata: {}
|
|
159
160
|
post_install_message:
|
|
160
161
|
rdoc_options: []
|
|
@@ -172,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
172
173
|
version: '0'
|
|
173
174
|
requirements: []
|
|
174
175
|
rubyforge_project:
|
|
175
|
-
rubygems_version: 2.0.
|
|
176
|
+
rubygems_version: 2.0.2
|
|
176
177
|
signing_key:
|
|
177
178
|
specification_version: 4
|
|
178
179
|
summary: Voting for ActiveRecord with multiple vote sources and karma calculation.
|