acts_as_votable 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,8 @@ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
2
  require 'sqlite3'
3
3
  require 'acts_as_votable'
4
4
 
5
+ Dir["./spec/support/**/*.rb"].sort.each {|f| require f}
6
+
5
7
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
6
8
 
7
9
  ActiveRecord::Schema.define(:version => 1) do
@@ -33,6 +35,10 @@ ActiveRecord::Schema.define(:version => 1) do
33
35
  t.string :name
34
36
  end
35
37
 
38
+ create_table :votable_voters do |t|
39
+ t.string :name
40
+ end
41
+
36
42
  create_table :sti_votables do |t|
37
43
  t.string :name
38
44
  t.string :type
@@ -78,6 +84,11 @@ class Votable < ActiveRecord::Base
78
84
  validates_presence_of :name
79
85
  end
80
86
 
87
+ class VotableVoter < ActiveRecord::Base
88
+ acts_as_votable
89
+ acts_as_voter
90
+ end
91
+
81
92
  class StiVotable < ActiveRecord::Base
82
93
  acts_as_votable
83
94
  end
@@ -2,11 +2,6 @@ require 'acts_as_votable'
2
2
  require 'spec_helper'
3
3
 
4
4
  describe ActsAsVotable::Votable do
5
-
6
- before(:each) do
7
- clean_database
8
- end
9
-
10
5
  it "should not be votable" do
11
6
  NotVotable.should_not be_votable
12
7
  end
@@ -15,420 +10,11 @@ describe ActsAsVotable::Votable do
15
10
  Votable.should be_votable
16
11
  end
17
12
 
18
- describe "voting on a votable object" do
19
-
20
- before(:each) do
21
- clean_database
22
- @voter = Voter.new(:name => 'i can vote!')
23
- @voter.save
24
-
25
- @voter2 = Voter.new(:name => 'a new person')
26
- @voter2.save
27
-
28
- @votable = Votable.new(:name => 'a voting model')
29
- @votable.save
30
- end
31
-
32
- it "should return false when a vote with no voter is saved" do
33
- @votable.vote.should be false
34
- end
35
-
36
- it "should have one vote when saved" do
37
- @votable.vote :voter => @voter, :vote => 'yes'
38
- @votable.votes.size.should == 1
39
- end
40
-
41
- it "should have one vote when voted on twice by the same person" do
42
- @votable.vote :voter => @voter, :vote => 'yes'
43
- @votable.vote :voter => @voter, :vote => 'no'
44
- @votable.votes.size.should == 1
45
- end
46
-
47
- it "should have two votes when voted on twice by the same person with duplicate paramenter" do
48
- @votable.vote :voter => @voter, :vote => 'yes'
49
- @votable.vote :voter => @voter, :vote => 'no', :duplicate => true
50
- @votable.votes.size.should == 2
51
- end
52
-
53
- it "should have one scoped vote when voting under an scope" do
54
- @votable.vote :voter => @voter, :vote => 'yes', :vote_scope => 'rank'
55
- @votable.find_votes(:vote_scope => 'rank').size.should == 1
56
- end
57
-
58
- it "should have one vote when voted on twice using scope by the same person" do
59
- @votable.vote :voter => @voter, :vote => 'yes', :vote_scope => 'rank'
60
- @votable.vote :voter => @voter, :vote => 'no', :vote_scope => 'rank'
61
- @votable.find_votes(:vote_scope => 'rank').size.should == 1
62
- end
63
-
64
- it "should have two votes when voting on two different scopes by the same person" do
65
- @votable.vote :voter => @voter, :vote => 'yes', :vote_scope => 'weekly_rank'
66
- @votable.vote :voter => @voter, :vote => 'no', :vote_scope => 'monthly_rank'
67
- @votable.votes.size.should == 2
68
- end
69
-
70
- it "should be callable with vote_up" do
71
- @votable.vote_up @voter
72
- @votable.up_votes.first.voter.should == @voter
73
- end
74
-
75
- it "should be callable with vote_down" do
76
- @votable.vote_down @voter
77
- @votable.down_votes.first.voter.should == @voter
78
- end
79
-
80
- it "should have 2 votes when voted on once by two different people" do
81
- @votable.vote :voter => @voter
82
- @votable.vote :voter => @voter2
83
- @votable.votes.size.should == 2
84
- end
85
-
86
- it "should have one true vote" do
87
- @votable.vote :voter => @voter
88
- @votable.vote :voter => @voter2, :vote => 'dislike'
89
- @votable.up_votes.size.should == 1
90
- end
91
-
92
- it "should have 2 false votes" do
93
- @votable.vote :voter => @voter, :vote => 'no'
94
- @votable.vote :voter => @voter2, :vote => 'dislike'
95
- @votable.down_votes.size.should == 2
96
- end
97
-
98
- it "should have been voted on by voter2" do
99
- @votable.vote :voter => @voter2, :vote => true
100
- @votable.find_votes.first.voter.id.should be @voter2.id
101
- end
102
-
103
- it "should count the vote as registered if this is the voters first vote" do
104
- @votable.vote :voter => @voter
105
- @votable.vote_registered?.should be true
106
- end
107
-
108
- it "should not count the vote as being registered if that voter has already voted and the vote has not changed" do
109
- @votable.vote :voter => @voter, :vote => true
110
- @votable.vote :voter => @voter, :vote => 'yes'
111
- @votable.vote_registered?.should be false
112
- end
113
-
114
- it "should count the vote as registered if the voter has voted and the vote has changed" do
115
- @votable.vote :voter => @voter, :vote => true
116
- @votable.vote :voter => @voter, :vote => 'dislike'
117
- @votable.vote_registered?.should be true
118
- end
119
-
120
- it "should be voted on by voter" do
121
- @votable.vote :voter => @voter
122
- @votable.voted_on_by?(@voter).should be true
123
- end
124
-
125
- it "should be able to unvote a voter" do
126
- @votable.liked_by(@voter)
127
- @votable.unliked_by(@voter)
128
- @votable.voted_on_by?(@voter).should be false
129
- end
130
-
131
- it "should unvote a positive vote" do
132
- @votable.vote :voter => @voter
133
- @votable.unvote :voter => @voter
134
- @votable.find_votes.count.should == 0
135
- end
136
-
137
- it "should set the votable to unregistered after unvoting" do
138
- @votable.vote :voter => @voter
139
- @votable.unvote :voter => @voter
140
- @votable.vote_registered?.should be false
141
- end
142
-
143
- it "should unvote a negative vote" do
144
- @votable.vote :voter => @voter, :vote => 'no'
145
- @votable.unvote :voter => @voter
146
- @votable.find_votes.count.should == 0
147
- end
148
-
149
- it "should unvote only the from a single voter" do
150
- @votable.vote :voter => @voter
151
- @votable.vote :voter => @voter2
152
- @votable.unvote :voter => @voter
153
- @votable.find_votes.count.should == 1
154
- end
155
-
156
- it "should be contained to instances" do
157
- votable2 = Votable.new(:name => '2nd votable')
158
- votable2.save
159
-
160
- @votable.vote :voter => @voter, :vote => false
161
- votable2.vote :voter => @voter, :vote => true
162
- votable2.vote :voter => @voter, :vote => true
163
-
164
- @votable.vote_registered?.should be true
165
- votable2.vote_registered?.should be false
166
- end
167
-
168
- describe "with cached votes" do
169
-
170
- before(:each) do
171
- clean_database
172
- @voter = Voter.new(:name => 'i can vote!')
173
- @voter.save
174
-
175
- @votable = Votable.new(:name => 'a voting model without a cache')
176
- @votable.save
177
-
178
- @votable_cache = VotableCache.new(:name => 'voting model with cache')
179
- @votable_cache.save
180
- end
181
-
182
- it "should not update cached votes if there are no columns" do
183
- @votable.vote :voter => @voter
184
- end
185
-
186
- it "should update cached total votes if there is a total column" do
187
- @votable_cache.cached_votes_total = 50
188
- @votable_cache.vote :voter => @voter
189
- @votable_cache.cached_votes_total.should == 1
190
- end
191
-
192
- it "should update cached total votes when a vote up is removed" do
193
- @votable_cache.vote :voter => @voter, :vote => 'true'
194
- @votable_cache.unvote :voter => @voter
195
- @votable_cache.cached_votes_total.should == 0
196
- end
197
-
198
- it "should update cached total votes when a vote down is removed" do
199
- @votable_cache.vote :voter => @voter, :vote => 'false'
200
- @votable_cache.unvote :voter => @voter
201
- @votable_cache.cached_votes_total.should == 0
202
- end
203
-
204
- it "should update cached score votes if there is a score column" do
205
- @votable_cache.cached_votes_score = 50
206
- @votable_cache.vote :voter => @voter
207
- @votable_cache.cached_votes_score.should == 1
208
- @votable_cache.vote :voter => @voter2, :vote => 'false'
209
- @votable_cache.cached_votes_score.should == 0
210
- @votable_cache.vote :voter => @voter, :vote => 'false'
211
- @votable_cache.cached_votes_score.should == -2
212
- end
213
-
214
- it "should update cached score votes when a vote up is removed" do
215
- @votable_cache.vote :voter => @voter, :vote => 'true'
216
- @votable_cache.cached_votes_score.should == 1
217
- @votable_cache.unvote :voter => @voter
218
- @votable_cache.cached_votes_score.should == 0
219
- end
220
-
221
- it "should update cached score votes when a vote down is removed" do
222
- @votable_cache.vote :voter => @voter, :vote => 'false'
223
- @votable_cache.cached_votes_score.should == -1
224
- @votable_cache.unvote :voter => @voter
225
- @votable_cache.cached_votes_score.should == 0
226
- end
227
-
228
- it "should update cached weighted score if there is a weighted score column" do
229
- @votable_cache.cached_weighted_score = 50
230
- @votable_cache.vote :voter => @voter
231
- @votable_cache.cached_weighted_score.should == 1
232
- @votable_cache.vote :voter => @voter2, :vote => 'false'
233
- @votable_cache.cached_weighted_score.should == 0
234
- @votable_cache.vote :voter => @voter, :vote => 'false'
235
- @votable_cache.cached_weighted_score.should == -2
236
- end
237
-
238
- it "should update cached weighted score votes when a vote up is removed" do
239
- @votable_cache.vote :voter => @voter, :vote => 'true', :vote_weight => 3
240
- @votable_cache.cached_weighted_score.should == 3
241
- @votable_cache.unvote :voter => @voter
242
- @votable_cache.cached_weighted_score.should == 0
243
- end
244
-
245
- it "should update cached weighted score votes when a vote down is removed" do
246
- @votable_cache.vote :voter => @voter, :vote => 'false', :vote_weight => 4
247
- @votable_cache.cached_weighted_score.should == -4
248
- @votable_cache.unvote :voter => @voter
249
- @votable_cache.cached_weighted_score.should == 0
250
- end
251
-
252
- it "should update cached up votes if there is an up vote column" do
253
- @votable_cache.cached_votes_up = 50
254
- @votable_cache.vote :voter => @voter
255
- @votable_cache.vote :voter => @voter
256
- @votable_cache.cached_votes_up.should == 1
257
- end
258
-
259
- it "should update cached down votes if there is a down vote column" do
260
- @votable_cache.cached_votes_down = 50
261
- @votable_cache.vote :voter => @voter, :vote => 'false'
262
- @votable_cache.cached_votes_down.should == 1
263
- end
264
-
265
- it "should update cached up votes when a vote up is removed" do
266
- @votable_cache.vote :voter => @voter, :vote => 'true'
267
- @votable_cache.unvote :voter => @voter
268
- @votable_cache.cached_votes_up.should == 0
269
- end
270
-
271
- it "should update cached down votes when a vote down is removed" do
272
- @votable_cache.vote :voter => @voter, :vote => 'false'
273
- @votable_cache.unvote :voter => @voter
274
- @votable_cache.cached_votes_down.should == 0
275
- end
276
-
277
- it "should select from cached total votes if there a total column" do
278
- @votable_cache.vote :voter => @voter
279
- @votable_cache.cached_votes_total = 50
280
- @votable_cache.count_votes_total.should == 50
281
- end
282
-
283
- it "should select from cached up votes if there is an up vote column" do
284
- @votable_cache.vote :voter => @voter
285
- @votable_cache.cached_votes_up = 50
286
- @votable_cache.count_votes_up.should == 50
287
- end
288
-
289
- it "should select from cached down votes if there is a down vote column" do
290
- @votable_cache.vote :voter => @voter, :vote => 'false'
291
- @votable_cache.cached_votes_down = 50
292
- @votable_cache.count_votes_down.should == 50
293
- end
294
-
295
- it "should select from cached weighted score if there is a weighted score column" do
296
- @votable_cache.vote :voter => @voter, :vote => 'false'
297
- @votable_cache.cached_weighted_score = 50
298
- @votable_cache.weighted_score.should == 50
299
- end
300
-
301
- end
302
-
303
- describe "with scoped cached votes" do
304
-
305
- before(:each) do
306
- clean_database
307
- @voter = Voter.new(:name => 'i can vote!')
308
- @voter.save
309
-
310
- @votable = Votable.new(:name => 'a voting model without a cache')
311
- @votable.save
312
-
313
- @votable_cache = VotableCache.new(:name => 'voting model with cache')
314
- @votable_cache.save
315
- end
316
-
317
- it "should update cached total votes if there is a total column" do
318
- @votable_cache.cached_scoped_test_votes_total = 50
319
- @votable_cache.vote :voter => @voter, :vote_scope => "test"
320
- @votable_cache.cached_scoped_test_votes_total.should == 1
321
- end
322
-
323
- it "should update cached total votes when a vote up is removed" do
324
- @votable_cache.vote :voter => @voter, :vote => 'true', :vote_scope => "test"
325
- @votable_cache.unvote :voter => @voter, :vote_scope => "test"
326
- @votable_cache.cached_scoped_test_votes_total.should == 0
327
- end
328
-
329
- it "should update cached total votes when a vote down is removed" do
330
- @votable_cache.vote :voter => @voter, :vote => 'false', :vote_scope => "test"
331
- @votable_cache.unvote :voter => @voter, :vote_scope => "test"
332
- @votable_cache.cached_scoped_test_votes_total.should == 0
333
- end
334
-
335
- it "should update cached score votes if there is a score column" do
336
- @votable_cache.cached_scoped_test_votes_score = 50
337
- @votable_cache.vote :voter => @voter, :vote_scope => "test"
338
- @votable_cache.cached_scoped_test_votes_score.should == 1
339
- @votable_cache.vote :voter => @voter2, :vote => 'false', :vote_scope => "test"
340
- @votable_cache.cached_scoped_test_votes_score.should == 0
341
- @votable_cache.vote :voter => @voter, :vote => 'false', :vote_scope => "test"
342
- @votable_cache.cached_scoped_test_votes_score.should == -2
343
- end
344
-
345
- it "should update cached score votes when a vote up is removed" do
346
- @votable_cache.vote :voter => @voter, :vote => 'true', :vote_scope => "test"
347
- @votable_cache.cached_scoped_test_votes_score.should == 1
348
- @votable_cache.unvote :voter => @voter, :vote_scope => "test"
349
- @votable_cache.cached_scoped_test_votes_score.should == 0
350
- end
351
-
352
- it "should update cached score votes when a vote down is removed" do
353
- @votable_cache.vote :voter => @voter, :vote => 'false', :vote_scope => "test"
354
- @votable_cache.cached_scoped_test_votes_score.should == -1
355
- @votable_cache.unvote :voter => @voter, :vote_scope => "test"
356
- @votable_cache.cached_scoped_test_votes_score.should == 0
357
- end
358
-
359
- it "should update cached up votes if there is an up vote column" do
360
- @votable_cache.cached_scoped_test_votes_up = 50
361
- @votable_cache.vote :voter => @voter, :vote_scope => "test"
362
- @votable_cache.vote :voter => @voter, :vote_scope => "test"
363
- @votable_cache.cached_scoped_test_votes_up.should == 1
364
- end
365
-
366
- it "should update cached down votes if there is a down vote column" do
367
- @votable_cache.cached_scoped_test_votes_down = 50
368
- @votable_cache.vote :voter => @voter, :vote => 'false', :vote_scope => "test"
369
- @votable_cache.cached_scoped_test_votes_down.should == 1
370
- end
371
-
372
- it "should update cached up votes when a vote up is removed" do
373
- @votable_cache.vote :voter => @voter, :vote => 'true', :vote_scope => "test"
374
- @votable_cache.unvote :voter => @voter, :vote_scope => "test"
375
- @votable_cache.cached_scoped_test_votes_up.should == 0
376
- end
377
-
378
- it "should update cached down votes when a vote down is removed" do
379
- @votable_cache.vote :voter => @voter, :vote => 'false', :vote_scope => "test"
380
- @votable_cache.unvote :voter => @voter, :vote_scope => "test"
381
- @votable_cache.cached_scoped_test_votes_down.should == 0
382
- end
383
-
384
- it "should select from cached total votes if there a total column" do
385
- @votable_cache.vote :voter => @voter, :vote_scope => "test"
386
- @votable_cache.cached_scoped_test_votes_total = 50
387
- @votable_cache.count_votes_total(false, "test").should == 50
388
- end
389
-
390
- it "should select from cached up votes if there is an up vote column" do
391
- @votable_cache.vote :voter => @voter, :vote_scope => "test"
392
- @votable_cache.cached_scoped_test_votes_up = 50
393
- @votable_cache.count_votes_up(false, "test").should == 50
394
- end
395
-
396
- it "should select from cached down votes if there is a down vote column" do
397
- @votable_cache.vote :voter => @voter, :vote => 'false', :vote_scope => "test"
398
- @votable_cache.cached_scoped_test_votes_down = 50
399
- @votable_cache.count_votes_down(false, "test").should == 50
400
- end
401
-
402
- end
403
-
404
- describe "sti models" do
405
-
406
- before(:each) do
407
- clean_database
408
- @voter = Voter.create(:name => 'i can vote!')
409
- end
410
-
411
- it "should be able to vote on a votable child of a non votable sti model" do
412
- votable = VotableChildOfStiNotVotable.create(:name => 'sti child')
413
-
414
- votable.vote :voter => @voter, :vote => 'yes'
415
- votable.votes.size.should == 1
416
- end
417
-
418
- it "should not be able to vote on a parent non votable" do
419
- StiNotVotable.should_not be_votable
420
- end
421
-
422
- it "should be able to vote on a child when its parent is votable" do
423
- votable = ChildOfStiVotable.create(:name => 'sti child')
424
-
425
- votable.vote :voter => @voter, :vote => 'yes'
426
- votable.votes.size.should == 1
427
- end
428
-
429
- end
430
-
13
+ it_behaves_like "a votable_model" do
14
+ # TODO Replace with factories
15
+ let (:voter) { Voter.create(:name =>'i can vote!') }
16
+ let (:voter2) { Voter.create(:name => 'a new person') }
17
+ let (:votable) { Votable.create(:name =>'a voting model') }
18
+ let (:votable_cache) { VotableCache.create(:name => 'voting model with cache') }
431
19
  end
432
-
433
-
434
20
  end