bullet 5.7.5 → 6.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +22 -1
  3. data/CHANGELOG.md +49 -12
  4. data/Gemfile.mongoid-7.0 +15 -0
  5. data/Gemfile.rails-4.0 +1 -1
  6. data/Gemfile.rails-4.1 +1 -1
  7. data/Gemfile.rails-4.2 +1 -1
  8. data/Gemfile.rails-5.0 +1 -1
  9. data/Gemfile.rails-5.1 +1 -1
  10. data/Gemfile.rails-5.2 +2 -2
  11. data/Gemfile.rails-6.0 +15 -0
  12. data/Gemfile.rails-6.1 +15 -0
  13. data/README.md +38 -13
  14. data/Rakefile +1 -1
  15. data/bullet.gemspec +8 -3
  16. data/lib/bullet.rb +50 -22
  17. data/lib/bullet/active_job.rb +13 -0
  18. data/lib/bullet/active_record4.rb +12 -35
  19. data/lib/bullet/active_record41.rb +10 -30
  20. data/lib/bullet/active_record42.rb +12 -27
  21. data/lib/bullet/active_record5.rb +197 -177
  22. data/lib/bullet/active_record52.rb +191 -166
  23. data/lib/bullet/active_record60.rb +278 -0
  24. data/lib/bullet/active_record61.rb +278 -0
  25. data/lib/bullet/bullet_xhr.js +63 -0
  26. data/lib/bullet/dependency.rb +54 -34
  27. data/lib/bullet/detector/association.rb +26 -20
  28. data/lib/bullet/detector/base.rb +1 -2
  29. data/lib/bullet/detector/counter_cache.rb +14 -9
  30. data/lib/bullet/detector/n_plus_one_query.rb +27 -17
  31. data/lib/bullet/detector/unused_eager_loading.rb +7 -3
  32. data/lib/bullet/ext/object.rb +5 -3
  33. data/lib/bullet/ext/string.rb +1 -1
  34. data/lib/bullet/mongoid4x.rb +4 -7
  35. data/lib/bullet/mongoid5x.rb +4 -7
  36. data/lib/bullet/mongoid6x.rb +8 -11
  37. data/lib/bullet/mongoid7x.rb +57 -0
  38. data/lib/bullet/notification/base.rb +15 -19
  39. data/lib/bullet/notification/n_plus_one_query.rb +2 -4
  40. data/lib/bullet/notification/unused_eager_loading.rb +2 -4
  41. data/lib/bullet/rack.rb +54 -28
  42. data/lib/bullet/stack_trace_filter.rb +39 -30
  43. data/lib/bullet/version.rb +1 -1
  44. data/lib/generators/bullet/install_generator.rb +26 -26
  45. data/perf/benchmark.rb +8 -14
  46. data/spec/bullet/detector/counter_cache_spec.rb +6 -6
  47. data/spec/bullet/detector/n_plus_one_query_spec.rb +30 -3
  48. data/spec/bullet/detector/unused_eager_loading_spec.rb +19 -6
  49. data/spec/bullet/ext/object_spec.rb +9 -4
  50. data/spec/bullet/notification/base_spec.rb +1 -3
  51. data/spec/bullet/notification/n_plus_one_query_spec.rb +16 -3
  52. data/spec/bullet/notification/unused_eager_loading_spec.rb +5 -1
  53. data/spec/bullet/rack_spec.rb +140 -5
  54. data/spec/bullet/registry/association_spec.rb +2 -2
  55. data/spec/bullet/registry/base_spec.rb +1 -1
  56. data/spec/bullet_spec.rb +10 -29
  57. data/spec/integration/active_record/association_spec.rb +122 -118
  58. data/spec/integration/counter_cache_spec.rb +11 -31
  59. data/spec/integration/mongoid/association_spec.rb +18 -32
  60. data/spec/models/attachment.rb +5 -0
  61. data/spec/models/client.rb +2 -0
  62. data/spec/models/firm.rb +1 -0
  63. data/spec/models/folder.rb +1 -2
  64. data/spec/models/group.rb +3 -0
  65. data/spec/models/page.rb +1 -2
  66. data/spec/models/post.rb +15 -0
  67. data/spec/models/submission.rb +1 -0
  68. data/spec/models/user.rb +1 -0
  69. data/spec/models/writer.rb +1 -2
  70. data/spec/spec_helper.rb +6 -10
  71. data/spec/support/bullet_ext.rb +8 -9
  72. data/spec/support/mongo_seed.rb +2 -16
  73. data/spec/support/sqlite_seed.rb +17 -2
  74. data/test.sh +2 -0
  75. data/update.sh +1 -0
  76. metadata +24 -11
@@ -6,9 +6,7 @@ if active_record?
6
6
  describe Bullet::Detector::Association, 'has_many' do
7
7
  context 'post => comments' do
8
8
  it 'should detect non preload post => comments' do
9
- Post.all.each do |post|
10
- post.comments.map(&:name)
11
- end
9
+ Post.all.each { |post| post.comments.map(&:name) }
12
10
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
13
11
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
14
12
 
@@ -16,9 +14,7 @@ if active_record?
16
14
  end
17
15
 
18
16
  it 'should detect non preload post => comments for find_by_sql' do
19
- Post.find_by_sql('SELECT * FROM posts').each do |post|
20
- post.comments.map(&:name)
21
- end
17
+ Post.find_by_sql('SELECT * FROM posts').each { |post| post.comments.map(&:name) }
22
18
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
23
19
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
24
20
 
@@ -26,9 +22,7 @@ if active_record?
26
22
  end
27
23
 
28
24
  it 'should detect preload with post => comments' do
29
- Post.includes(:comments).each do |post|
30
- post.comments.map(&:name)
31
- end
25
+ Post.includes(:comments).each { |post| post.comments.map(&:name) }
32
26
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
33
27
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
34
28
 
@@ -65,9 +59,7 @@ if active_record?
65
59
  end
66
60
 
67
61
  it 'should detect non preload post => comments with empty?' do
68
- Post.all.each do |post|
69
- post.comments.empty?
70
- end
62
+ Post.all.each { |post| post.comments.empty? }
71
63
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
72
64
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
73
65
 
@@ -76,9 +68,7 @@ if active_record?
76
68
 
77
69
  it 'should detect non preload post => comments with include?' do
78
70
  comment = Comment.last
79
- Post.all.each do |post|
80
- post.comments.include?(comment)
81
- end
71
+ Post.all.each { |post| post.comments.include?(comment) }
82
72
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
83
73
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
84
74
 
@@ -86,9 +76,7 @@ if active_record?
86
76
  end
87
77
 
88
78
  it 'should not detect unused preload person => pets with empty?' do
89
- Person.all.each do |person|
90
- person.pets.empty?
91
- end
79
+ Person.all.each { |person| person.pets.empty? }
92
80
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
93
81
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
94
82
 
@@ -98,11 +86,7 @@ if active_record?
98
86
 
99
87
  context 'category => posts => comments' do
100
88
  it 'should detect non preload category => posts => comments' do
101
- Category.all.each do |category|
102
- category.posts.each do |post|
103
- post.comments.map(&:name)
104
- end
105
- end
89
+ Category.all.each { |category| category.posts.each { |post| post.comments.map(&:name) } }
106
90
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
107
91
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
108
92
 
@@ -111,11 +95,7 @@ if active_record?
111
95
  end
112
96
 
113
97
  it 'should detect preload category => posts, but no post => comments' do
114
- Category.includes(:posts).each do |category|
115
- category.posts.each do |post|
116
- post.comments.map(&:name)
117
- end
118
- end
98
+ Category.includes(:posts).each { |category| category.posts.each { |post| post.comments.map(&:name) } }
119
99
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
120
100
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
121
101
 
@@ -124,11 +104,7 @@ if active_record?
124
104
  end
125
105
 
126
106
  it 'should detect preload with category => posts => comments' do
127
- Category.includes(posts: :comments).each do |category|
128
- category.posts.each do |post|
129
- post.comments.map(&:name)
130
- end
131
- end
107
+ Category.includes(posts: :comments).each { |category| category.posts.each { |post| post.comments.map(&:name) } }
132
108
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
133
109
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
134
110
 
@@ -137,9 +113,7 @@ if active_record?
137
113
 
138
114
  it 'should detect preload with category => posts => comments with posts.id > 0' do
139
115
  Category.includes(posts: :comments).where('posts.id > 0').references(:posts).each do |category|
140
- category.posts.each do |post|
141
- post.comments.map(&:name)
142
- end
116
+ category.posts.each { |post| post.comments.map(&:name) }
143
117
  end
144
118
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
145
119
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
@@ -156,9 +130,7 @@ if active_record?
156
130
  end
157
131
 
158
132
  it 'should detect unused preload with post => commnets, no category => posts' do
159
- Category.includes(posts: :comments).each do |category|
160
- category.posts.map(&:name)
161
- end
133
+ Category.includes(posts: :comments).each { |category| category.posts.map(&:name) }
162
134
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
163
135
  expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
164
136
 
@@ -212,9 +184,7 @@ if active_record?
212
184
  end
213
185
 
214
186
  it 'should detect unused preload with category => entries, but not with category => posts' do
215
- Category.includes(%i[posts entries]).each do |category|
216
- category.posts.map(&:name)
217
- end
187
+ Category.includes(%i[posts entries]).each { |category| category.posts.map(&:name) }
218
188
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
219
189
  expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Category, :posts)
220
190
  expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Category, :entries)
@@ -225,9 +195,7 @@ if active_record?
225
195
 
226
196
  context 'post => comment' do
227
197
  it 'should detect unused preload with post => comments' do
228
- Post.includes(:comments).each do |post|
229
- post.comments.first.name if post.comments.first
230
- end
198
+ Post.includes(:comments).each { |post| post.comments.first&.name }
231
199
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
232
200
  expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Post, :comments)
233
201
 
@@ -246,7 +214,7 @@ if active_record?
246
214
  category = Category.first
247
215
  category.draft_post.destroy!
248
216
  post = category.draft_post
249
- post.update_attributes!(link: true)
217
+ post.update!(link: true)
250
218
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
251
219
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
252
220
 
@@ -272,9 +240,7 @@ if active_record?
272
240
 
273
241
  context 'scope for_category_name' do
274
242
  it 'should detect preload with post => category' do
275
- Post.in_category_name('first').references(:categories).each do |post|
276
- post.category.name
277
- end
243
+ Post.in_category_name('first').references(:categories).each { |post| post.category.name }
278
244
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
279
245
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
280
246
 
@@ -292,9 +258,7 @@ if active_record?
292
258
 
293
259
  context 'scope preload_comments' do
294
260
  it 'should detect preload post => comments with scope' do
295
- Post.preload_comments.each do |post|
296
- post.comments.map(&:name)
297
- end
261
+ Post.preload_comments.each { |post| post.comments.map(&:name) }
298
262
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
299
263
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
300
264
 
@@ -314,9 +278,7 @@ if active_record?
314
278
  describe Bullet::Detector::Association, 'belongs_to' do
315
279
  context 'comment => post' do
316
280
  it 'should detect non preload with comment => post' do
317
- Comment.all.each do |comment|
318
- comment.post.name
319
- end
281
+ Comment.all.each { |comment| comment.post.name }
320
282
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
321
283
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
322
284
 
@@ -331,10 +293,8 @@ if active_record?
331
293
  expect(Bullet::Detector::Association).to be_completely_preloading_associations
332
294
  end
333
295
 
334
- it 'should dtect preload with comment => post' do
335
- Comment.includes(:post).each do |comment|
336
- comment.post.name
337
- end
296
+ it 'should detect preload with comment => post' do
297
+ Comment.includes(:post).each { |comment| comment.post.name }
338
298
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
339
299
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
340
300
 
@@ -356,13 +316,22 @@ if active_record?
356
316
 
357
317
  expect(Bullet::Detector::Association).to be_completely_preloading_associations
358
318
  end
319
+
320
+ it 'should not detect newly assigned object in an after_save' do
321
+ new_post = Post.new(category: Category.first)
322
+
323
+ new_post.trigger_after_save = true
324
+ new_post.save!
325
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
326
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
327
+
328
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
329
+ end
359
330
  end
360
331
 
361
332
  context 'comment => post => category' do
362
333
  it 'should detect non preload association with comment => post' do
363
- Comment.all.each do |comment|
364
- comment.post.category.name
365
- end
334
+ Comment.all.each { |comment| comment.post.category.name }
366
335
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
367
336
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
368
337
 
@@ -379,9 +348,7 @@ if active_record?
379
348
  end
380
349
 
381
350
  it 'should detect non preload association with post => category' do
382
- Comment.includes(:post).each do |comment|
383
- comment.post.category.name
384
- end
351
+ Comment.includes(:post).each { |comment| comment.post.category.name }
385
352
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
386
353
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
387
354
 
@@ -389,9 +356,7 @@ if active_record?
389
356
  end
390
357
 
391
358
  it 'should not detect unpreload association' do
392
- Comment.includes(post: :category).each do |comment|
393
- comment.post.category.name
394
- end
359
+ Comment.includes(post: :category).each { |comment| comment.post.category.name }
395
360
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
396
361
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
397
362
 
@@ -401,9 +366,8 @@ if active_record?
401
366
 
402
367
  context 'comment => author, post => writer' do
403
368
  it 'should detect non preloaded writer' do
404
- Comment.includes(%i[author post]).where(['base_users.id = ?', BaseUser.first]).references(:base_users).each do |comment|
405
- comment.post.writer.name
406
- end
369
+ Comment.includes(%i[author post]).where(['base_users.id = ?', BaseUser.first]).references(:base_users)
370
+ .each { |comment| comment.post.writer.name }
407
371
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
408
372
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
409
373
 
@@ -411,9 +375,9 @@ if active_record?
411
375
  end
412
376
 
413
377
  it 'should detect unused preload with comment => author' do
414
- Comment.includes([:author, { post: :writer }]).where(['base_users.id = ?', BaseUser.first]).references(:base_users).each do |comment|
415
- comment.post.writer.name
416
- end
378
+ Comment.includes([:author, { post: :writer }]).where(['base_users.id = ?', BaseUser.first]).references(
379
+ :base_users
380
+ ).each { |comment| comment.post.writer.name }
417
381
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
418
382
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
419
383
 
@@ -431,11 +395,7 @@ if active_record?
431
395
  end
432
396
 
433
397
  it 'should not raise a stack error from posts to category' do
434
- expect {
435
- Comment.includes(post: :category).each do |com|
436
- com.post.category
437
- end
438
- }.not_to raise_error
398
+ expect { Comment.includes(post: :category).each { |com| com.post.category } }.not_to raise_error
439
399
  end
440
400
  end
441
401
  end
@@ -443,9 +403,7 @@ if active_record?
443
403
  describe Bullet::Detector::Association, 'has_and_belongs_to_many' do
444
404
  context 'students <=> teachers' do
445
405
  it 'should detect non preload associations' do
446
- Student.all.each do |student|
447
- student.teachers.map(&:name)
448
- end
406
+ Student.all.each { |student| student.teachers.map(&:name) }
449
407
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
450
408
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
451
409
 
@@ -453,9 +411,7 @@ if active_record?
453
411
  end
454
412
 
455
413
  it 'should detect preload associations' do
456
- Student.includes(:teachers).each do |student|
457
- student.teachers.map(&:name)
458
- end
414
+ Student.includes(:teachers).each { |student| student.teachers.map(&:name) }
459
415
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
460
416
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
461
417
 
@@ -479,9 +435,7 @@ if active_record?
479
435
  end
480
436
 
481
437
  it 'should detect non preload student => teachers with empty?' do
482
- Student.all.each do |student|
483
- student.teachers.empty?
484
- end
438
+ Student.all.each { |student| student.teachers.empty? }
485
439
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
486
440
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
487
441
 
@@ -493,9 +447,7 @@ if active_record?
493
447
  describe Bullet::Detector::Association, 'has_many :through' do
494
448
  context 'firm => clients' do
495
449
  it 'should detect non preload associations' do
496
- Firm.all.each do |firm|
497
- firm.clients.map(&:name)
498
- end
450
+ Firm.all.each { |firm| firm.clients.map(&:name) }
499
451
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
500
452
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
501
453
 
@@ -503,9 +455,7 @@ if active_record?
503
455
  end
504
456
 
505
457
  it 'should detect preload associations' do
506
- Firm.includes(:clients).each do |firm|
507
- firm.clients.map(&:name)
508
- end
458
+ Firm.includes(:clients).each { |firm| firm.clients.map(&:name) }
509
459
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
510
460
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
511
461
 
@@ -528,14 +478,46 @@ if active_record?
528
478
  expect(Bullet::Detector::Association).to be_completely_preloading_associations
529
479
  end
530
480
  end
481
+
482
+ context 'firm => clients => groups' do
483
+ it 'should detect non preload associations' do
484
+ Firm.all.each { |firm| firm.groups.map(&:name) }
485
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
486
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
487
+
488
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Firm, :groups)
489
+ end
490
+
491
+ it 'should detect preload associations' do
492
+ Firm.includes(:groups).each { |firm| firm.groups.map(&:name) }
493
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
494
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
495
+
496
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
497
+ end
498
+
499
+ it 'should not detect preload associations' do
500
+ Firm.all.map(&:name)
501
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
502
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
503
+
504
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
505
+ end
506
+
507
+ it 'should detect unused preload associations' do
508
+ Firm.includes(:groups).map(&:name)
509
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
510
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Firm, :groups)
511
+
512
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
513
+ end
514
+ end
531
515
  end
532
516
 
533
517
  describe Bullet::Detector::Association, 'has_one' do
534
518
  context 'company => address' do
535
519
  it 'should detect non preload association' do
536
- Company.all.each do |company|
537
- company.address.name
538
- end
520
+ Company.all.each { |company| company.address.name }
539
521
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
540
522
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
541
523
 
@@ -543,9 +525,7 @@ if active_record?
543
525
  end
544
526
 
545
527
  it 'should detect preload association' do
546
- Company.includes(:address).each do |company|
547
- company.address.name
548
- end
528
+ Company.includes(:address).each { |company| company.address.name }
549
529
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
550
530
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
551
531
 
@@ -581,6 +561,42 @@ if active_record?
581
561
  end
582
562
  end
583
563
 
564
+ describe Bullet::Detector::Association, 'has_one :through' do
565
+ context 'user => attachment' do
566
+ it 'should detect non preload associations' do
567
+ User.all.each { |user| user.submission_attachment.file_name }
568
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
569
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
570
+
571
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(User, :submission_attachment)
572
+ end
573
+
574
+ it 'should detect preload associations' do
575
+ User.includes(:submission_attachment).each { |user| user.submission_attachment.file_name }
576
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
577
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
578
+
579
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
580
+ end
581
+
582
+ it 'should not detect preload associations' do
583
+ User.all.map(&:name)
584
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
585
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
586
+
587
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
588
+ end
589
+
590
+ it 'should detect unused preload associations' do
591
+ User.includes(:submission_attachment).map(&:name)
592
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
593
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(User, :submission_attachment)
594
+
595
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
596
+ end
597
+ end
598
+ end
599
+
584
600
  describe Bullet::Detector::Association, 'call one association that in possible objects' do
585
601
  it 'should not detect preload association' do
586
602
  Post.all
@@ -639,9 +655,7 @@ if active_record?
639
655
  describe Bullet::Detector::Association, 'STI' do
640
656
  context 'page => author' do
641
657
  it 'should detect non preload associations' do
642
- Page.all.each do |page|
643
- page.author.name
644
- end
658
+ Page.all.each { |page| page.author.name }
645
659
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
646
660
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
647
661
 
@@ -649,9 +663,7 @@ if active_record?
649
663
  end
650
664
 
651
665
  it 'should detect preload associations' do
652
- Page.includes(:author).each do |page|
653
- page.author.name
654
- end
666
+ Page.includes(:author).each { |page| page.author.name }
655
667
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
656
668
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
657
669
 
@@ -680,9 +692,7 @@ if active_record?
680
692
  after { Bullet.n_plus_one_query_enable = true }
681
693
 
682
694
  it 'should not detect n plus one query' do
683
- Post.all.each do |post|
684
- post.comments.map(&:name)
685
- end
695
+ Post.all.each { |post| post.comments.map(&:name) }
686
696
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
687
697
 
688
698
  expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Post, :comments)
@@ -711,9 +721,7 @@ if active_record?
711
721
  end
712
722
 
713
723
  it 'should still detect n plus one query' do
714
- Post.all.each do |post|
715
- post.comments.map(&:name)
716
- end
724
+ Post.all.each { |post| post.comments.map(&:name) }
717
725
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
718
726
 
719
727
  expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
@@ -726,9 +734,7 @@ if active_record?
726
734
  after { Bullet.clear_whitelist }
727
735
 
728
736
  it 'should not detect n plus one query' do
729
- Post.all.each do |post|
730
- post.comments.map(&:name)
731
- end
737
+ Post.all.each { |post| post.comments.map(&:name) }
732
738
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
733
739
 
734
740
  expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Post, :comments)
@@ -757,9 +763,7 @@ if active_record?
757
763
  end
758
764
 
759
765
  it 'should still detect n plus one query' do
760
- Post.all.each do |post|
761
- post.comments.map(&:name)
762
- end
766
+ Post.all.each { |post| post.comments.map(&:name) }
763
767
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
764
768
 
765
769
  expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)