bullet 2.0.0.beta.2 → 2.1.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.
Files changed (49) hide show
  1. data/.gitignore +9 -0
  2. data/.rspec +2 -0
  3. data/.rvmrc +2 -0
  4. data/.rvmrc.example +2 -0
  5. data/.watchr +24 -0
  6. data/Gemfile +15 -0
  7. data/Gemfile.lock +105 -0
  8. data/Hacking.textile +69 -0
  9. data/MIT-LICENSE +1 -1
  10. data/README.textile +61 -49
  11. data/README_for_rails2.textile +41 -42
  12. data/Rakefile +40 -29
  13. data/bullet.gemspec +17 -60
  14. data/lib/bullet/action_controller2.rb +6 -6
  15. data/lib/bullet/active_record2.rb +17 -16
  16. data/lib/bullet/active_record3.rb +17 -25
  17. data/lib/bullet/active_record31.rb +96 -0
  18. data/lib/bullet/detector/association.rb +144 -0
  19. data/lib/bullet/detector/base.rb +12 -0
  20. data/lib/bullet/detector/counter.rb +43 -0
  21. data/lib/bullet/detector/n_plus_one_query.rb +39 -0
  22. data/lib/bullet/detector/unused_eager_association.rb +41 -0
  23. data/lib/bullet/detector.rb +9 -0
  24. data/lib/bullet/notification/base.rb +61 -0
  25. data/lib/bullet/notification/counter_cache.rb +13 -0
  26. data/lib/bullet/notification/n_plus_one_query.rb +32 -0
  27. data/lib/bullet/notification/unused_eager_loading.rb +14 -0
  28. data/lib/bullet/notification.rb +4 -79
  29. data/lib/bullet/notification_collector.rb +25 -0
  30. data/lib/bullet/rack.rb +44 -0
  31. data/lib/bullet/registry/association.rb +15 -0
  32. data/lib/bullet/registry/base.rb +37 -0
  33. data/lib/bullet/registry/object.rb +15 -0
  34. data/lib/bullet/registry.rb +7 -0
  35. data/lib/bullet/version.rb +5 -0
  36. data/lib/bullet.rb +64 -43
  37. data/perf/benchmark.rb +106 -0
  38. data/spec/bullet/association_for_chris_spec.rb +6 -6
  39. data/spec/bullet/association_for_peschkaj_spec.rb +6 -6
  40. data/spec/bullet/association_spec.rb +140 -294
  41. data/spec/bullet/counter_spec.rb +10 -10
  42. data/spec/spec_helper.rb +51 -17
  43. metadata +72 -57
  44. data/VERSION +0 -1
  45. data/lib/bullet/association.rb +0 -294
  46. data/lib/bullet/counter.rb +0 -101
  47. data/lib/bullet/logger.rb +0 -9
  48. data/lib/bulletware.rb +0 -42
  49. data/spec/spec.opts +0 -3
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
4
4
 
5
- describe Bullet::Association, 'has_many' do
5
+ describe Bullet::Detector::Association, 'has_many' do
6
6
 
7
7
  def setup_db
8
8
  ActiveRecord::Schema.define(:version => 1) do
@@ -75,7 +75,7 @@ describe Bullet::Association, 'has_many' do
75
75
  has_many :posts
76
76
  belongs_to :newspaper
77
77
  end
78
-
78
+
79
79
  class Newspaper < ActiveRecord::Base
80
80
  has_many :writers, :class_name => "BaseUser"
81
81
  end
@@ -85,7 +85,7 @@ describe Bullet::Association, 'has_many' do
85
85
 
86
86
  before(:all) do
87
87
  setup_db
88
-
88
+
89
89
  newspaper1 = Newspaper.create(:name => "First Newspaper")
90
90
  newspaper2 = Newspaper.create(:name => "Second Newspaper")
91
91
 
@@ -121,15 +121,14 @@ describe Bullet::Association, 'has_many' do
121
121
  end
122
122
 
123
123
  before(:each) do
124
- Bullet::Association.start_request
124
+ Bullet.start_request
125
125
  end
126
126
 
127
127
  after(:each) do
128
- Bullet::Association.end_request
128
+ Bullet.end_request
129
129
  end
130
130
 
131
- # FIXME: setup and teardown are not inherited by context
132
- # context "for unused cases" do
131
+ context "for unused cases" do
133
132
  #If you have the same record created twice with different includes
134
133
  # the hash value get's accumulated includes, which leads to false Unused eager loading
135
134
  #it "should not incorrectly mark associations as unused when multiple object instances" do
@@ -159,14 +158,13 @@ describe Bullet::Association, 'has_many' do
159
158
  comments_with_author = Comment.includes(:author)
160
159
  comment_collection = comments_with_author.limit(2)
161
160
  comment_collection.collect { |com| com.author.name }
162
- Bullet::Association.check_unused_preload_associations
163
- Bullet::Association.should_not be_unused_preload_associations_for(Comment, :author)
161
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
162
+ Bullet::Detector::Association.should_not be_unused_preload_associations_for(Comment, :author)
164
163
  end
165
- # end
164
+ end
166
165
 
167
166
 
168
- # FIXME: setup and teardown are not inherited by context
169
- # context "comments => posts => category" do
167
+ context "comments => posts => category" do
170
168
 
171
169
  # this happens because the post isn't a possible object even though the writer is access through the post
172
170
  # which leads to an 1+N queries
@@ -174,7 +172,7 @@ describe Bullet::Association, 'has_many' do
174
172
  Comment.includes([:author, :post]).where(["base_users.id = ?", BaseUser.first]).each do |com|
175
173
  com.post.writer.name
176
174
  end
177
- Bullet::Association.should be_detecting_unpreloaded_association_for(Post, :writer)
175
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Post, :writer)
178
176
  end
179
177
 
180
178
  # this happens because the comment doesn't break down the hash into keys
@@ -183,17 +181,17 @@ describe Bullet::Association, 'has_many' do
183
181
  comments = Comment.includes([:author, {:post => :writer}]).where(["base_users.id = ?", BaseUser.first]).each do |com|
184
182
  com.post.writer.name
185
183
  end
186
- Bullet::Association.should_not be_detecting_unpreloaded_association_for(Comment, :post)
187
- Bullet::Association.should be_completely_preloading_associations
184
+ Bullet::Detector::Association.should_not be_detecting_unpreloaded_association_for(Comment, :post)
185
+ Bullet::Detector::Association.should be_completely_preloading_associations
188
186
  end
189
187
 
190
188
  it "should detect preload of post => writer" do
191
189
  comments = Comment.includes([:author, {:post => :writer}]).where(["base_users.id = ?", BaseUser.first]).each do |com|
192
190
  com.post.writer.name
193
191
  end
194
- Bullet::Association.should be_creating_object_association_for(comments.first, :author)
195
- Bullet::Association.should_not be_detecting_unpreloaded_association_for(Post, :writer)
196
- Bullet::Association.should be_completely_preloading_associations
192
+ Bullet::Detector::Association.should be_creating_object_association_for(comments.first, :author)
193
+ Bullet::Detector::Association.should_not be_detecting_unpreloaded_association_for(Post, :writer)
194
+ Bullet::Detector::Association.should be_completely_preloading_associations
197
195
  end
198
196
 
199
197
  # To flyerhzm: This does not detect that newspaper is unpreloaded. The association is
@@ -202,7 +200,7 @@ describe Bullet::Association, 'has_many' do
202
200
  comments = Comment.all(:include => {:post => :writer}, :conditions => "posts.name like '%first%'").each do |com|
203
201
  com.post.writer.newspaper.name
204
202
  end
205
- Bullet::Association.should be_detecting_unpreloaded_association_for(Writer, :newspaper)
203
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Writer, :newspaper)
206
204
  end
207
205
 
208
206
  # when we attempt to access category, there is an infinite overflow because load_target is hijacked leading to
@@ -214,63 +212,66 @@ describe Bullet::Association, 'has_many' do
214
212
  end
215
213
  }.should_not raise_error(SystemStackError)
216
214
  end
217
- # end
215
+ end
218
216
 
219
- # FIXME: setup and teardown are not inherited by context
220
- # context "post => comments" do
217
+ context "post => comments" do
218
+ #
219
+ ### FIXME: Please double check semantic equivalence with original
221
220
  it "should detect preload with post => comments" do
222
221
  Post.includes(:comments).each do |post|
223
222
  post.comments.collect(&:name)
224
223
  end
225
- Bullet::Association.should_not be_has_unpreload_associations
224
+ # Bullet::Detector::Association.should_not be_has_unpreload_associations
225
+ Bullet::Detector::Association.should be_completely_preloading_associations
226
226
  end
227
227
 
228
228
  it "should detect no preload post => comments" do
229
229
  Post.all.each do |post|
230
230
  post.comments.collect(&:name)
231
231
  end
232
- Bullet::Association.should be_has_unpreload_associations
232
+ # Bullet::Detector::Association.should be_has_unpreload_associations
233
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
233
234
  end
234
235
 
235
236
  it "should detect unused preload post => comments for post" do
236
237
  Post.includes(:comments).collect(&:name)
237
- Bullet::Association.check_unused_preload_associations
238
- Bullet::Association.should be_has_unused_preload_associations
238
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
239
+ Bullet::Detector::Association.should be_has_unused_preload_associations
239
240
  end
240
241
 
241
242
  it "should detect no unused preload post => comments for post" do
242
243
  Post.all.collect(&:name)
243
- Bullet::Association.check_unused_preload_associations
244
- Bullet::Association.should_not be_has_unused_preload_associations
244
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
245
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
245
246
  end
246
247
 
247
248
  it "should detect no unused preload post => comments for comment" do
248
249
  Post.all.each do |post|
249
250
  post.comments.collect(&:name)
250
251
  end
251
- Bullet::Association.check_unused_preload_associations
252
- Bullet::Association.should_not be_has_unused_preload_associations
252
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
253
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
253
254
 
254
- Bullet::Association.end_request
255
- Bullet::Association.start_request
255
+ Bullet.end_request
256
+ Bullet.start_request
256
257
 
257
258
  Post.all.each do |post|
258
259
  post.comments.collect(&:name)
259
260
  end
260
- Bullet::Association.check_unused_preload_associations
261
- Bullet::Association.should_not be_has_unused_preload_associations
261
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
262
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
262
263
  end
263
- # end
264
+ end
264
265
 
265
- # FIXME: setup and teardown are not inherited by context
266
- # context "category => posts => comments" do
266
+ context "category => posts => comments" do
267
267
  it "should detect preload with category => posts => comments" do
268
268
  Category.includes({:posts => :comments}).each do |category|
269
269
  category.posts.each do |post|
270
270
  post.comments.collect(&:name)
271
271
  end
272
272
  end
273
- Bullet::Association.should_not be_has_unpreload_associations
273
+ # Bullet::Detector::Association.should_not be_has_unpreload_associations
274
+ Bullet::Detector::Association.should be_completely_preloading_associations
274
275
  end
275
276
 
276
277
  it "should detect preload category => posts, but no post => comments" do
@@ -279,7 +280,8 @@ describe Bullet::Association, 'has_many' do
279
280
  post.comments.collect(&:name)
280
281
  end
281
282
  end
282
- Bullet::Association.should be_has_unpreload_associations
283
+ # Bullet::Detector::Association.should be_has_unpreload_associations
284
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
283
285
  end
284
286
 
285
287
  it "should detect no preload category => posts => comments" do
@@ -288,21 +290,22 @@ describe Bullet::Association, 'has_many' do
288
290
  post.comments.collect(&:name)
289
291
  end
290
292
  end
291
- Bullet::Association.should be_has_unpreload_associations
293
+ # Bullet::Detector::Association.should be_has_unpreload_associations
294
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
292
295
  end
293
296
 
294
297
  it "should detect unused preload with category => posts => comments" do
295
298
  Category.includes({:posts => :comments}).collect(&:name)
296
- Bullet::Association.check_unused_preload_associations
297
- Bullet::Association.should be_has_unused_preload_associations
299
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
300
+ Bullet::Detector::Association.should be_has_unused_preload_associations
298
301
  end
299
302
 
300
303
  it "should detect unused preload with post => commnets, no category => posts" do
301
304
  Category.includes({:posts => :comments}).each do |category|
302
305
  category.posts.collect(&:name)
303
306
  end
304
- Bullet::Association.check_unused_preload_associations
305
- Bullet::Association.should be_has_unused_preload_associations
307
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
308
+ Bullet::Detector::Association.should be_has_unused_preload_associations
306
309
  end
307
310
 
308
311
  it "should no detect preload with category => posts => comments" do
@@ -311,19 +314,18 @@ describe Bullet::Association, 'has_many' do
311
314
  post.comments.collect(&:name)
312
315
  end
313
316
  end
314
- Bullet::Association.check_unused_preload_associations
315
- Bullet::Association.should_not be_has_unused_preload_associations
317
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
318
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
316
319
  end
317
- # end
320
+ end
318
321
 
319
- # FIXME: setup and teardown are not inherited by context
320
- # context "category => posts, category => entries" do
322
+ context "category => posts, category => entries" do
321
323
  it "should detect preload with category => [posts, entries]" do
322
324
  Category.includes([:posts, :entries]).each do |category|
323
325
  category.posts.collect(&:name)
324
326
  category.entries.collect(&:name)
325
327
  end
326
- Bullet::Association.should_not be_has_unpreload_associations
328
+ Bullet::Detector::Association.should be_completely_preloading_associations
327
329
  end
328
330
 
329
331
  it "should detect preload with category => posts, but no category => entries" do
@@ -331,7 +333,7 @@ describe Bullet::Association, 'has_many' do
331
333
  category.posts.collect(&:name)
332
334
  category.entries.collect(&:name)
333
335
  end
334
- Bullet::Association.should be_has_unpreload_associations
336
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
335
337
  end
336
338
 
337
339
  it "should detect no preload with category => [posts, entries]" do
@@ -339,21 +341,21 @@ describe Bullet::Association, 'has_many' do
339
341
  category.posts.collect(&:name)
340
342
  category.entries.collect(&:name)
341
343
  end
342
- Bullet::Association.should be_has_unpreload_associations
344
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
343
345
  end
344
346
 
345
347
  it "should detect unused with category => [posts, entries]" do
346
348
  Category.includes([:posts, :entries]).collect(&:name)
347
- Bullet::Association.check_unused_preload_associations
348
- Bullet::Association.should be_has_unused_preload_associations
349
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
350
+ Bullet::Detector::Association.should be_has_unused_preload_associations
349
351
  end
350
352
 
351
353
  it "should detect unused preload with category => entries, but no category => posts" do
352
354
  Category.includes([:posts, :entries]).each do |category|
353
355
  category.posts.collect(&:name)
354
356
  end
355
- Bullet::Association.check_unused_preload_associations
356
- Bullet::Association.should be_has_unused_preload_associations
357
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
358
+ Bullet::Detector::Association.should be_has_unused_preload_associations
357
359
  end
358
360
 
359
361
  it "should detect no unused preload" do
@@ -361,62 +363,58 @@ describe Bullet::Association, 'has_many' do
361
363
  category.posts.collect(&:name)
362
364
  category.entries.collect(&:name)
363
365
  end
364
- Bullet::Association.check_unused_preload_associations
365
- Bullet::Association.should_not be_has_unused_preload_associations
366
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
367
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
366
368
  end
367
- # end
369
+ end
368
370
 
369
- # FIXME: setup and teardown are not inherited by context
370
- # context "no preload" do
371
+ context "no preload" do
371
372
  it "should no preload only display only one post => comment" do
372
373
  Post.includes(:comments).each do |post|
373
374
  post.comments.first.name
374
375
  end
375
- Bullet::Association.should_not be_has_unpreload_associations
376
+ Bullet::Detector::Association.should be_completely_preloading_associations
376
377
  end
377
378
 
378
379
  it "should no preload only one post => commnets" do
379
380
  Post.first.comments.collect(&:name)
380
- Bullet::Association.should_not be_has_unpreload_associations
381
+ Bullet::Detector::Association.should be_completely_preloading_associations
381
382
  end
382
- # end
383
+ end
383
384
 
384
- # FIXME: setup and teardown are not inherited by context
385
- # context "scope for_category_name" do
385
+ context "scope for_category_name" do
386
386
  it "should detect preload with post => category" do
387
387
  Post.in_category_name('first').all.each do |post|
388
388
  post.category.name
389
389
  end
390
- Bullet::Association.should_not be_has_unpreload_associations
390
+ Bullet::Detector::Association.should be_completely_preloading_associations
391
391
  end
392
392
 
393
393
  it "should not be unused preload post => category" do
394
394
  Post.in_category_name('first').all.collect(&:name)
395
- Bullet::Association.should_not be_has_unpreload_associations
396
- Bullet::Association.check_unused_preload_associations
397
- Bullet::Association.should_not be_has_unused_preload_associations
395
+ Bullet::Detector::Association.should be_completely_preloading_associations
396
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
397
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
398
398
  end
399
- # end
399
+ end
400
400
 
401
- # FIXME: setup and teardown are not inherited by context
402
- # context "scope preload_posts" do
401
+ context "scope preload_posts" do
403
402
  it "should no preload post => comments with scope" do
404
403
  Post.preload_posts.each do |post|
405
404
  post.comments.collect(&:name)
406
405
  end
407
- Bullet::Association.should_not be_has_unpreload_associations
406
+ Bullet::Detector::Association.should be_completely_preloading_associations
408
407
  end
409
408
 
410
409
  it "should unused preload with scope" do
411
410
  Post.preload_posts.collect(&:name)
412
- Bullet::Association.should_not be_has_unpreload_associations
413
- Bullet::Association.check_unused_preload_associations
414
- Bullet::Association.should be_has_unused_preload_associations
411
+ Bullet::Detector::Association.should be_completely_preloading_associations
412
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
413
+ Bullet::Detector::Association.should be_has_unused_preload_associations
415
414
  end
416
- # end
415
+ end
417
416
 
418
- # FIXME: setup and teardown are not inherited by context
419
- # context "no unused" do
417
+ context "no unused" do
420
418
  it "should no unused only display only one post => comment" do
421
419
  Post.includes(:comments).each do |post|
422
420
  i = 0
@@ -428,63 +426,62 @@ describe Bullet::Association, 'has_many' do
428
426
  end
429
427
  end
430
428
  end
431
- Bullet::Association.check_unused_preload_associations
432
- Bullet::Association.should_not be_has_unused_preload_associations
429
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
430
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
433
431
  end
434
- # end
432
+ end
435
433
 
436
- # FIXME: setup and teardown are not inherited by context
437
- # context "belongs_to" do
434
+ context "belongs_to" do
438
435
  it "should preload comments => post" do
439
436
  Comment.all.each do |comment|
440
437
  comment.post.name
441
438
  end
442
- Bullet::Association.should be_has_unpreload_associations
439
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
443
440
  end
444
441
 
445
442
  it "should no preload comment => post" do
446
443
  Comment.first.post.name
447
- Bullet::Association.should_not be_has_unpreload_associations
444
+ Bullet::Detector::Association.should be_completely_preloading_associations
448
445
  end
449
446
 
450
447
  it "should no preload comments => post" do
451
448
  Comment.includes(:post).each do |comment|
452
449
  comment.post.name
453
450
  end
454
- Bullet::Association.should_not be_has_unpreload_associations
451
+ Bullet::Detector::Association.should be_completely_preloading_associations
455
452
  end
456
453
 
457
454
  it "should detect no unused preload comments => post" do
458
455
  Comment.all.collect(&:name)
459
- Bullet::Association.check_unused_preload_associations
460
- Bullet::Association.should_not be_has_unused_preload_associations
456
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
457
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
461
458
  end
462
459
 
463
460
  it "should detect unused preload comments => post" do
464
461
  Comment.includes(:post).collect(&:name)
465
- Bullet::Association.check_unused_preload_associations
466
- Bullet::Association.should be_has_unused_preload_associations
462
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
463
+ Bullet::Detector::Association.should be_has_unused_preload_associations
467
464
  end
468
465
 
469
466
  it "should dectect no unused preload comments => post" do
470
467
  Comment.all.each do |comment|
471
468
  comment.post.name
472
469
  end
473
- Bullet::Association.check_unused_preload_associations
474
- Bullet::Association.should_not be_has_unused_preload_associations
470
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
471
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
475
472
  end
476
473
 
477
474
  it "should dectect no unused preload comments => post" do
478
475
  Comment.includes(:post).each do |comment|
479
476
  comment.post.name
480
477
  end
481
- Bullet::Association.check_unused_preload_associations
482
- Bullet::Association.should_not be_has_unused_preload_associations
478
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
479
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
483
480
  end
484
- # end
481
+ end
485
482
  end
486
483
 
487
- describe Bullet::Association, 'has_and_belongs_to_many' do
484
+ describe Bullet::Detector::Association, 'has_and_belongs_to_many' do
488
485
 
489
486
  def setup_db
490
487
  ActiveRecord::Schema.define(:version => 1) do
@@ -534,41 +531,41 @@ describe Bullet::Association, 'has_and_belongs_to_many' do
534
531
  end
535
532
 
536
533
  before(:each) do
537
- Bullet::Association.start_request
534
+ Bullet.start_request
538
535
  end
539
536
 
540
537
  after(:each) do
541
- Bullet::Association.end_request
538
+ Bullet.end_request
542
539
  end
543
540
 
544
541
  it "should detect unpreload associations" do
545
542
  Student.all.each do |student|
546
543
  student.teachers.collect(&:name)
547
544
  end
548
- Bullet::Association.should be_has_unpreload_associations
545
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
549
546
  end
550
547
 
551
548
  it "should detect no unpreload associations" do
552
549
  Student.includes(:teachers).each do |student|
553
550
  student.teachers.collect(&:name)
554
551
  end
555
- Bullet::Association.should_not be_has_unpreload_associations
552
+ Bullet::Detector::Association.should be_completely_preloading_associations
556
553
  end
557
554
 
558
555
  it "should detect unused preload associations" do
559
556
  Student.includes(:teachers).collect(&:name)
560
- Bullet::Association.check_unused_preload_associations
561
- Bullet::Association.should be_has_unused_preload_associations
557
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
558
+ Bullet::Detector::Association.should be_has_unused_preload_associations
562
559
  end
563
560
 
564
561
  it "should detect no unused preload associations" do
565
562
  Student.all.collect(&:name)
566
- Bullet::Association.check_unused_preload_associations
567
- Bullet::Association.should_not be_has_unused_preload_associations
563
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
564
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
568
565
  end
569
566
  end
570
567
 
571
- describe Bullet::Association, 'has_many :through' do
568
+ describe Bullet::Detector::Association, 'has_many :through' do
572
569
 
573
570
  def setup_db
574
571
  ActiveRecord::Schema.define(:version => 1) do
@@ -625,194 +622,43 @@ describe Bullet::Association, 'has_many :through' do
625
622
  end
626
623
 
627
624
  before(:each) do
628
- Bullet::Association.start_request
625
+ Bullet.start_request
629
626
  end
630
627
 
631
628
  after(:each) do
632
- Bullet::Association.end_request
629
+ Bullet.end_request
633
630
  end
634
631
 
635
632
  it "should detect unpreload associations" do
636
633
  Firm.all.each do |firm|
637
634
  firm.clients.collect(&:name)
638
635
  end
639
- Bullet::Association.should be_has_unpreload_associations
636
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
640
637
  end
641
638
 
642
639
  it "should detect no unpreload associations" do
643
640
  Firm.includes(:clients).each do |firm|
644
641
  firm.clients.collect(&:name)
645
642
  end
646
- Bullet::Association.should_not be_has_unpreload_associations
643
+ Bullet::Detector::Association.should be_completely_preloading_associations
647
644
  end
648
645
 
649
646
  it "should detect no unused preload associations" do
650
647
  Firm.all.collect(&:name)
651
- Bullet::Association.check_unused_preload_associations
652
- Bullet::Association.should_not be_has_unused_preload_associations
648
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
649
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
653
650
  end
654
651
 
655
652
  it "should detect unused preload associations" do
656
653
  Firm.includes(:clients).collect(&:name)
657
- Bullet::Association.check_unused_preload_associations
658
- Bullet::Association.should be_has_unused_preload_associations
654
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
655
+ Bullet::Detector::Association.should be_has_unused_preload_associations
659
656
  end
660
657
  end
661
658
 
662
- describe Bullet::Association, 'has_many :as' do
663
-
664
- def setup_db
665
- ActiveRecord::Schema.define(:version => 1) do
666
- create_table :votes do |t|
667
- t.column :vote, :integer
668
- t.references :voteable, :polymorphic => true
669
- end
670
-
671
- create_table :users do |t|
672
- t.column :name, :string
673
- end
674
-
675
- create_table :pets do |t|
676
- t.column :name, :string
677
- t.column :user_id, :integer
678
- end
679
-
680
- create_table :news do |t|
681
- t.column :name, :string
682
- end
683
- end
684
- end
685
-
686
- def teardown_db
687
- ActiveRecord::Base.connection.tables.each do |table|
688
- ActiveRecord::Base.connection.drop_table(table)
689
- end
690
- end
691
-
692
- class Vote < ActiveRecord::Base
693
- belongs_to :voteable, :polymorphic => true
694
- end
695
-
696
- class User < ActiveRecord::Base
697
- has_many :votes, :as => :voteable
698
- has_many :pets
699
- end
700
-
701
- class Pet < ActiveRecord::Base
702
- belongs_to :user
703
- end
704
-
705
- class News < ActiveRecord::Base
706
- has_many :votes, :as => :voteable
707
- end
708
659
 
709
- before(:all) do
710
- setup_db
711
- user1 = User.create(:name => 'first')
712
- user2 = User.create(:name => 'second')
713
- user3 = User.create(:name => 'third')
714
- user4 = User.create(:name => 'fourth')
715
-
716
- pet1 = user1.pets.create(:name => "dog")
717
- pet2 = user1.pets.create(:name => "dog")
718
- pet3 = user2.pets.create(:name => "cat")
719
- pet4 = user2.pets.create(:name => "cat")
720
-
721
- user1.votes.create(:vote => 10)
722
- user1.votes.create(:vote => 20)
723
- user2.votes.create(:vote => 10)
724
- user2.votes.create(:vote => 20)
725
- user3.votes.create(:vote => 10)
726
- user3.votes.create(:vote => 20)
727
- user4.votes.create(:vote => 10)
728
- user4.votes.create(:vote => 20)
729
-
730
- news1 = News.create(:name => 'first')
731
- news2 = News.create(:name => 'second')
732
- news1.votes.create(:vote => 10)
733
- news1.votes.create(:vote => 20)
734
- news2.votes.create(:vote => 10)
735
- news2.votes.create(:vote => 20)
736
- end
737
-
738
- after(:all) do
739
- teardown_db
740
- end
741
-
742
- before(:each) do
743
- Bullet::Association.start_request
744
- end
745
-
746
- after(:each) do
747
- Bullet::Association.end_request
748
- end
749
-
750
- # this happens only when a polymorphic association is included along with another table which is being referenced in the query
751
- it "should not have unused preloaded associations with conditions" do
752
- all_users = User.includes(:pets)
753
- users_with_ten_votes = User.includes(:votes).where(["votes.vote = ?", 10])
754
- users_without_ten_votes = User.where(["users.id not in (?)", users_with_ten_votes.collect(&:id)])
755
- puts all_users.to_sql
756
- all_users.each { |t| t.pets.collect(&:name) }
757
- Bullet::Association.check_unused_preload_associations
758
- Bullet::Association.should_not be_unused_preload_associations_for(User, :pets)
759
- Bullet::Association.should_not be_unused_preload_associations_for(User, :votes)
760
- end
761
-
762
- it "should detect unpreload associations" do
763
- User.all.each do |user|
764
- user.votes.collect(&:vote)
765
- end
766
- Bullet::Association.should be_has_unpreload_associations
767
- end
768
-
769
- it "should detect no unpreload associations" do
770
- User.includes(:votes).each do |user|
771
- user.votes.collect(&:vote)
772
- end
773
- Bullet::Association.should_not be_has_unpreload_associations
774
- end
775
-
776
- it "should detect unpreload associations with voteable" do
777
- Vote.all.each do |vote|
778
- vote.voteable.name
779
- end
780
- Bullet::Association.should be_has_unpreload_associations
781
- end
782
-
783
- it "should detect no unpreload associations with voteable" do
784
- Vote.includes(:voteable).each do |vote|
785
- vote.voteable.name
786
- end
787
- Bullet::Association.should_not be_has_unpreload_associations
788
- end
789
-
790
- it "should detect no unused preload associations" do
791
- User.all.collect(&:name)
792
- Bullet::Association.check_unused_preload_associations
793
- Bullet::Association.should_not be_has_unused_preload_associations
794
- end
795
-
796
- it "should detect unused preload associations" do
797
- User.includes(:votes).collect(&:name)
798
- Bullet::Association.check_unused_preload_associations
799
- Bullet::Association.should be_has_unused_preload_associations
800
- end
801
-
802
- it "should detect no unused preload associations with voteable" do
803
- Vote.all.collect(&:vote)
804
- Bullet::Association.check_unused_preload_associations
805
- Bullet::Association.should_not be_has_unused_preload_associations
806
- end
807
-
808
- it "should detect unused preload associations with voteable" do
809
- Vote.includes(:voteable).collect(&:vote)
810
- Bullet::Association.check_unused_preload_associations
811
- Bullet::Association.should be_has_unused_preload_associations
812
- end
813
- end
814
660
 
815
- describe Bullet::Association, "has_one" do
661
+ describe Bullet::Detector::Association, "has_one" do
816
662
 
817
663
  def setup_db
818
664
  ActiveRecord::Schema.define(:version => 1) do
@@ -856,41 +702,41 @@ describe Bullet::Association, "has_one" do
856
702
  end
857
703
 
858
704
  before(:each) do
859
- Bullet::Association.start_request
705
+ Bullet.start_request
860
706
  end
861
707
 
862
708
  after(:each) do
863
- Bullet::Association.end_request
709
+ Bullet.end_request
864
710
  end
865
711
 
866
712
  it "should detect unpreload association" do
867
713
  Company.all.each do |company|
868
714
  company.address.name
869
715
  end
870
- Bullet::Association.should be_has_unpreload_associations
716
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
871
717
  end
872
718
 
873
719
  it "should detect no unpreload association" do
874
720
  Company.find(:all, :include => :address).each do |company|
875
721
  company.address.name
876
722
  end
877
- Bullet::Association.should_not be_has_unpreload_associations
723
+ Bullet::Detector::Association.should be_completely_preloading_associations
878
724
  end
879
725
 
880
726
  it "should detect no unused preload association" do
881
727
  Company.all.collect(&:name)
882
- Bullet::Association.check_unused_preload_associations
883
- Bullet::Association.should_not be_has_unused_preload_associations
728
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
729
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
884
730
  end
885
731
 
886
732
  it "should detect unused preload association" do
887
733
  Company.find(:all, :include => :address).collect(&:name)
888
- Bullet::Association.check_unused_preload_associations
889
- Bullet::Association.should be_has_unused_preload_associations
734
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
735
+ Bullet::Detector::Association.should be_has_unused_preload_associations
890
736
  end
891
737
  end
892
738
 
893
- describe Bullet::Association, "call one association that in possible objects" do
739
+ describe Bullet::Detector::Association, "call one association that in possible objects" do
894
740
 
895
741
  def setup_db
896
742
  ActiveRecord::Schema.define(:version => 1) do
@@ -936,21 +782,21 @@ describe Bullet::Association, "call one association that in possible objects" do
936
782
  end
937
783
 
938
784
  before(:each) do
939
- Bullet::Association.start_request
785
+ Bullet.start_request
940
786
  end
941
787
 
942
788
  after(:each) do
943
- Bullet::Association.end_request
789
+ Bullet.end_request
944
790
  end
945
791
 
946
792
  it "should detect no unpreload association" do
947
793
  Contact.all
948
794
  Contact.first.emails.collect(&:name)
949
- Bullet::Association.should_not be_has_unpreload_associations
795
+ Bullet::Detector::Association.should be_completely_preloading_associations
950
796
  end
951
797
  end
952
798
 
953
- describe Bullet::Association, "STI" do
799
+ describe Bullet::Detector::Association, "STI" do
954
800
 
955
801
  def setup_db
956
802
  ActiveRecord::Schema.define(:version => 1) do
@@ -1002,42 +848,42 @@ describe Bullet::Association, "STI" do
1002
848
  end
1003
849
 
1004
850
  before(:each) do
1005
- Bullet::Association.start_request
851
+ Bullet.start_request
1006
852
  end
1007
853
 
1008
854
  after(:each) do
1009
- Bullet::Association.end_request
855
+ Bullet.end_request
1010
856
  end
1011
857
 
1012
858
  it "should detect unpreload associations" do
1013
859
  Page.all.each do |page|
1014
860
  page.author.name
1015
861
  end
1016
- Bullet::Association.should be_has_unpreload_associations
1017
- Bullet::Association.check_unused_preload_associations
1018
- Bullet::Association.should_not be_has_unused_preload_associations
862
+ Bullet::Detector::Association.should_not be_completely_preloading_associations
863
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
864
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
1019
865
  end
1020
866
 
1021
867
  it "should not detect unpreload associations" do
1022
868
  Page.find(:all, :include => :author).each do |page|
1023
869
  page.author.name
1024
870
  end
1025
- Bullet::Association.should_not be_has_unpreload_associations
1026
- Bullet::Association.check_unused_preload_associations
1027
- Bullet::Association.should_not be_has_unused_preload_associations
871
+ Bullet::Detector::Association.should be_completely_preloading_associations
872
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
873
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
1028
874
  end
1029
875
 
1030
876
  it "should detect unused preload associations" do
1031
877
  Page.find(:all, :include => :author).collect(&:name)
1032
- Bullet::Association.should_not be_has_unpreload_associations
1033
- Bullet::Association.check_unused_preload_associations
1034
- Bullet::Association.should be_has_unused_preload_associations
878
+ Bullet::Detector::Association.should be_completely_preloading_associations
879
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
880
+ Bullet::Detector::Association.should be_has_unused_preload_associations
1035
881
  end
1036
882
 
1037
883
  it "should not detect unused preload associations" do
1038
884
  Page.all.collect(&:name)
1039
- Bullet::Association.should_not be_has_unpreload_associations
1040
- Bullet::Association.check_unused_preload_associations
1041
- Bullet::Association.should_not be_has_unused_preload_associations
885
+ Bullet::Detector::Association.should be_completely_preloading_associations
886
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
887
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
1042
888
  end
1043
889
  end