bullet 5.5.0 → 6.1.5

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