bullet 7.0.7 → 7.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -1
  3. data/README.md +3 -0
  4. data/lib/bullet/active_record4.rb +9 -0
  5. data/lib/bullet/active_record41.rb +9 -0
  6. data/lib/bullet/active_record42.rb +9 -0
  7. data/lib/bullet/active_record5.rb +11 -0
  8. data/lib/bullet/active_record52.rb +11 -0
  9. data/lib/bullet/active_record60.rb +11 -0
  10. data/lib/bullet/active_record61.rb +11 -0
  11. data/lib/bullet/active_record70.rb +19 -6
  12. data/lib/bullet/active_record71.rb +297 -0
  13. data/lib/bullet/dependency.rb +12 -0
  14. data/lib/bullet/detector/unused_eager_loading.rb +1 -1
  15. data/lib/bullet/mongoid8x.rb +59 -0
  16. data/lib/bullet/notification/counter_cache.rb +1 -1
  17. data/lib/bullet/rack.rb +1 -1
  18. data/lib/bullet/version.rb +1 -1
  19. data/lib/bullet.rb +5 -1
  20. metadata +7 -155
  21. data/.github/workflows/main.yml +0 -82
  22. data/.gitignore +0 -15
  23. data/.rspec +0 -2
  24. data/Gemfile +0 -24
  25. data/Gemfile.mongoid +0 -12
  26. data/Gemfile.mongoid-4.0 +0 -15
  27. data/Gemfile.mongoid-5.0 +0 -15
  28. data/Gemfile.mongoid-6.0 +0 -15
  29. data/Gemfile.mongoid-7.0 +0 -15
  30. data/Gemfile.rails-4.0 +0 -16
  31. data/Gemfile.rails-4.1 +0 -16
  32. data/Gemfile.rails-4.2 +0 -16
  33. data/Gemfile.rails-5.0 +0 -15
  34. data/Gemfile.rails-5.1 +0 -15
  35. data/Gemfile.rails-5.2 +0 -15
  36. data/Gemfile.rails-6.0 +0 -15
  37. data/Gemfile.rails-6.1 +0 -15
  38. data/Gemfile.rails-7.0 +0 -10
  39. data/Guardfile +0 -8
  40. data/Hacking.md +0 -75
  41. data/Rakefile +0 -51
  42. data/bullet.gemspec +0 -33
  43. data/perf/benchmark.rb +0 -118
  44. data/rails/init.rb +0 -3
  45. data/spec/bullet/detector/association_spec.rb +0 -28
  46. data/spec/bullet/detector/base_spec.rb +0 -10
  47. data/spec/bullet/detector/counter_cache_spec.rb +0 -58
  48. data/spec/bullet/detector/n_plus_one_query_spec.rb +0 -150
  49. data/spec/bullet/detector/unused_eager_loading_spec.rb +0 -126
  50. data/spec/bullet/ext/object_spec.rb +0 -44
  51. data/spec/bullet/ext/string_spec.rb +0 -15
  52. data/spec/bullet/notification/base_spec.rb +0 -94
  53. data/spec/bullet/notification/counter_cache_spec.rb +0 -14
  54. data/spec/bullet/notification/n_plus_one_query_spec.rb +0 -29
  55. data/spec/bullet/notification/unused_eager_loading_spec.rb +0 -18
  56. data/spec/bullet/notification_collector_spec.rb +0 -34
  57. data/spec/bullet/rack_spec.rb +0 -296
  58. data/spec/bullet/registry/association_spec.rb +0 -28
  59. data/spec/bullet/registry/base_spec.rb +0 -46
  60. data/spec/bullet/registry/object_spec.rb +0 -26
  61. data/spec/bullet/stack_trace_filter_spec.rb +0 -26
  62. data/spec/bullet_spec.rb +0 -136
  63. data/spec/integration/active_record/association_spec.rb +0 -822
  64. data/spec/integration/counter_cache_spec.rb +0 -68
  65. data/spec/integration/mongoid/association_spec.rb +0 -246
  66. data/spec/models/address.rb +0 -5
  67. data/spec/models/attachment.rb +0 -5
  68. data/spec/models/author.rb +0 -5
  69. data/spec/models/base_user.rb +0 -7
  70. data/spec/models/category.rb +0 -12
  71. data/spec/models/city.rb +0 -5
  72. data/spec/models/client.rb +0 -8
  73. data/spec/models/comment.rb +0 -8
  74. data/spec/models/company.rb +0 -5
  75. data/spec/models/country.rb +0 -5
  76. data/spec/models/deal.rb +0 -5
  77. data/spec/models/document.rb +0 -7
  78. data/spec/models/entry.rb +0 -5
  79. data/spec/models/firm.rb +0 -7
  80. data/spec/models/folder.rb +0 -4
  81. data/spec/models/group.rb +0 -4
  82. data/spec/models/mongoid/address.rb +0 -9
  83. data/spec/models/mongoid/category.rb +0 -10
  84. data/spec/models/mongoid/comment.rb +0 -9
  85. data/spec/models/mongoid/company.rb +0 -9
  86. data/spec/models/mongoid/entry.rb +0 -9
  87. data/spec/models/mongoid/post.rb +0 -14
  88. data/spec/models/mongoid/user.rb +0 -7
  89. data/spec/models/newspaper.rb +0 -5
  90. data/spec/models/page.rb +0 -4
  91. data/spec/models/person.rb +0 -5
  92. data/spec/models/pet.rb +0 -5
  93. data/spec/models/post.rb +0 -34
  94. data/spec/models/relationship.rb +0 -6
  95. data/spec/models/reply.rb +0 -5
  96. data/spec/models/role.rb +0 -7
  97. data/spec/models/student.rb +0 -5
  98. data/spec/models/submission.rb +0 -7
  99. data/spec/models/teacher.rb +0 -5
  100. data/spec/models/user.rb +0 -8
  101. data/spec/models/writer.rb +0 -4
  102. data/spec/spec_helper.rb +0 -97
  103. data/spec/support/bullet_ext.rb +0 -56
  104. data/spec/support/mongo_seed.rb +0 -59
  105. data/spec/support/rack_double.rb +0 -49
  106. data/spec/support/sqlite_seed.rb +0 -284
  107. data/test.sh +0 -15
  108. data/update.sh +0 -10
@@ -1,822 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- if active_record?
6
- describe Bullet::Detector::Association, 'has_many' do
7
- context 'post => comments' do
8
- it 'should detect non preload post => comments' do
9
- Post.all.each { |post| post.comments.map(&:name) }
10
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
11
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
12
-
13
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
14
- end
15
-
16
- it 'should detect non preload post => comments for find_by_sql' do
17
- Post.find_by_sql('SELECT * FROM posts').each { |post| post.comments.map(&:name) }
18
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
19
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
20
-
21
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
22
- end
23
-
24
- it 'should detect preload with post => comments' do
25
- Post.includes(:comments).each { |post| post.comments.map(&:name) }
26
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
27
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
28
-
29
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
30
- end
31
-
32
- it 'should detect unused preload post => comments' do
33
- Post.includes(:comments).map(&:name)
34
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
35
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
36
-
37
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
38
- end
39
-
40
- it 'should not detect unused preload post => comments' do
41
- Post.all.map(&:name)
42
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
43
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
44
-
45
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
46
- end
47
-
48
- it 'should detect non preload comment => post with inverse_of' do
49
- Post.includes(:comments).each do |post|
50
- post.comments.each do |comment|
51
- comment.name
52
- comment.post.name
53
- end
54
- end
55
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
56
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
57
-
58
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
59
- end
60
-
61
- it 'should detect non preload comment => post with inverse_of from a query' do
62
- Post.first.comments.find_each do |comment|
63
- comment.name
64
- comment.post.name
65
- end
66
-
67
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
68
- expect(Post.first.comments.count).not_to eq(0)
69
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
70
-
71
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
72
- end
73
-
74
- it 'should detect non preload post => comments with empty?' do
75
- Post.all.each { |post| post.comments.empty? }
76
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
77
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
78
-
79
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
80
- end
81
-
82
- it 'should detect non preload post => comments with include?' do
83
- comment = Comment.last
84
- Post.all.each { |post| post.comments.include?(comment) }
85
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
86
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
87
-
88
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
89
- end
90
-
91
- it 'should not detect unused preload person => pets with empty?' do
92
- Person.all.each { |person| person.pets.empty? }
93
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
94
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
95
-
96
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
97
- end
98
- end
99
-
100
- context 'category => posts => comments' do
101
- it 'should detect non preload category => posts => comments' do
102
- Category.all.each { |category| category.posts.each { |post| post.comments.map(&:name) } }
103
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
104
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
105
-
106
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :posts)
107
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
108
- end
109
-
110
- it 'should detect preload category => posts, but no post => comments' do
111
- Category.includes(:posts).each { |category| category.posts.each { |post| post.comments.map(&:name) } }
112
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
113
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
114
-
115
- expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Category, :posts)
116
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
117
- end
118
-
119
- it 'should detect preload with category => posts => comments' do
120
- Category.includes(posts: :comments).each { |category| category.posts.each { |post| post.comments.map(&:name) } }
121
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
122
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
123
-
124
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
125
- end
126
-
127
- it 'should detect preload with category => posts => comments with posts.id > 0' do
128
- Category.includes(posts: :comments).where('posts.id > 0').references(:posts).each do |category|
129
- category.posts.each { |post| post.comments.map(&:name) }
130
- end
131
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
132
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
133
-
134
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
135
- end
136
-
137
- it 'should detect unused preload with category => posts => comments' do
138
- Category.includes(posts: :comments).map(&:name)
139
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
140
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
141
-
142
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
143
- end
144
-
145
- it 'should detect unused preload with post => comments, no category => posts' do
146
- Category.includes(posts: :comments).each { |category| category.posts.map(&:name) }
147
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
148
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
149
-
150
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
151
- end
152
- end
153
-
154
- context 'category => posts, category => entries' do
155
- it 'should detect non preload with category => [posts, entries]' do
156
- Category.all.each do |category|
157
- category.posts.map(&:name)
158
- category.entries.map(&:name)
159
- end
160
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
161
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
162
-
163
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :posts)
164
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :entries)
165
- end
166
-
167
- it 'should detect preload with category => posts, but not with category => entries' do
168
- Category.includes(:posts).each do |category|
169
- category.posts.map(&:name)
170
- category.entries.map(&:name)
171
- end
172
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
173
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
174
-
175
- expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Category, :posts)
176
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :entries)
177
- end
178
-
179
- it 'should detect preload with category => [posts, entries]' do
180
- Category.includes(%i[posts entries]).each do |category|
181
- category.posts.map(&:name)
182
- category.entries.map(&:name)
183
- end
184
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
185
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
186
-
187
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
188
- end
189
-
190
- it 'should detect unused preload with category => [posts, entries]' do
191
- Category.includes(%i[posts entries]).map(&:name)
192
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
193
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Category, :posts)
194
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Category, :entries)
195
-
196
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
197
- end
198
-
199
- it 'should detect unused preload with category => entries, but not with category => posts' do
200
- Category.includes(%i[posts entries]).each { |category| category.posts.map(&:name) }
201
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
202
- expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Category, :posts)
203
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Category, :entries)
204
-
205
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
206
- end
207
- end
208
-
209
- context 'post => comment' do
210
- it 'should detect unused preload with post => comments' do
211
- Post.includes(:comments).each { |post| post.comments.first&.name }
212
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
213
- expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Post, :comments)
214
-
215
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
216
- end
217
-
218
- it 'should detect preload with post => comments' do
219
- Post.first.comments.map(&:name)
220
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
221
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
222
-
223
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
224
- end
225
-
226
- it 'should not detect unused preload with category => posts' do
227
- category = Category.first
228
- category.draft_post.destroy!
229
- post = category.draft_post
230
- post.update!(link: true)
231
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
232
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
233
-
234
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
235
-
236
- Support::SqliteSeed.setup_db
237
- Support::SqliteSeed.seed_db
238
- end
239
- end
240
-
241
- context 'category => posts => writer' do
242
- it 'should not detect unused preload associations' do
243
- category = Category.includes(posts: :writer).order('id DESC').find_by_name('first')
244
- category.posts.map do |post|
245
- post.name
246
- post.writer.name
247
- end
248
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
249
- expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Category, :posts)
250
- expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Post, :writer)
251
- end
252
- end
253
-
254
- context 'scope for_category_name' do
255
- it 'should detect preload with post => category' do
256
- Post.in_category_name('first').references(:categories).each { |post| post.category.name }
257
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
258
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
259
-
260
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
261
- end
262
-
263
- it 'should not be unused preload post => category' do
264
- Post.in_category_name('first').references(:categories).map(&:name)
265
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
266
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
267
-
268
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
269
- end
270
- end
271
-
272
- context 'scope preload_comments' do
273
- it 'should detect preload post => comments with scope' do
274
- Post.preload_comments.each { |post| post.comments.map(&:name) }
275
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
276
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
277
-
278
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
279
- end
280
-
281
- it 'should detect unused preload with scope' do
282
- Post.preload_comments.map(&:name)
283
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
284
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
285
-
286
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
287
- end
288
- end
289
- end
290
-
291
- describe Bullet::Detector::Association, 'belongs_to' do
292
- context 'comment => post' do
293
- it 'should detect non preload with comment => post' do
294
- Comment.all.each { |comment| comment.post.name }
295
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
296
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
297
-
298
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Comment, :post)
299
- end
300
-
301
- it 'should detect preload with one comment => post' do
302
- Comment.first.post.name
303
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
304
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
305
-
306
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
307
- end
308
-
309
- it 'should detect preload with comment => post' do
310
- Comment.includes(:post).each { |comment| comment.post.name }
311
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
312
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
313
-
314
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
315
- end
316
-
317
- it 'should not detect preload with comment => post' do
318
- Comment.all.map(&:name)
319
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
320
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
321
-
322
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
323
- end
324
-
325
- it 'should detect unused preload with comment => post' do
326
- Comment.includes(:post).map(&:name)
327
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
328
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Comment, :post)
329
-
330
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
331
- end
332
-
333
- it 'should not detect newly assigned object in an after_save' do
334
- new_post = Post.new(category: Category.first)
335
-
336
- new_post.trigger_after_save = true
337
- new_post.save!
338
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
339
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
340
-
341
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
342
- end
343
- end
344
-
345
- context 'comment => post => category' do
346
- it 'should detect non preload association with comment => post' do
347
- Comment.all.each { |comment| comment.post.category.name }
348
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
349
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
350
-
351
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Comment, :post)
352
- end
353
-
354
- it 'should not detect non preload association with only one comment' do
355
- Comment.first.post.category.name
356
-
357
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
358
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
359
-
360
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
361
- end
362
-
363
- it 'should detect non preload association with post => category' do
364
- Comment.includes(:post).each { |comment| comment.post.category.name }
365
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
366
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
367
-
368
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :category)
369
- end
370
-
371
- it 'should not detect unpreload association' do
372
- Comment.includes(post: :category).each { |comment| comment.post.category.name }
373
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
374
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
375
-
376
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
377
- end
378
- end
379
-
380
- context 'comment => author, post => writer' do
381
- it 'should detect non preloaded writer' do
382
- Comment.includes(%i[author post]).where(['base_users.id = ?', BaseUser.first]).references(:base_users)
383
- .each { |comment| comment.post.writer.name }
384
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
385
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
386
-
387
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :writer)
388
- end
389
-
390
- it 'should detect unused preload with comment => author' do
391
- Comment.includes([:author, { post: :writer }]).where(['base_users.id = ?', BaseUser.first]).references(
392
- :base_users
393
- ).each { |comment| comment.post.writer.name }
394
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
395
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
396
-
397
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
398
- end
399
-
400
- it 'should detect non preloading with writer => newspaper' do
401
- Comment.includes(post: :writer).where("posts.name like '%first%'").references(:posts).each do |comment|
402
- comment.post.writer.newspaper.name
403
- end
404
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
405
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
406
-
407
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Writer, :newspaper)
408
- end
409
-
410
- it 'should not raise a stack error from posts to category' do
411
- expect { Comment.includes(post: :category).each { |com| com.post.category } }.not_to raise_error
412
- end
413
- end
414
- end
415
-
416
- describe Bullet::Detector::Association, 'has_and_belongs_to_many' do
417
- context 'posts <=> deals' do
418
- it 'should detect preload associations with join tables that have identifier' do
419
- Post.includes(:deals).each { |post| post.deals.map(&:name) }
420
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
421
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
422
-
423
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
424
- end
425
- end
426
- context 'students <=> teachers' do
427
- it 'should detect non preload associations' do
428
- Student.all.each { |student| student.teachers.map(&:name) }
429
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
430
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
431
-
432
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Student, :teachers)
433
- end
434
-
435
- it 'should detect preload associations' do
436
- Student.includes(:teachers).each { |student| student.teachers.map(&:name) }
437
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
438
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
439
-
440
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
441
- end
442
-
443
- it 'should detect unused preload associations' do
444
- Student.includes(:teachers).map(&:name)
445
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
446
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Student, :teachers)
447
-
448
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
449
- end
450
-
451
- it 'should detect no unused preload associations' do
452
- Student.all.map(&:name)
453
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
454
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
455
-
456
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
457
- end
458
-
459
- it 'should detect non preload student => teachers with empty?' do
460
- Student.all.each { |student| student.teachers.empty? }
461
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
462
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
463
-
464
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Student, :teachers)
465
- end
466
- end
467
-
468
- context 'user => roles' do
469
- it 'should detect preload associations' do
470
- User.first.roles.includes(:resource).each { |role| role.resource }
471
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
472
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
473
-
474
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
475
- end
476
- end
477
- end
478
-
479
- describe Bullet::Detector::Association, 'has_many :through' do
480
- context 'firm => clients' do
481
- it 'should detect non preload associations' do
482
- Firm.all.each { |firm| firm.clients.map(&:name) }
483
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
484
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
485
-
486
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Firm, :clients)
487
- end
488
-
489
- it 'should detect preload associations' do
490
- Firm.preload(:clients).each { |firm| firm.clients.map(&:name) }
491
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
492
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
493
-
494
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
495
- end
496
-
497
- it 'should detect eager load association' do
498
- Firm.eager_load(:clients).each { |firm| firm.clients.map(&:name) }
499
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
500
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
501
-
502
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
503
- end
504
-
505
- it 'should not detect preload associations' do
506
- Firm.all.map(&:name)
507
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
508
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
509
-
510
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
511
- end
512
-
513
- it 'should detect unused preload associations' do
514
- Firm.includes(:clients).map(&:name)
515
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
516
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Firm, :clients)
517
-
518
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
519
- end
520
- end
521
-
522
- context 'firm => clients => groups' do
523
- it 'should detect non preload associations' do
524
- Firm.all.each { |firm| firm.groups.map(&:name) }
525
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
526
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
527
-
528
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Firm, :groups)
529
- end
530
-
531
- it 'should detect preload associations' do
532
- Firm.preload(:groups).each { |firm| firm.groups.map(&:name) }
533
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
534
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
535
-
536
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
537
- end
538
-
539
- it 'should detect eager load associations' do
540
- Firm.eager_load(:groups).each { |firm| firm.groups.map(&:name) }
541
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
542
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
543
-
544
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
545
- end
546
-
547
- it 'should not detect preload associations' do
548
- Firm.all.map(&:name)
549
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
550
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
551
-
552
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
553
- end
554
-
555
- it 'should detect unused preload associations' do
556
- Firm.includes(:groups).map(&:name)
557
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
558
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Firm, :groups)
559
-
560
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
561
- end
562
- end
563
- end
564
-
565
- describe Bullet::Detector::Association, 'has_one' do
566
- context 'company => address' do
567
- it 'should detect non preload association' do
568
- Company.all.each { |company| company.address.name }
569
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
570
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
571
-
572
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Company, :address)
573
- end
574
-
575
- it 'should detect preload association' do
576
- Company.includes(:address).each { |company| company.address.name }
577
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
578
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
579
-
580
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
581
- end
582
-
583
- it 'should not detect preload association' do
584
- Company.all.map(&:name)
585
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
586
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
587
-
588
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
589
- end
590
-
591
- it 'should detect unused preload association' do
592
- Company.includes(:address).map(&:name)
593
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
594
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Company, :address)
595
-
596
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
597
- end
598
- end
599
- end
600
-
601
- describe Bullet::Detector::Association, 'has_one => has_many' do
602
- it 'should not detect preload association' do
603
- user = User.first
604
- user.submission.replies.map(&:name)
605
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
606
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
607
-
608
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
609
- end
610
- end
611
-
612
- describe Bullet::Detector::Association, 'has_one :through' do
613
- context 'user => attachment' do
614
- it 'should detect non preload associations' do
615
- User.all.each { |user| user.submission_attachment.file_name }
616
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
617
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
618
-
619
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(User, :submission_attachment)
620
- end
621
-
622
- it 'should detect preload associations' do
623
- User.includes(:submission_attachment).each { |user| user.submission_attachment.file_name }
624
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
625
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
626
-
627
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
628
- end
629
-
630
- it 'should not detect preload associations' do
631
- User.all.map(&:name)
632
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
633
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
634
-
635
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
636
- end
637
-
638
- it 'should detect unused preload associations' do
639
- User.includes(:submission_attachment).map(&:name)
640
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
641
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(User, :submission_attachment)
642
-
643
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
644
- end
645
- end
646
- end
647
-
648
- describe Bullet::Detector::Association, 'call one association that in possible objects' do
649
- it 'should not detect preload association' do
650
- Post.all
651
- Post.first.comments.map(&:name)
652
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
653
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
654
-
655
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
656
- end
657
- end
658
-
659
- describe Bullet::Detector::Association, 'query immediately after creation' do
660
- context 'with save' do
661
- context 'document => children' do
662
- it 'should not detect non preload associations' do
663
- document1 = Document.new
664
- document1.children.build
665
- document1.save
666
-
667
- document2 = Document.new(parent: document1)
668
- document2.save
669
- document2.parent
670
-
671
- document1.children.each.first
672
-
673
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
674
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
675
-
676
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
677
- end
678
- end
679
- end
680
-
681
- context 'with save!' do
682
- context 'document => children' do
683
- it 'should not detect non preload associations' do
684
- document1 = Document.new
685
- document1.children.build
686
- document1.save!
687
-
688
- document2 = Document.new(parent: document1)
689
- document2.save!
690
- document2.parent
691
-
692
- document1.children.each.first
693
-
694
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
695
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
696
-
697
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
698
- end
699
- end
700
- end
701
- end
702
-
703
- describe Bullet::Detector::Association, 'STI' do
704
- context 'page => author' do
705
- it 'should detect non preload associations' do
706
- Page.all.each { |page| page.author.name }
707
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
708
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
709
-
710
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Page, :author)
711
- end
712
-
713
- it 'should detect preload associations' do
714
- Page.includes(:author).each { |page| page.author.name }
715
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
716
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
717
-
718
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
719
- end
720
-
721
- it 'should detect unused preload associations' do
722
- Page.includes(:author).map(&:name)
723
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
724
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Page, :author)
725
-
726
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
727
- end
728
-
729
- it 'should not detect preload associations' do
730
- Page.all.map(&:name)
731
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
732
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
733
-
734
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
735
- end
736
- end
737
-
738
- context 'disable n plus one query' do
739
- before { Bullet.n_plus_one_query_enable = false }
740
- after { Bullet.n_plus_one_query_enable = true }
741
-
742
- it 'should not detect n plus one query' do
743
- Post.all.each { |post| post.comments.map(&:name) }
744
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
745
-
746
- expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Post, :comments)
747
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
748
- end
749
-
750
- it 'should still detect unused eager loading' do
751
- Post.includes(:comments).map(&:name)
752
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
753
-
754
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
755
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
756
- end
757
- end
758
-
759
- context 'disable unused eager loading' do
760
- before { Bullet.unused_eager_loading_enable = false }
761
- after { Bullet.unused_eager_loading_enable = true }
762
-
763
- it 'should not detect unused eager loading' do
764
- Post.includes(:comments).map(&:name)
765
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
766
-
767
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
768
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
769
- end
770
-
771
- it 'should still detect n plus one query' do
772
- Post.all.each { |post| post.comments.map(&:name) }
773
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
774
-
775
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
776
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
777
- end
778
- end
779
-
780
- context 'add n plus one query to safelist' do
781
- before { Bullet.add_safelist type: :n_plus_one_query, class_name: 'Post', association: :comments }
782
- after { Bullet.clear_safelist }
783
-
784
- it 'should not detect n plus one query' do
785
- Post.all.each { |post| post.comments.map(&:name) }
786
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
787
-
788
- expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Post, :comments)
789
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
790
- end
791
-
792
- it 'should still detect unused eager loading' do
793
- Post.includes(:comments).map(&:name)
794
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
795
-
796
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
797
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
798
- end
799
- end
800
-
801
- context 'add unused eager loading to safelist' do
802
- before { Bullet.add_safelist type: :unused_eager_loading, class_name: 'Post', association: :comments }
803
- after { Bullet.clear_safelist }
804
-
805
- it 'should not detect unused eager loading' do
806
- Post.includes(:comments).map(&:name)
807
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
808
-
809
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
810
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
811
- end
812
-
813
- it 'should still detect n plus one query' do
814
- Post.all.each { |post| post.comments.map(&:name) }
815
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
816
-
817
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
818
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
819
- end
820
- end
821
- end
822
- end