bullet 5.9.0 → 7.0.4

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 (71) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/main.yml +82 -0
  3. data/CHANGELOG.md +72 -0
  4. data/Gemfile.rails-4.0 +1 -1
  5. data/Gemfile.rails-4.1 +1 -1
  6. data/Gemfile.rails-4.2 +1 -1
  7. data/Gemfile.rails-5.0 +1 -1
  8. data/Gemfile.rails-5.1 +1 -1
  9. data/Gemfile.rails-5.2 +1 -1
  10. data/Gemfile.rails-6.0 +15 -0
  11. data/Gemfile.rails-6.1 +15 -0
  12. data/Gemfile.rails-7.0 +10 -0
  13. data/MIT-LICENSE +1 -1
  14. data/README.md +59 -33
  15. data/lib/bullet/active_job.rb +13 -0
  16. data/lib/bullet/active_record4.rb +9 -32
  17. data/lib/bullet/active_record41.rb +8 -27
  18. data/lib/bullet/active_record42.rb +9 -24
  19. data/lib/bullet/active_record5.rb +190 -179
  20. data/lib/bullet/active_record52.rb +184 -169
  21. data/lib/bullet/active_record60.rb +274 -0
  22. data/lib/bullet/active_record61.rb +274 -0
  23. data/lib/bullet/active_record70.rb +277 -0
  24. data/lib/bullet/bullet_xhr.js +64 -0
  25. data/lib/bullet/dependency.rb +60 -36
  26. data/lib/bullet/detector/association.rb +26 -20
  27. data/lib/bullet/detector/counter_cache.rb +15 -11
  28. data/lib/bullet/detector/n_plus_one_query.rb +24 -14
  29. data/lib/bullet/detector/unused_eager_loading.rb +8 -5
  30. data/lib/bullet/ext/object.rb +4 -2
  31. data/lib/bullet/mongoid4x.rb +3 -7
  32. data/lib/bullet/mongoid5x.rb +3 -7
  33. data/lib/bullet/mongoid6x.rb +3 -7
  34. data/lib/bullet/mongoid7x.rb +34 -23
  35. data/lib/bullet/notification/base.rb +14 -18
  36. data/lib/bullet/notification/n_plus_one_query.rb +2 -4
  37. data/lib/bullet/notification/unused_eager_loading.rb +2 -4
  38. data/lib/bullet/notification.rb +2 -1
  39. data/lib/bullet/rack.rb +55 -27
  40. data/lib/bullet/stack_trace_filter.rb +11 -19
  41. data/lib/bullet/version.rb +1 -1
  42. data/lib/bullet.rb +68 -42
  43. data/lib/generators/bullet/install_generator.rb +22 -23
  44. data/perf/benchmark.rb +11 -14
  45. data/spec/bullet/detector/counter_cache_spec.rb +6 -6
  46. data/spec/bullet/detector/n_plus_one_query_spec.rb +8 -4
  47. data/spec/bullet/detector/unused_eager_loading_spec.rb +25 -8
  48. data/spec/bullet/ext/object_spec.rb +10 -5
  49. data/spec/bullet/notification/base_spec.rb +5 -7
  50. data/spec/bullet/notification/n_plus_one_query_spec.rb +16 -3
  51. data/spec/bullet/notification/unused_eager_loading_spec.rb +5 -1
  52. data/spec/bullet/rack_spec.rb +161 -11
  53. data/spec/bullet/registry/association_spec.rb +2 -2
  54. data/spec/bullet/registry/base_spec.rb +1 -1
  55. data/spec/bullet_spec.rb +25 -44
  56. data/spec/integration/active_record/association_spec.rb +115 -144
  57. data/spec/integration/counter_cache_spec.rb +14 -34
  58. data/spec/integration/mongoid/association_spec.rb +19 -33
  59. data/spec/models/attachment.rb +5 -0
  60. data/spec/models/deal.rb +5 -0
  61. data/spec/models/post.rb +2 -0
  62. data/spec/models/role.rb +7 -0
  63. data/spec/models/submission.rb +1 -0
  64. data/spec/models/user.rb +2 -0
  65. data/spec/spec_helper.rb +4 -10
  66. data/spec/support/bullet_ext.rb +8 -9
  67. data/spec/support/mongo_seed.rb +3 -16
  68. data/spec/support/sqlite_seed.rb +38 -0
  69. data/test.sh +3 -0
  70. metadata +21 -8
  71. data/.travis.yml +0 -12
@@ -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
@@ -155,10 +129,8 @@ if active_record?
155
129
  expect(Bullet::Detector::Association).to be_completely_preloading_associations
156
130
  end
157
131
 
158
- 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
132
+ it 'should detect unused preload with post => comments, no category => posts' do
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,16 +195,14 @@ 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
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
 
234
202
  expect(Bullet::Detector::Association).to be_completely_preloading_associations
235
203
  end
236
204
 
237
- it 'should detect preload with post => commnets' do
205
+ it 'should detect preload with post => comments' do
238
206
  Post.first.comments.map(&:name)
239
207
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
240
208
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
@@ -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
 
@@ -362,29 +322,16 @@ if active_record?
362
322
 
363
323
  new_post.trigger_after_save = true
364
324
  new_post.save!
325
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
365
326
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
366
327
 
367
328
  expect(Bullet::Detector::Association).to be_completely_preloading_associations
368
329
  end
369
-
370
- it 'should not detect "manual" preload' do
371
- comment = Comment.all.to_a.first
372
- post = Post.find(comment.post_id)
373
- # "manually" preload with out-of-band data
374
- comment.post = post
375
- # loading it should not trigger anything
376
- comment.post
377
-
378
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
379
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
380
- end
381
330
  end
382
331
 
383
332
  context 'comment => post => category' do
384
333
  it 'should detect non preload association with comment => post' do
385
- Comment.all.each do |comment|
386
- comment.post.category.name
387
- end
334
+ Comment.all.each { |comment| comment.post.category.name }
388
335
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
389
336
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
390
337
 
@@ -401,9 +348,7 @@ if active_record?
401
348
  end
402
349
 
403
350
  it 'should detect non preload association with post => category' do
404
- Comment.includes(:post).each do |comment|
405
- comment.post.category.name
406
- end
351
+ Comment.includes(:post).each { |comment| comment.post.category.name }
407
352
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
408
353
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
409
354
 
@@ -411,9 +356,7 @@ if active_record?
411
356
  end
412
357
 
413
358
  it 'should not detect unpreload association' do
414
- Comment.includes(post: :category).each do |comment|
415
- comment.post.category.name
416
- end
359
+ Comment.includes(post: :category).each { |comment| comment.post.category.name }
417
360
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
418
361
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
419
362
 
@@ -423,9 +366,8 @@ if active_record?
423
366
 
424
367
  context 'comment => author, post => writer' do
425
368
  it 'should detect non preloaded writer' do
426
- Comment.includes(%i[author post]).where(['base_users.id = ?', BaseUser.first]).references(:base_users).each do |comment|
427
- comment.post.writer.name
428
- end
369
+ Comment.includes(%i[author post]).where(['base_users.id = ?', BaseUser.first]).references(:base_users)
370
+ .each { |comment| comment.post.writer.name }
429
371
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
430
372
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
431
373
 
@@ -433,9 +375,9 @@ if active_record?
433
375
  end
434
376
 
435
377
  it 'should detect unused preload with comment => author' do
436
- Comment.includes([:author, { post: :writer }]).where(['base_users.id = ?', BaseUser.first]).references(:base_users).each do |comment|
437
- comment.post.writer.name
438
- end
378
+ Comment.includes([:author, { post: :writer }]).where(['base_users.id = ?', BaseUser.first]).references(
379
+ :base_users
380
+ ).each { |comment| comment.post.writer.name }
439
381
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
440
382
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
441
383
 
@@ -453,21 +395,24 @@ if active_record?
453
395
  end
454
396
 
455
397
  it 'should not raise a stack error from posts to category' do
456
- expect {
457
- Comment.includes(post: :category).each do |com|
458
- com.post.category
459
- end
460
- }.not_to raise_error
398
+ expect { Comment.includes(post: :category).each { |com| com.post.category } }.not_to raise_error
461
399
  end
462
400
  end
463
401
  end
464
402
 
465
403
  describe Bullet::Detector::Association, 'has_and_belongs_to_many' do
404
+ context 'posts <=> deals' do
405
+ it 'should detect preload associations with join tables that have identifier' do
406
+ Post.includes(:deals).each { |post| post.deals.map(&:name) }
407
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
408
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
409
+
410
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
411
+ end
412
+ end
466
413
  context 'students <=> teachers' do
467
414
  it 'should detect non preload associations' do
468
- Student.all.each do |student|
469
- student.teachers.map(&:name)
470
- end
415
+ Student.all.each { |student| student.teachers.map(&:name) }
471
416
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
472
417
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
473
418
 
@@ -475,9 +420,7 @@ if active_record?
475
420
  end
476
421
 
477
422
  it 'should detect preload associations' do
478
- Student.includes(:teachers).each do |student|
479
- student.teachers.map(&:name)
480
- end
423
+ Student.includes(:teachers).each { |student| student.teachers.map(&:name) }
481
424
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
482
425
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
483
426
 
@@ -501,23 +444,29 @@ if active_record?
501
444
  end
502
445
 
503
446
  it 'should detect non preload student => teachers with empty?' do
504
- Student.all.each do |student|
505
- student.teachers.empty?
506
- end
447
+ Student.all.each { |student| student.teachers.empty? }
507
448
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
508
449
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
509
450
 
510
451
  expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Student, :teachers)
511
452
  end
512
453
  end
454
+
455
+ context 'user => roles' do
456
+ it 'should detect preload associations' do
457
+ User.first.roles.includes(:resource).each { |role| role.resource }
458
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
459
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
460
+
461
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
462
+ end
463
+ end
513
464
  end
514
465
 
515
466
  describe Bullet::Detector::Association, 'has_many :through' do
516
467
  context 'firm => clients' do
517
468
  it 'should detect non preload associations' do
518
- Firm.all.each do |firm|
519
- firm.clients.map(&:name)
520
- end
469
+ Firm.all.each { |firm| firm.clients.map(&:name) }
521
470
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
522
471
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
523
472
 
@@ -525,9 +474,15 @@ if active_record?
525
474
  end
526
475
 
527
476
  it 'should detect preload associations' do
528
- Firm.includes(:clients).each do |firm|
529
- firm.clients.map(&:name)
530
- end
477
+ Firm.preload(:clients).each { |firm| firm.clients.map(&:name) }
478
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
479
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
480
+
481
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
482
+ end
483
+
484
+ it 'should detect eager load association' do
485
+ Firm.eager_load(:clients).each { |firm| firm.clients.map(&:name) }
531
486
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
532
487
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
533
488
 
@@ -553,9 +508,7 @@ if active_record?
553
508
 
554
509
  context 'firm => clients => groups' do
555
510
  it 'should detect non preload associations' do
556
- Firm.all.each do |firm|
557
- firm.groups.map(&:name)
558
- end
511
+ Firm.all.each { |firm| firm.groups.map(&:name) }
559
512
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
560
513
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
561
514
 
@@ -563,9 +516,7 @@ if active_record?
563
516
  end
564
517
 
565
518
  it 'should detect preload associations' do
566
- Firm.includes(:groups).each do |firm|
567
- firm.groups.map(&:name)
568
- end
519
+ Firm.includes(:groups).each { |firm| firm.groups.map(&:name) }
569
520
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
570
521
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
571
522
 
@@ -593,9 +544,7 @@ if active_record?
593
544
  describe Bullet::Detector::Association, 'has_one' do
594
545
  context 'company => address' do
595
546
  it 'should detect non preload association' do
596
- Company.all.each do |company|
597
- company.address.name
598
- end
547
+ Company.all.each { |company| company.address.name }
599
548
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
600
549
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
601
550
 
@@ -603,9 +552,7 @@ if active_record?
603
552
  end
604
553
 
605
554
  it 'should detect preload association' do
606
- Company.includes(:address).each do |company|
607
- company.address.name
608
- end
555
+ Company.includes(:address).each { |company| company.address.name }
609
556
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
610
557
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
611
558
 
@@ -641,6 +588,42 @@ if active_record?
641
588
  end
642
589
  end
643
590
 
591
+ describe Bullet::Detector::Association, 'has_one :through' do
592
+ context 'user => attachment' do
593
+ it 'should detect non preload associations' do
594
+ User.all.each { |user| user.submission_attachment.file_name }
595
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
596
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
597
+
598
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(User, :submission_attachment)
599
+ end
600
+
601
+ it 'should detect preload associations' do
602
+ User.includes(:submission_attachment).each { |user| user.submission_attachment.file_name }
603
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
604
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
605
+
606
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
607
+ end
608
+
609
+ it 'should not detect preload associations' do
610
+ User.all.map(&:name)
611
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
612
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
613
+
614
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
615
+ end
616
+
617
+ it 'should detect unused preload associations' do
618
+ User.includes(:submission_attachment).map(&:name)
619
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
620
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(User, :submission_attachment)
621
+
622
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
623
+ end
624
+ end
625
+ end
626
+
644
627
  describe Bullet::Detector::Association, 'call one association that in possible objects' do
645
628
  it 'should not detect preload association' do
646
629
  Post.all
@@ -699,9 +682,7 @@ if active_record?
699
682
  describe Bullet::Detector::Association, 'STI' do
700
683
  context 'page => author' do
701
684
  it 'should detect non preload associations' do
702
- Page.all.each do |page|
703
- page.author.name
704
- end
685
+ Page.all.each { |page| page.author.name }
705
686
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
706
687
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
707
688
 
@@ -709,9 +690,7 @@ if active_record?
709
690
  end
710
691
 
711
692
  it 'should detect preload associations' do
712
- Page.includes(:author).each do |page|
713
- page.author.name
714
- end
693
+ Page.includes(:author).each { |page| page.author.name }
715
694
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
716
695
  expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
717
696
 
@@ -740,9 +719,7 @@ if active_record?
740
719
  after { Bullet.n_plus_one_query_enable = true }
741
720
 
742
721
  it 'should not detect n plus one query' do
743
- Post.all.each do |post|
744
- post.comments.map(&:name)
745
- end
722
+ Post.all.each { |post| post.comments.map(&:name) }
746
723
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
747
724
 
748
725
  expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Post, :comments)
@@ -771,9 +748,7 @@ if active_record?
771
748
  end
772
749
 
773
750
  it 'should still detect n plus one query' do
774
- Post.all.each do |post|
775
- post.comments.map(&:name)
776
- end
751
+ Post.all.each { |post| post.comments.map(&:name) }
777
752
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
778
753
 
779
754
  expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
@@ -781,14 +756,12 @@ if active_record?
781
756
  end
782
757
  end
783
758
 
784
- context 'whitelist n plus one query' do
785
- before { Bullet.add_whitelist type: :n_plus_one_query, class_name: 'Post', association: :comments }
786
- after { Bullet.clear_whitelist }
759
+ context 'add n plus one query to safelist' do
760
+ before { Bullet.add_safelist type: :n_plus_one_query, class_name: 'Post', association: :comments }
761
+ after { Bullet.clear_safelist }
787
762
 
788
763
  it 'should not detect n plus one query' do
789
- Post.all.each do |post|
790
- post.comments.map(&:name)
791
- end
764
+ Post.all.each { |post| post.comments.map(&:name) }
792
765
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
793
766
 
794
767
  expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Post, :comments)
@@ -804,9 +777,9 @@ if active_record?
804
777
  end
805
778
  end
806
779
 
807
- context 'whitelist unused eager loading' do
808
- before { Bullet.add_whitelist type: :unused_eager_loading, class_name: 'Post', association: :comments }
809
- after { Bullet.clear_whitelist }
780
+ context 'add unused eager loading to safelist' do
781
+ before { Bullet.add_safelist type: :unused_eager_loading, class_name: 'Post', association: :comments }
782
+ after { Bullet.clear_safelist }
810
783
 
811
784
  it 'should not detect unused eager loading' do
812
785
  Post.includes(:comments).map(&:name)
@@ -817,9 +790,7 @@ if active_record?
817
790
  end
818
791
 
819
792
  it 'should still detect n plus one query' do
820
- Post.all.each do |post|
821
- post.comments.map(&:name)
822
- end
793
+ Post.all.each { |post| post.comments.map(&:name) }
823
794
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
824
795
 
825
796
  expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
@@ -4,25 +4,17 @@ require 'spec_helper'
4
4
 
5
5
  if !mongoid? && active_record?
6
6
  describe Bullet::Detector::CounterCache do
7
- before(:each) do
8
- Bullet.start_request
9
- end
7
+ before(:each) { Bullet.start_request }
10
8
 
11
- after(:each) do
12
- Bullet.end_request
13
- end
9
+ after(:each) { Bullet.end_request }
14
10
 
15
11
  it 'should need counter cache with all cities' do
16
- Country.all.each do |country|
17
- country.cities.size
18
- end
12
+ Country.all.each { |country| country.cities.size }
19
13
  expect(Bullet.collected_counter_cache_notifications).not_to be_empty
20
14
  end
21
15
 
22
16
  it 'should not need counter cache if already define counter_cache' do
23
- Person.all.each do |person|
24
- person.pets.size
25
- end
17
+ Person.all.each { |person| person.pets.size }
26
18
  expect(Bullet.collected_counter_cache_notifications).to be_empty
27
19
  end
28
20
 
@@ -32,32 +24,24 @@ if !mongoid? && active_record?
32
24
  end
33
25
 
34
26
  it 'should not need counter cache without size' do
35
- Country.includes(:cities).each do |country|
36
- country.cities.empty?
37
- end
27
+ Country.includes(:cities).each { |country| country.cities.empty? }
38
28
  expect(Bullet.collected_counter_cache_notifications).to be_empty
39
29
  end
40
30
 
41
- if active_record5?
31
+ if ActiveRecord::VERSION::MAJOR > 4
42
32
  it 'should not need counter cache for has_many through' do
43
- Client.all.each do |client|
44
- client.firms.size
45
- end
33
+ Client.all.each { |client| client.firms.size }
46
34
  expect(Bullet.collected_counter_cache_notifications).to be_empty
47
35
  end
48
36
  else
49
37
  it 'should need counter cache for has_many through' do
50
- Client.all.each do |client|
51
- client.firms.size
52
- end
38
+ Client.all.each { |client| client.firms.size }
53
39
  expect(Bullet.collected_counter_cache_notifications).not_to be_empty
54
40
  end
55
41
  end
56
42
 
57
43
  it 'should not need counter cache with part of cities' do
58
- Country.all.each do |country|
59
- country.cities.where(name: 'first').size
60
- end
44
+ Country.all.each { |country| country.cities.where(name: 'first').size }
61
45
  expect(Bullet.collected_counter_cache_notifications).to be_empty
62
46
  end
63
47
 
@@ -66,21 +50,17 @@ if !mongoid? && active_record?
66
50
  after { Bullet.counter_cache_enable = true }
67
51
 
68
52
  it 'should not detect counter cache' do
69
- Country.all.each do |country|
70
- country.cities.size
71
- end
53
+ Country.all.each { |country| country.cities.size }
72
54
  expect(Bullet.collected_counter_cache_notifications).to be_empty
73
55
  end
74
56
  end
75
57
 
76
- context 'whitelist' do
77
- before { Bullet.add_whitelist type: :counter_cache, class_name: 'Country', association: :cities }
78
- after { Bullet.clear_whitelist }
58
+ context 'safelist' do
59
+ before { Bullet.add_safelist type: :counter_cache, class_name: 'Country', association: :cities }
60
+ after { Bullet.clear_safelist }
79
61
 
80
62
  it 'should not detect counter cache' do
81
- Country.all.each do |country|
82
- country.cities.size
83
- end
63
+ Country.all.each { |country| country.cities.size }
84
64
  expect(Bullet.collected_counter_cache_notifications).to be_empty
85
65
  end
86
66
  end