acts_as_votable 0.5.0 → 0.9.0
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 +7 -0
- data/.gitignore +1 -0
- data/.travis.yml +25 -0
- data/Gemfile +14 -1
- data/README.markdown +243 -143
- data/Rakefile +8 -0
- data/acts_as_votable.gemspec +1 -6
- data/lib/acts_as_votable/extenders/controller.rb +19 -0
- data/lib/acts_as_votable/version.rb +1 -1
- data/lib/acts_as_votable/votable.rb +118 -44
- data/lib/acts_as_votable/vote.rb +8 -6
- data/lib/acts_as_votable/voter.rb +19 -20
- data/lib/acts_as_votable.rb +5 -0
- data/lib/generators/acts_as_votable/migration/migration_generator.rb +2 -2
- data/lib/generators/acts_as_votable/migration/templates/active_record/migration.rb +6 -2
- data/spec/shared_example/votable_model_spec.rb +393 -0
- data/spec/shared_example/voter_model_spec.rb +279 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/votable_spec.rb +6 -277
- data/spec/votable_voter_spec.rb +20 -0
- data/spec/voter_spec.rb +6 -289
- metadata +21 -42
- data/Gemfile.lock +0 -104
@@ -0,0 +1,393 @@
|
|
1
|
+
shared_examples "a votable_model" do
|
2
|
+
it "should return false when a vote with no voter is saved" do
|
3
|
+
votable.vote_by.should be false
|
4
|
+
end
|
5
|
+
|
6
|
+
it "should have one vote when saved" do
|
7
|
+
votable.vote_by :voter => voter, :vote => 'yes'
|
8
|
+
votable.votes_for.size.should == 1
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should have one vote when voted on twice by the same person" do
|
12
|
+
votable.vote_by :voter => voter, :vote => 'yes'
|
13
|
+
votable.vote_by :voter => voter, :vote => 'no'
|
14
|
+
votable.votes_for.size.should == 1
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should have two votes_for when voted on twice by the same person with duplicate paramenter" do
|
18
|
+
votable.vote_by :voter => voter, :vote => 'yes'
|
19
|
+
votable.vote_by :voter => voter, :vote => 'no', :duplicate => true
|
20
|
+
votable.votes_for.size.should == 2
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should have one scoped vote when voting under an scope" do
|
24
|
+
votable.vote_by :voter => voter, :vote => 'yes', :vote_scope => 'rank'
|
25
|
+
votable.find_votes_for(:vote_scope => 'rank').size.should == 1
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should have one vote when voted on twice using scope by the same person" do
|
29
|
+
votable.vote_by :voter => voter, :vote => 'yes', :vote_scope => 'rank'
|
30
|
+
votable.vote_by :voter => voter, :vote => 'no', :vote_scope => 'rank'
|
31
|
+
votable.find_votes_for(:vote_scope => 'rank').size.should == 1
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should have two votes_for when voting on two different scopes by the same person" do
|
35
|
+
votable.vote_by :voter => voter, :vote => 'yes', :vote_scope => 'weekly_rank'
|
36
|
+
votable.vote_by :voter => voter, :vote => 'no', :vote_scope => 'monthly_rank'
|
37
|
+
votable.votes_for.size.should == 2
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should be callable with vote_up" do
|
41
|
+
votable.vote_up voter
|
42
|
+
votable.get_up_votes.first.voter.should == voter
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should be callable with vote_down" do
|
46
|
+
votable.vote_down voter
|
47
|
+
votable.get_down_votes.first.voter.should == voter
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should have 2 votes_for when voted on once by two different people" do
|
51
|
+
votable.vote_by :voter => voter
|
52
|
+
votable.vote_by :voter => voter2
|
53
|
+
votable.votes_for.size.should == 2
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should have one true vote" do
|
57
|
+
votable.vote_by :voter => voter
|
58
|
+
votable.vote_by :voter => voter2, :vote => 'dislike'
|
59
|
+
votable.get_up_votes.size.should == 1
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should have 2 false votes_for" do
|
63
|
+
votable.vote_by :voter => voter, :vote => 'no'
|
64
|
+
votable.vote_by :voter => voter2, :vote => 'dislike'
|
65
|
+
votable.get_down_votes.size.should == 2
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should have been voted on by voter2" do
|
69
|
+
votable.vote_by :voter => voter2, :vote => true
|
70
|
+
votable.find_votes_for.first.voter.id.should be voter2.id
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should count the vote as registered if this is the voters first vote" do
|
74
|
+
votable.vote_by :voter => voter
|
75
|
+
votable.vote_registered?.should be true
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should not count the vote as being registered if that voter has already voted and the vote has not changed" do
|
79
|
+
votable.vote_by :voter => voter, :vote => true
|
80
|
+
votable.vote_by :voter => voter, :vote => 'yes'
|
81
|
+
votable.vote_registered?.should be false
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should count the vote as registered if the voter has voted and the vote flag has changed" do
|
85
|
+
votable.vote_by :voter => voter, :vote => true
|
86
|
+
votable.vote_by :voter => voter, :vote => 'dislike'
|
87
|
+
votable.vote_registered?.should be true
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should count the vote as registered if the voter has voted and the vote weight has changed" do
|
91
|
+
votable.vote_by :voter => voter, :vote => true, :vote_weight => 1
|
92
|
+
votable.vote_by :voter => voter, :vote => true, :vote_weight => 2
|
93
|
+
votable.vote_registered?.should be true
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should be voted on by voter" do
|
97
|
+
votable.vote_by :voter => voter
|
98
|
+
votable.voted_on_by?(voter).should be true
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should be able to unvote a voter" do
|
102
|
+
votable.liked_by(voter)
|
103
|
+
votable.unliked_by(voter)
|
104
|
+
votable.voted_on_by?(voter).should be false
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should unvote a positive vote" do
|
108
|
+
votable.vote_by :voter => voter
|
109
|
+
votable.unvote :voter => voter
|
110
|
+
votable.find_votes_for.count.should == 0
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should set the votable to unregistered after unvoting" do
|
114
|
+
votable.vote_by :voter => voter
|
115
|
+
votable.unvote :voter => voter
|
116
|
+
votable.vote_registered?.should be false
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should unvote a negative vote" do
|
120
|
+
votable.vote_by :voter => voter, :vote => 'no'
|
121
|
+
votable.unvote :voter => voter
|
122
|
+
votable.find_votes_for.count.should == 0
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should unvote only the from a single voter" do
|
126
|
+
votable.vote_by :voter => voter
|
127
|
+
votable.vote_by :voter => voter2
|
128
|
+
votable.unvote :voter => voter
|
129
|
+
votable.find_votes_for.count.should == 1
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should be contained to instances" do
|
133
|
+
votable2 = Votable.new(:name => '2nd votable')
|
134
|
+
votable2.save
|
135
|
+
|
136
|
+
votable.vote_by :voter => voter, :vote => false
|
137
|
+
votable2.vote_by :voter => voter, :vote => true
|
138
|
+
votable2.vote_by :voter => voter, :vote => true
|
139
|
+
|
140
|
+
votable.vote_registered?.should be true
|
141
|
+
votable2.vote_registered?.should be false
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should set default vote weight to 1 if not specified" do
|
145
|
+
votable.upvote_by voter
|
146
|
+
votable.find_votes_for.first.vote_weight.should == 1
|
147
|
+
end
|
148
|
+
|
149
|
+
describe "with cached votes_for" do
|
150
|
+
|
151
|
+
before(:each) do
|
152
|
+
clean_database
|
153
|
+
voter = Voter.new(:name => 'i can vote!')
|
154
|
+
voter.save
|
155
|
+
|
156
|
+
votable = Votable.new(:name => 'a voting model without a cache')
|
157
|
+
votable.save
|
158
|
+
|
159
|
+
votable_cache = VotableCache.new(:name => 'voting model with cache')
|
160
|
+
votable_cache.save
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should not update cached votes_for if there are no columns" do
|
164
|
+
votable.vote_by :voter => voter
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should update cached total votes_for if there is a total column" do
|
168
|
+
votable_cache.cached_votes_total = 50
|
169
|
+
votable_cache.vote_by :voter => voter
|
170
|
+
votable_cache.cached_votes_total.should == 1
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should update cached total votes_for when a vote up is removed" do
|
174
|
+
votable_cache.vote_by :voter => voter, :vote => 'true'
|
175
|
+
votable_cache.unvote :voter => voter
|
176
|
+
votable_cache.cached_votes_total.should == 0
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should update cached total votes_for when a vote down is removed" do
|
180
|
+
votable_cache.vote_by :voter => voter, :vote => 'false'
|
181
|
+
votable_cache.unvote :voter => voter
|
182
|
+
votable_cache.cached_votes_total.should == 0
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should update cached score votes_for if there is a score column" do
|
186
|
+
votable_cache.cached_votes_score = 50
|
187
|
+
votable_cache.vote_by :voter => voter
|
188
|
+
votable_cache.cached_votes_score.should == 1
|
189
|
+
votable_cache.vote_by :voter => voter2, :vote => 'false'
|
190
|
+
votable_cache.cached_votes_score.should == 0
|
191
|
+
votable_cache.vote_by :voter => voter, :vote => 'false'
|
192
|
+
votable_cache.cached_votes_score.should == -2
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should update cached score votes_for when a vote up is removed" do
|
196
|
+
votable_cache.vote_by :voter => voter, :vote => 'true'
|
197
|
+
votable_cache.cached_votes_score.should == 1
|
198
|
+
votable_cache.unvote :voter => voter
|
199
|
+
votable_cache.cached_votes_score.should == 0
|
200
|
+
end
|
201
|
+
|
202
|
+
it "should update cached score votes_for when a vote down is removed" do
|
203
|
+
votable_cache.vote_by :voter => voter, :vote => 'false'
|
204
|
+
votable_cache.cached_votes_score.should == -1
|
205
|
+
votable_cache.unvote :voter => voter
|
206
|
+
votable_cache.cached_votes_score.should == 0
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should update cached weighted score if there is a weighted score column" do
|
210
|
+
votable_cache.cached_weighted_score = 50
|
211
|
+
votable_cache.vote_by :voter => voter
|
212
|
+
votable_cache.cached_weighted_score.should == 1
|
213
|
+
votable_cache.vote_by :voter => voter2, :vote => 'false'
|
214
|
+
votable_cache.cached_weighted_score.should == 0
|
215
|
+
votable_cache.vote_by :voter => voter, :vote => 'false'
|
216
|
+
votable_cache.cached_weighted_score.should == -2
|
217
|
+
end
|
218
|
+
|
219
|
+
it "should update cached weighted score votes_for when a vote up is removed" do
|
220
|
+
votable_cache.vote_by :voter => voter, :vote => 'true', :vote_weight => 3
|
221
|
+
votable_cache.cached_weighted_score.should == 3
|
222
|
+
votable_cache.unvote :voter => voter
|
223
|
+
votable_cache.cached_weighted_score.should == 0
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should update cached weighted score votes_for when a vote down is removed" do
|
227
|
+
votable_cache.vote_by :voter => voter, :vote => 'false', :vote_weight => 4
|
228
|
+
votable_cache.cached_weighted_score.should == -4
|
229
|
+
votable_cache.unvote :voter => voter
|
230
|
+
votable_cache.cached_weighted_score.should == 0
|
231
|
+
end
|
232
|
+
|
233
|
+
it "should update cached up votes_for if there is an up vote column" do
|
234
|
+
votable_cache.cached_votes_up = 50
|
235
|
+
votable_cache.vote_by :voter => voter
|
236
|
+
votable_cache.vote_by :voter => voter
|
237
|
+
votable_cache.cached_votes_up.should == 1
|
238
|
+
end
|
239
|
+
|
240
|
+
it "should update cached down votes_for if there is a down vote column" do
|
241
|
+
votable_cache.cached_votes_down = 50
|
242
|
+
votable_cache.vote_by :voter => voter, :vote => 'false'
|
243
|
+
votable_cache.cached_votes_down.should == 1
|
244
|
+
end
|
245
|
+
|
246
|
+
it "should update cached up votes_for when a vote up is removed" do
|
247
|
+
votable_cache.vote_by :voter => voter, :vote => 'true'
|
248
|
+
votable_cache.unvote :voter => voter
|
249
|
+
votable_cache.cached_votes_up.should == 0
|
250
|
+
end
|
251
|
+
|
252
|
+
it "should update cached down votes_for when a vote down is removed" do
|
253
|
+
votable_cache.vote_by :voter => voter, :vote => 'false'
|
254
|
+
votable_cache.unvote :voter => voter
|
255
|
+
votable_cache.cached_votes_down.should == 0
|
256
|
+
end
|
257
|
+
|
258
|
+
it "should select from cached total votes_for if there a total column" do
|
259
|
+
votable_cache.vote_by :voter => voter
|
260
|
+
votable_cache.cached_votes_total = 50
|
261
|
+
votable_cache.count_votes_total.should == 50
|
262
|
+
end
|
263
|
+
|
264
|
+
it "should select from cached up votes_for if there is an up vote column" do
|
265
|
+
votable_cache.vote_by :voter => voter
|
266
|
+
votable_cache.cached_votes_up = 50
|
267
|
+
votable_cache.count_votes_up.should == 50
|
268
|
+
end
|
269
|
+
|
270
|
+
it "should select from cached down votes_for if there is a down vote column" do
|
271
|
+
votable_cache.vote_by :voter => voter, :vote => 'false'
|
272
|
+
votable_cache.cached_votes_down = 50
|
273
|
+
votable_cache.count_votes_down.should == 50
|
274
|
+
end
|
275
|
+
|
276
|
+
it "should select from cached weighted score if there is a weighted score column" do
|
277
|
+
votable_cache.vote_by :voter => voter, :vote => 'false'
|
278
|
+
votable_cache.cached_weighted_score = 50
|
279
|
+
votable_cache.weighted_score.should == 50
|
280
|
+
end
|
281
|
+
|
282
|
+
end
|
283
|
+
|
284
|
+
describe "with scoped cached votes_for" do
|
285
|
+
|
286
|
+
it "should update cached total votes_for if there is a total column" do
|
287
|
+
votable_cache.cached_scoped_test_votes_total = 50
|
288
|
+
votable_cache.vote_by :voter => voter, :vote_scope => "test"
|
289
|
+
votable_cache.cached_scoped_test_votes_total.should == 1
|
290
|
+
end
|
291
|
+
|
292
|
+
it "should update cached total votes_for when a vote up is removed" do
|
293
|
+
votable_cache.vote_by :voter => voter, :vote => 'true', :vote_scope => "test"
|
294
|
+
votable_cache.unvote :voter => voter, :vote_scope => "test"
|
295
|
+
votable_cache.cached_scoped_test_votes_total.should == 0
|
296
|
+
end
|
297
|
+
|
298
|
+
it "should update cached total votes_for when a vote down is removed" do
|
299
|
+
votable_cache.vote_by :voter => voter, :vote => 'false', :vote_scope => "test"
|
300
|
+
votable_cache.unvote :voter => voter, :vote_scope => "test"
|
301
|
+
votable_cache.cached_scoped_test_votes_total.should == 0
|
302
|
+
end
|
303
|
+
|
304
|
+
it "should update cached score votes_for if there is a score column" do
|
305
|
+
votable_cache.cached_scoped_test_votes_score = 50
|
306
|
+
votable_cache.vote_by :voter => voter, :vote_scope => "test"
|
307
|
+
votable_cache.cached_scoped_test_votes_score.should == 1
|
308
|
+
votable_cache.vote_by :voter => voter2, :vote => 'false', :vote_scope => "test"
|
309
|
+
votable_cache.cached_scoped_test_votes_score.should == 0
|
310
|
+
votable_cache.vote_by :voter => voter, :vote => 'false', :vote_scope => "test"
|
311
|
+
votable_cache.cached_scoped_test_votes_score.should == -2
|
312
|
+
end
|
313
|
+
|
314
|
+
it "should update cached score votes_for when a vote up is removed" do
|
315
|
+
votable_cache.vote_by :voter => voter, :vote => 'true', :vote_scope => "test"
|
316
|
+
votable_cache.cached_scoped_test_votes_score.should == 1
|
317
|
+
votable_cache.unvote :voter => voter, :vote_scope => "test"
|
318
|
+
votable_cache.cached_scoped_test_votes_score.should == 0
|
319
|
+
end
|
320
|
+
|
321
|
+
it "should update cached score votes_for when a vote down is removed" do
|
322
|
+
votable_cache.vote_by :voter => voter, :vote => 'false', :vote_scope => "test"
|
323
|
+
votable_cache.cached_scoped_test_votes_score.should == -1
|
324
|
+
votable_cache.unvote :voter => voter, :vote_scope => "test"
|
325
|
+
votable_cache.cached_scoped_test_votes_score.should == 0
|
326
|
+
end
|
327
|
+
|
328
|
+
it "should update cached up votes_for if there is an up vote column" do
|
329
|
+
votable_cache.cached_scoped_test_votes_up = 50
|
330
|
+
votable_cache.vote_by :voter => voter, :vote_scope => "test"
|
331
|
+
votable_cache.vote_by :voter => voter, :vote_scope => "test"
|
332
|
+
votable_cache.cached_scoped_test_votes_up.should == 1
|
333
|
+
end
|
334
|
+
|
335
|
+
it "should update cached down votes_for if there is a down vote column" do
|
336
|
+
votable_cache.cached_scoped_test_votes_down = 50
|
337
|
+
votable_cache.vote_by :voter => voter, :vote => 'false', :vote_scope => "test"
|
338
|
+
votable_cache.cached_scoped_test_votes_down.should == 1
|
339
|
+
end
|
340
|
+
|
341
|
+
it "should update cached up votes_for when a vote up is removed" do
|
342
|
+
votable_cache.vote_by :voter => voter, :vote => 'true', :vote_scope => "test"
|
343
|
+
votable_cache.unvote :voter => voter, :vote_scope => "test"
|
344
|
+
votable_cache.cached_scoped_test_votes_up.should == 0
|
345
|
+
end
|
346
|
+
|
347
|
+
it "should update cached down votes_for when a vote down is removed" do
|
348
|
+
votable_cache.vote_by :voter => voter, :vote => 'false', :vote_scope => "test"
|
349
|
+
votable_cache.unvote :voter => voter, :vote_scope => "test"
|
350
|
+
votable_cache.cached_scoped_test_votes_down.should == 0
|
351
|
+
end
|
352
|
+
|
353
|
+
it "should select from cached total votes_for if there a total column" do
|
354
|
+
votable_cache.vote_by :voter => voter, :vote_scope => "test"
|
355
|
+
votable_cache.cached_scoped_test_votes_total = 50
|
356
|
+
votable_cache.count_votes_total(false, "test").should == 50
|
357
|
+
end
|
358
|
+
|
359
|
+
it "should select from cached up votes_for if there is an up vote column" do
|
360
|
+
votable_cache.vote_by :voter => voter, :vote_scope => "test"
|
361
|
+
votable_cache.cached_scoped_test_votes_up = 50
|
362
|
+
votable_cache.count_votes_up(false, "test").should == 50
|
363
|
+
end
|
364
|
+
|
365
|
+
it "should select from cached down votes_for if there is a down vote column" do
|
366
|
+
votable_cache.vote_by :voter => voter, :vote => 'false', :vote_scope => "test"
|
367
|
+
votable_cache.cached_scoped_test_votes_down = 50
|
368
|
+
votable_cache.count_votes_down(false, "test").should == 50
|
369
|
+
end
|
370
|
+
|
371
|
+
end
|
372
|
+
|
373
|
+
describe "sti models" do
|
374
|
+
|
375
|
+
it "should be able to vote on a votable child of a non votable sti model" do
|
376
|
+
votable = VotableChildOfStiNotVotable.create(:name => 'sti child')
|
377
|
+
|
378
|
+
votable.vote_by :voter => voter, :vote => 'yes'
|
379
|
+
votable.votes_for.size.should == 1
|
380
|
+
end
|
381
|
+
|
382
|
+
it "should not be able to vote on a parent non votable" do
|
383
|
+
StiNotVotable.should_not be_votable
|
384
|
+
end
|
385
|
+
|
386
|
+
it "should be able to vote on a child when its parent is votable" do
|
387
|
+
votable = ChildOfStiVotable.create(:name => 'sti child')
|
388
|
+
|
389
|
+
votable.vote_by :voter => voter, :vote => 'yes'
|
390
|
+
votable.votes_for.size.should == 1
|
391
|
+
end
|
392
|
+
end
|
393
|
+
end
|
@@ -0,0 +1,279 @@
|
|
1
|
+
shared_examples "a voter_model" do
|
2
|
+
let (:votable_klass) { votable.class }
|
3
|
+
|
4
|
+
it "should be voted on after a voter has voted" do
|
5
|
+
votable.vote_by :voter => voter
|
6
|
+
voter.voted_on?(votable).should be true
|
7
|
+
voter.voted_for?(votable).should be true
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should not be voted on if a voter has not voted" do
|
11
|
+
voter.voted_on?(votable).should be false
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should be voted on after a voter has voted under scope" do
|
15
|
+
votable.vote_by :voter => voter, :vote_scope => 'rank'
|
16
|
+
voter.voted_on?(votable, :vote_scope => 'rank').should be true
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should not be voted on other scope after a voter has voted under one scope" do
|
20
|
+
votable.vote_by :voter => voter, :vote_scope => 'rank'
|
21
|
+
voter.voted_on?(votable).should be false
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should be voted as true when a voter has voted true" do
|
25
|
+
votable.vote_by :voter => voter
|
26
|
+
voter.voted_as_when_voted_on(votable).should be true
|
27
|
+
voter.voted_as_when_voted_for(votable).should be true
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should be voted as true when a voter has voted true under scope" do
|
31
|
+
votable.vote_by :voter => voter, :vote_scope => 'rank'
|
32
|
+
voter.voted_as_when_voted_for(votable, :vote_scope => 'rank').should be true
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be voted as false when a voter has voted false" do
|
36
|
+
votable.vote_by :voter => voter, :vote => false
|
37
|
+
voter.voted_as_when_voted_for(votable).should be false
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should be voted as false when a voter has voted false under scope" do
|
41
|
+
votable.vote_by :voter => voter, :vote => false, :vote_scope => 'rank'
|
42
|
+
voter.voted_as_when_voted_for(votable, :vote_scope => 'rank').should be false
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should be voted as nil when a voter has never voted" do
|
46
|
+
voter.voted_as_when_voting_on(votable).should be nil
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should be voted as nil when a voter has never voted under the scope" do
|
50
|
+
votable.vote_by :voter => voter, :vote => false, :vote_scope => 'rank'
|
51
|
+
voter.voted_as_when_voting_on(votable).should be nil
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should return true if voter has voted true" do
|
55
|
+
votable.vote_by :voter => voter
|
56
|
+
voter.voted_up_on?(votable).should be true
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should return false if voter has not voted true" do
|
60
|
+
votable.vote_by :voter => voter, :vote => false
|
61
|
+
voter.voted_up_on?(votable).should be false
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should return true if the voter has voted false" do
|
65
|
+
votable.vote_by :voter => voter, :vote => false
|
66
|
+
voter.voted_down_on?(votable).should be true
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should return false if the voter has not voted false" do
|
70
|
+
votable.vote_by :voter => voter, :vote => true
|
71
|
+
voter.voted_down_on?(votable).should be false
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should provide reserve functionality, voter can vote on votable" do
|
75
|
+
voter.vote :votable => votable, :vote => 'bad'
|
76
|
+
voter.voted_as_when_voting_on(votable).should be false
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should allow the voter to vote up a model" do
|
80
|
+
voter.vote_up_for votable
|
81
|
+
votable.get_up_votes.first.voter.should == voter
|
82
|
+
votable.votes_for.up.first.voter.should == voter
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should allow the voter to vote down a model" do
|
86
|
+
voter.vote_down_for votable
|
87
|
+
votable.get_down_votes.first.voter.should == voter
|
88
|
+
votable.votes_for.down.first.voter.should == voter
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should allow the voter to unvote a model" do
|
92
|
+
voter.vote_up_for votable
|
93
|
+
voter.unvote_for votable
|
94
|
+
votable.find_votes_for.size.should == 0
|
95
|
+
votable.votes_for.count.should == 0
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should get all of the voters votes" do
|
99
|
+
voter.vote_up_for votable
|
100
|
+
voter.find_votes.size.should == 1
|
101
|
+
voter.votes.up.count.should == 1
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should get all of the voters up votes" do
|
105
|
+
voter.vote_up_for votable
|
106
|
+
voter.find_up_votes.size.should == 1
|
107
|
+
voter.votes.up.count.should == 1
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should get all of the voters down votes" do
|
111
|
+
voter.vote_down_for votable
|
112
|
+
voter.find_down_votes.size.should == 1
|
113
|
+
voter.votes.down.count.should == 1
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should get all of the votes votes for a class" do
|
117
|
+
votable.vote_by :voter => voter
|
118
|
+
votable2.vote_by :voter => voter, :vote => false
|
119
|
+
voter.find_votes_for_class(votable_klass).size.should == 2
|
120
|
+
voter.votes.for_type(votable_klass).count.should == 2
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should get all of the voters up votes for a class" do
|
124
|
+
votable.vote_by :voter => voter
|
125
|
+
votable2.vote_by :voter => voter, :vote => false
|
126
|
+
voter.find_up_votes_for_class(votable_klass).size.should == 1
|
127
|
+
voter.votes.up.for_type(votable_klass).count.should == 1
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should get all of the voters down votes for a class" do
|
131
|
+
votable.vote_by :voter => voter
|
132
|
+
votable2.vote_by :voter => voter, :vote => false
|
133
|
+
voter.find_down_votes_for_class(votable_klass).size.should == 1
|
134
|
+
voter.votes.down.for_type(votable_klass).count.should == 1
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should be contained to instances" do
|
138
|
+
voter.vote :votable => votable, :vote => false
|
139
|
+
voter2.vote :votable => votable
|
140
|
+
|
141
|
+
voter.voted_as_when_voting_on(votable).should be false
|
142
|
+
end
|
143
|
+
|
144
|
+
describe '#find_voted_items' do
|
145
|
+
it 'returns objects that a user has upvoted for' do
|
146
|
+
votable.vote_by :voter => voter
|
147
|
+
votable2.vote_by :voter => voter2
|
148
|
+
voter.find_voted_items.should include votable
|
149
|
+
voter.find_voted_items.size.should == 1
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'returns objects that a user has upvoted for, using scope' do
|
153
|
+
votable.vote_by :voter => voter, :vote_scope => 'rank'
|
154
|
+
votable2.vote_by :voter => voter2, :vote_scope => 'rank'
|
155
|
+
voter.find_voted_items(:vote_scope => 'rank').should include votable
|
156
|
+
voter.find_voted_items(:vote_scope => 'rank').size.should == 1
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'returns objects that a user has downvoted for' do
|
160
|
+
votable.vote_down voter
|
161
|
+
votable2.vote_down voter2
|
162
|
+
voter.find_voted_items.should include votable
|
163
|
+
voter.find_voted_items.size.should == 1
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'returns objects that a user has downvoted for, using scope' do
|
167
|
+
votable.vote_down voter, :vote_scope => 'rank'
|
168
|
+
votable2.vote_down voter2, :vote_scope => 'rank'
|
169
|
+
voter.find_voted_items(:vote_scope => 'rank').should include votable
|
170
|
+
voter.find_voted_items(:vote_scope => 'rank').size.should == 1
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
describe '#find_up_voted_items' do
|
175
|
+
it 'returns objects that a user has upvoted for' do
|
176
|
+
votable.vote_by :voter => voter
|
177
|
+
votable2.vote_by :voter => voter2
|
178
|
+
voter.find_up_voted_items.should include votable
|
179
|
+
voter.find_up_voted_items.size.should == 1
|
180
|
+
voter.find_liked_items.should include votable
|
181
|
+
voter.find_liked_items.size.should == 1
|
182
|
+
end
|
183
|
+
|
184
|
+
it 'returns objects that a user has upvoted for, using scope' do
|
185
|
+
votable.vote_by :voter => voter, :vote_scope => 'rank'
|
186
|
+
votable2.vote_by :voter => voter2, :vote_scope => 'rank'
|
187
|
+
voter.find_up_voted_items(:vote_scope => 'rank').should include votable
|
188
|
+
voter.find_up_voted_items(:vote_scope => 'rank').size.should == 1
|
189
|
+
end
|
190
|
+
|
191
|
+
it 'does not return objects that a user has downvoted for' do
|
192
|
+
votable.vote_down voter
|
193
|
+
voter.find_up_voted_items.size.should == 0
|
194
|
+
end
|
195
|
+
|
196
|
+
it 'does not return objects that a user has downvoted for, using scope' do
|
197
|
+
votable.vote_down voter, :vote_scope => 'rank'
|
198
|
+
voter.find_up_voted_items(:vote_scope => 'rank').size.should == 0
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
describe '#find_down_voted_items' do
|
203
|
+
it 'does not return objects that a user has upvoted for' do
|
204
|
+
votable.vote_by :voter => voter
|
205
|
+
voter.find_down_voted_items.size.should == 0
|
206
|
+
end
|
207
|
+
|
208
|
+
it 'does not return objects that a user has upvoted for, using scope' do
|
209
|
+
votable.vote_by :voter => voter, :vote_scope => 'rank'
|
210
|
+
voter.find_down_voted_items(:vote_scope => 'rank').size.should == 0
|
211
|
+
end
|
212
|
+
|
213
|
+
it 'returns objects that a user has downvoted for' do
|
214
|
+
votable.vote_down voter
|
215
|
+
votable2.vote_down voter2
|
216
|
+
voter.find_down_voted_items.should include votable
|
217
|
+
voter.find_down_voted_items.size.should == 1
|
218
|
+
voter.find_disliked_items.should include votable
|
219
|
+
voter.find_disliked_items.size.should == 1
|
220
|
+
end
|
221
|
+
|
222
|
+
it 'returns objects that a user has downvoted for, using scope' do
|
223
|
+
votable.vote_down voter, :vote_scope => 'rank'
|
224
|
+
votable2.vote_down voter2, :vote_scope => 'rank'
|
225
|
+
voter.find_down_voted_items(:vote_scope => 'rank').should include votable
|
226
|
+
voter.find_down_voted_items(:vote_scope => 'rank').size.should == 1
|
227
|
+
end
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
describe '#get_voted' do
|
232
|
+
subject { voter.get_voted(votable.class) }
|
233
|
+
|
234
|
+
it 'returns objects of a class that a voter has voted for' do
|
235
|
+
votable.vote_by :voter => voter
|
236
|
+
votable2.vote_down voter
|
237
|
+
subject.should include votable
|
238
|
+
subject.should include votable2
|
239
|
+
subject.size.should == 2
|
240
|
+
end
|
241
|
+
|
242
|
+
it 'does not return objects of a class that a voter has voted for' do
|
243
|
+
votable.vote_by :voter => voter2
|
244
|
+
votable2.vote_by :voter => voter2
|
245
|
+
subject.size.should == 0
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
describe '#get_up_voted' do
|
250
|
+
subject { voter.get_up_voted(votable.class) }
|
251
|
+
|
252
|
+
it 'returns up voted items that a voter has voted for' do
|
253
|
+
votable.vote_by :voter => voter
|
254
|
+
subject.should include votable
|
255
|
+
subject.size.should == 1
|
256
|
+
end
|
257
|
+
|
258
|
+
it 'does not return down voted items a voter has voted for' do
|
259
|
+
votable.vote_down voter
|
260
|
+
subject.size.should == 0
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
describe '#get_down_voted' do
|
265
|
+
subject { voter.get_down_voted(votable.class) }
|
266
|
+
|
267
|
+
it 'does not return up voted items that a voter has voted for' do
|
268
|
+
votable.vote_by :voter => voter
|
269
|
+
subject.size.should == 0
|
270
|
+
end
|
271
|
+
|
272
|
+
it 'returns down voted items a voter has voted for' do
|
273
|
+
votable.vote_down voter
|
274
|
+
subject.should include votable
|
275
|
+
subject.size.should == 1
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
end
|