bullet 4.3.1 → 4.5.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.
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- if active_record3?
3
+ if active_record3? || active_record4?
4
4
  describe Bullet::Detector::Association, 'has_many' do
5
5
  before(:each) do
6
6
  Bullet.clear
@@ -16,7 +16,7 @@ if active_record3?
16
16
  Post.all.each do |post|
17
17
  post.comments.map(&:name)
18
18
  end
19
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
19
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
20
20
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
21
21
 
22
22
  Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Post, :comments)
@@ -26,7 +26,7 @@ if active_record3?
26
26
  Post.includes(:comments).each do |post|
27
27
  post.comments.map(&:name)
28
28
  end
29
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
29
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
30
30
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
31
31
 
32
32
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -34,7 +34,7 @@ if active_record3?
34
34
 
35
35
  it "should detect unused preload post => comments" do
36
36
  Post.includes(:comments).map(&:name)
37
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
37
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
38
38
  Bullet::Detector::Association.should be_unused_preload_associations_for(Post, :comments)
39
39
 
40
40
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -42,7 +42,7 @@ if active_record3?
42
42
 
43
43
  it "should not detect unused preload post => comments" do
44
44
  Post.all.map(&:name)
45
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
45
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
46
46
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
47
47
 
48
48
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -56,7 +56,7 @@ if active_record3?
56
56
  post.comments.map(&:name)
57
57
  end
58
58
  end
59
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
59
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
60
60
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
61
61
 
62
62
  Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Category, :posts)
@@ -66,10 +66,10 @@ if active_record3?
66
66
  it "should detect preload category => posts, but no post => comments" do
67
67
  Category.includes(:posts).each do |category|
68
68
  category.posts.each do |post|
69
- post.comments.collect(&:name)
69
+ post.comments.map(&:name)
70
70
  end
71
71
  end
72
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
72
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
73
73
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
74
74
 
75
75
  Bullet::Detector::Association.should_not be_detecting_unpreloaded_association_for(Category, :posts)
@@ -82,7 +82,7 @@ if active_record3?
82
82
  post.comments.map(&:name)
83
83
  end
84
84
  end
85
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
85
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
86
86
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
87
87
 
88
88
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -90,7 +90,7 @@ if active_record3?
90
90
 
91
91
  it "should detect unused preload with category => posts => comments" do
92
92
  Category.includes({:posts => :comments}).map(&:name)
93
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
93
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
94
94
  Bullet::Detector::Association.should be_unused_preload_associations_for(Post, :comments)
95
95
 
96
96
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -100,7 +100,7 @@ if active_record3?
100
100
  Category.includes({:posts => :comments}).each do |category|
101
101
  category.posts.map(&:name)
102
102
  end
103
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
103
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
104
104
  Bullet::Detector::Association.should be_unused_preload_associations_for(Post, :comments)
105
105
 
106
106
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -113,7 +113,7 @@ if active_record3?
113
113
  category.posts.map(&:name)
114
114
  category.entries.map(&:name)
115
115
  end
116
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
116
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
117
117
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
118
118
 
119
119
  Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Category, :posts)
@@ -125,7 +125,7 @@ if active_record3?
125
125
  category.posts.map(&:name)
126
126
  category.entries.map(&:name)
127
127
  end
128
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
128
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
129
129
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
130
130
 
131
131
  Bullet::Detector::Association.should_not be_detecting_unpreloaded_association_for(Category, :posts)
@@ -137,7 +137,7 @@ if active_record3?
137
137
  category.posts.map(&:name)
138
138
  category.entries.map(&:name)
139
139
  end
140
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
140
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
141
141
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
142
142
 
143
143
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -145,7 +145,7 @@ if active_record3?
145
145
 
146
146
  it "should detect unused preload with category => [posts, entries]" do
147
147
  Category.includes([:posts, :entries]).map(&:name)
148
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
148
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
149
149
  Bullet::Detector::Association.should be_unused_preload_associations_for(Category, :posts)
150
150
  Bullet::Detector::Association.should be_unused_preload_associations_for(Category, :entries)
151
151
 
@@ -156,7 +156,7 @@ if active_record3?
156
156
  Category.includes([:posts, :entries]).each do |category|
157
157
  category.posts.map(&:name)
158
158
  end
159
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
159
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
160
160
  Bullet::Detector::Association.should_not be_unused_preload_associations_for(Category, :posts)
161
161
  Bullet::Detector::Association.should be_unused_preload_associations_for(Category, :entries)
162
162
 
@@ -169,15 +169,15 @@ if active_record3?
169
169
  Post.includes(:comments).each do |post|
170
170
  post.comments.first.name
171
171
  end
172
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
172
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
173
173
  Bullet::Detector::Association.should_not be_unused_preload_associations_for(Post, :comments)
174
174
 
175
175
  Bullet::Detector::Association.should be_completely_preloading_associations
176
176
  end
177
177
 
178
178
  it "should detect preload with post => commnets" do
179
- Post.first.comments.collect(&:name)
180
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
179
+ Post.first.comments.map(&:name)
180
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
181
181
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
182
182
 
183
183
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -191,7 +191,7 @@ if active_record3?
191
191
  post.name
192
192
  post.writer.name
193
193
  end
194
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
194
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
195
195
  Bullet::Detector::Association.should_not be_unused_preload_associations_for(Category, :posts)
196
196
  Bullet::Detector::Association.should_not be_unused_preload_associations_for(Post, :writer)
197
197
  end
@@ -199,10 +199,10 @@ if active_record3?
199
199
 
200
200
  context "scope for_category_name" do
201
201
  it "should detect preload with post => category" do
202
- Post.in_category_name('first').all.each do |post|
202
+ Post.in_category_name('first').each do |post|
203
203
  post.category.name
204
204
  end
205
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
205
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
206
206
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
207
207
 
208
208
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -210,7 +210,7 @@ if active_record3?
210
210
 
211
211
  it "should not be unused preload post => category" do
212
212
  Post.in_category_name('first').all.map(&:name)
213
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
213
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
214
214
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
215
215
 
216
216
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -222,7 +222,7 @@ if active_record3?
222
222
  Post.preload_comments.each do |post|
223
223
  post.comments.map(&:name)
224
224
  end
225
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
225
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
226
226
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
227
227
 
228
228
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -230,7 +230,7 @@ if active_record3?
230
230
 
231
231
  it "should detect unused preload with scope" do
232
232
  Post.preload_comments.map(&:name)
233
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
233
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
234
234
  Bullet::Detector::Association.should be_unused_preload_associations_for(Post, :comments)
235
235
 
236
236
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -253,7 +253,7 @@ if active_record3?
253
253
  Comment.all.each do |comment|
254
254
  comment.post.name
255
255
  end
256
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
256
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
257
257
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
258
258
 
259
259
  Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Comment, :post)
@@ -261,7 +261,7 @@ if active_record3?
261
261
 
262
262
  it "should detect preload with one comment => post" do
263
263
  Comment.first.post.name
264
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
264
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
265
265
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
266
266
 
267
267
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -271,15 +271,15 @@ if active_record3?
271
271
  Comment.includes(:post).each do |comment|
272
272
  comment.post.name
273
273
  end
274
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
274
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
275
275
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
276
276
 
277
277
  Bullet::Detector::Association.should be_completely_preloading_associations
278
278
  end
279
279
 
280
280
  it "should not detect preload with comment => post" do
281
- Comment.all.collect(&:name)
282
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
281
+ Comment.all.map(&:name)
282
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
283
283
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
284
284
 
285
285
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -287,7 +287,7 @@ if active_record3?
287
287
 
288
288
  it "should detect unused preload with comments => post" do
289
289
  Comment.includes(:post).map(&:name)
290
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
290
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
291
291
  Bullet::Detector::Association.should be_unused_preload_associations_for(Comment, :post)
292
292
 
293
293
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -299,7 +299,7 @@ if active_record3?
299
299
  Comment.all.each do |comment|
300
300
  comment.post.category.name
301
301
  end
302
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
302
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
303
303
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
304
304
 
305
305
  Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Comment, :post)
@@ -309,7 +309,7 @@ if active_record3?
309
309
  Comment.includes(:post).each do |comment|
310
310
  comment.post.category.name
311
311
  end
312
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
312
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
313
313
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
314
314
 
315
315
  Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Post, :category)
@@ -319,7 +319,7 @@ if active_record3?
319
319
  Comment.includes(:post => :category).each do |comment|
320
320
  comment.post.category.name
321
321
  end
322
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
322
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
323
323
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
324
324
 
325
325
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -333,7 +333,7 @@ if active_record3?
333
333
  Comment.includes([:author, :post]).where(["base_users.id = ?", BaseUser.first]).each do |comment|
334
334
  comment.post.writer.name
335
335
  end
336
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
336
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
337
337
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
338
338
 
339
339
  Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Post, :writer)
@@ -345,7 +345,7 @@ if active_record3?
345
345
  Comment.includes([:author, {:post => :writer}]).where(["base_users.id = ?", BaseUser.first]).each do |comment|
346
346
  comment.post.writer.name
347
347
  end
348
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
348
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
349
349
  Bullet::Detector::Association.should be_unused_preload_associations_for(Comment, :author)
350
350
 
351
351
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -357,7 +357,7 @@ if active_record3?
357
357
  Comment.all(:include => {:post => :writer}, :conditions => "posts.name like '%first%'").each do |comment|
358
358
  comment.post.writer.newspaper.name
359
359
  end
360
- #Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
360
+ #Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
361
361
  #Bullet::Detector::Association.should_not be_has_unused_preload_associations
362
362
 
363
363
  Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Writer, :newspaper)
@@ -390,7 +390,7 @@ if active_record3?
390
390
  Student.all.each do |student|
391
391
  student.teachers.map(&:name)
392
392
  end
393
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
393
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
394
394
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
395
395
 
396
396
  Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Student, :teachers)
@@ -400,7 +400,7 @@ if active_record3?
400
400
  Student.includes(:teachers).each do |student|
401
401
  student.teachers.map(&:name)
402
402
  end
403
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
403
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
404
404
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
405
405
 
406
406
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -408,15 +408,15 @@ if active_record3?
408
408
 
409
409
  it "should detect unused preload associations" do
410
410
  Student.includes(:teachers).map(&:name)
411
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
411
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
412
412
  Bullet::Detector::Association.should be_unused_preload_associations_for(Student, :teachers)
413
413
 
414
414
  Bullet::Detector::Association.should be_completely_preloading_associations
415
415
  end
416
416
 
417
417
  it "should detect no unused preload associations" do
418
- Student.all.collect(&:name)
419
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
418
+ Student.all.map(&:name)
419
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
420
420
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
421
421
 
422
422
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -439,7 +439,7 @@ if active_record3?
439
439
  Firm.all.each do |firm|
440
440
  firm.clients.map(&:name)
441
441
  end
442
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
442
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
443
443
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
444
444
 
445
445
  Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Firm, :clients)
@@ -449,23 +449,23 @@ if active_record3?
449
449
  Firm.includes(:clients).each do |firm|
450
450
  firm.clients.map(&:name)
451
451
  end
452
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
452
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
453
453
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
454
454
 
455
455
  Bullet::Detector::Association.should be_completely_preloading_associations
456
456
  end
457
457
 
458
458
  it "should not detect preload associations" do
459
- Firm.all.collect(&:name)
460
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
459
+ Firm.all.map(&:name)
460
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
461
461
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
462
462
 
463
463
  Bullet::Detector::Association.should be_completely_preloading_associations
464
464
  end
465
465
 
466
466
  it "should detect unused preload associations" do
467
- Firm.includes(:clients).collect(&:name)
468
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
467
+ Firm.includes(:clients).map(&:name)
468
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
469
469
  Bullet::Detector::Association.should be_unused_preload_associations_for(Firm, :clients)
470
470
 
471
471
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -488,33 +488,33 @@ if active_record3?
488
488
  Company.all.each do |company|
489
489
  company.address.name
490
490
  end
491
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
491
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
492
492
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
493
493
 
494
494
  Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Company, :address)
495
495
  end
496
496
 
497
497
  it "should detect preload association" do
498
- Company.find(:all, :include => :address).each do |company|
498
+ Company.includes(:address).each do |company|
499
499
  company.address.name
500
500
  end
501
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
501
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
502
502
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
503
503
 
504
504
  Bullet::Detector::Association.should be_completely_preloading_associations
505
505
  end
506
506
 
507
507
  it "should not detect preload association" do
508
- Company.all.collect(&:name)
509
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
508
+ Company.all.map(&:name)
509
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
510
510
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
511
511
 
512
512
  Bullet::Detector::Association.should be_completely_preloading_associations
513
513
  end
514
514
 
515
515
  it "should detect unused preload association" do
516
- Company.find(:all, :include => :address).collect(&:name)
517
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
516
+ Company.includes(:address).map(&:name)
517
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
518
518
  Bullet::Detector::Association.should be_unused_preload_associations_for(Company, :address)
519
519
 
520
520
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -535,7 +535,7 @@ if active_record3?
535
535
  it "should not detect preload association" do
536
536
  Post.all
537
537
  Post.first.comments.map(&:name)
538
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
538
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
539
539
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
540
540
 
541
541
  Bullet::Detector::Association.should be_completely_preloading_associations
@@ -557,37 +557,129 @@ if active_record3?
557
557
  Page.all.each do |page|
558
558
  page.author.name
559
559
  end
560
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
560
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
561
561
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
562
562
 
563
563
  Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Page, :author)
564
564
  end
565
565
 
566
566
  it "should detect preload associations" do
567
- Page.find(:all, :include => :author).each do |page|
567
+ Page.includes(:author).each do |page|
568
568
  page.author.name
569
569
  end
570
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
570
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
571
571
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
572
572
 
573
573
  Bullet::Detector::Association.should be_completely_preloading_associations
574
574
  end
575
575
 
576
576
  it "should detect unused preload associations" do
577
- Page.find(:all, :include => :author).collect(&:name)
578
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
577
+ Page.includes(:author).map(&:name)
578
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
579
579
  Bullet::Detector::Association.should be_unused_preload_associations_for(Page, :author)
580
580
 
581
581
  Bullet::Detector::Association.should be_completely_preloading_associations
582
582
  end
583
583
 
584
584
  it "should not detect preload associations" do
585
- Page.all.collect(&:name)
586
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
585
+ Page.all.map(&:name)
586
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
587
587
  Bullet::Detector::Association.should_not be_has_unused_preload_associations
588
588
 
589
589
  Bullet::Detector::Association.should be_completely_preloading_associations
590
590
  end
591
591
  end
592
+
593
+ context "disable n plus one query" do
594
+ before { Bullet.n_plus_one_query_enable = false }
595
+ after { Bullet.n_plus_one_query_enable = true }
596
+
597
+ it "should not detect n plus one query" do
598
+ Post.all.each do |post|
599
+ post.comments.map(&:name)
600
+ end
601
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
602
+
603
+ expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Post, :comments)
604
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
605
+ end
606
+
607
+ it "should still detect unused eager loading" do
608
+ Post.includes(:comments).map(&:name)
609
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
610
+
611
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
612
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
613
+ end
614
+ end
615
+
616
+ context "disable unused eager loading" do
617
+ before { Bullet.unused_eager_loading_enable = false }
618
+ after { Bullet.unused_eager_loading_enable = true }
619
+
620
+ it "should not detect unused eager loading" do
621
+ Post.includes(:comments).map(&:name)
622
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
623
+
624
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
625
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
626
+ end
627
+
628
+ it "should still detect n plus one query" do
629
+ Post.all.each do |post|
630
+ post.comments.map(&:name)
631
+ end
632
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
633
+
634
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
635
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
636
+ end
637
+ end
638
+
639
+ context "whitelist n plus one query" do
640
+ before { Bullet.add_whitelist :type => :n_plus_one_query, :class_name => "Post", :association => :comments }
641
+ after { Bullet.reset_whitelist }
642
+
643
+ it "should not detect n plus one query" do
644
+ Post.all.each do |post|
645
+ post.comments.map(&:name)
646
+ end
647
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
648
+
649
+ expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Post, :comments)
650
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
651
+ end
652
+
653
+ it "should still detect unused eager loading" do
654
+ Post.includes(:comments).map(&:name)
655
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
656
+
657
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
658
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
659
+ end
660
+ end
661
+
662
+ context "whitelist unused eager loading" do
663
+ before { Bullet.add_whitelist :type => :unused_eager_loading, :class_name => "Post", :association => :comments }
664
+ after { Bullet.reset_whitelist }
665
+
666
+ it "should not detect unused eager loading" do
667
+ Post.includes(:comments).map(&:name)
668
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
669
+
670
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
671
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
672
+ end
673
+
674
+ it "should still detect n plus one query" do
675
+ Post.all.each do |post|
676
+ post.comments.map(&:name)
677
+ end
678
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
679
+
680
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
681
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
682
+ end
683
+ end
592
684
  end
593
685
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
- if active_record3?
4
- describe Bullet::Detector::Counter do
3
+ if active_record3? || active_record4?
4
+ describe Bullet::Detector::CounterCache do
5
5
  before(:each) do
6
6
  Bullet.start_request
7
7
  end
@@ -35,5 +35,29 @@ if active_record3?
35
35
  end
36
36
  Bullet.collected_counter_cache_notifications.should be_empty
37
37
  end
38
+
39
+ context "disable" do
40
+ before { Bullet.counter_cache_enable = false }
41
+ after { Bullet.counter_cache_enable = true }
42
+
43
+ it "should not detect counter cache" do
44
+ Country.all.each do |country|
45
+ country.cities.size
46
+ end
47
+ expect(Bullet.collected_counter_cache_notifications).to be_empty
48
+ end
49
+ end
50
+
51
+ context "whitelist" do
52
+ before { Bullet.add_whitelist :type => :counter_cache, :class_name => "Country", :association => :cities }
53
+ after { Bullet.reset_whitelist }
54
+
55
+ it "should not detect counter cache" do
56
+ Country.all.each do |country|
57
+ country.cities.size
58
+ end
59
+ expect(Bullet.collected_counter_cache_notifications).to be_empty
60
+ end
61
+ end
38
62
  end
39
63
  end