bullet 5.7.5 → 6.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +22 -1
  3. data/CHANGELOG.md +49 -12
  4. data/Gemfile.mongoid-7.0 +15 -0
  5. data/Gemfile.rails-4.0 +1 -1
  6. data/Gemfile.rails-4.1 +1 -1
  7. data/Gemfile.rails-4.2 +1 -1
  8. data/Gemfile.rails-5.0 +1 -1
  9. data/Gemfile.rails-5.1 +1 -1
  10. data/Gemfile.rails-5.2 +2 -2
  11. data/Gemfile.rails-6.0 +15 -0
  12. data/Gemfile.rails-6.1 +15 -0
  13. data/README.md +38 -13
  14. data/Rakefile +1 -1
  15. data/bullet.gemspec +8 -3
  16. data/lib/bullet.rb +50 -22
  17. data/lib/bullet/active_job.rb +13 -0
  18. data/lib/bullet/active_record4.rb +12 -35
  19. data/lib/bullet/active_record41.rb +10 -30
  20. data/lib/bullet/active_record42.rb +12 -27
  21. data/lib/bullet/active_record5.rb +197 -177
  22. data/lib/bullet/active_record52.rb +191 -166
  23. data/lib/bullet/active_record60.rb +278 -0
  24. data/lib/bullet/active_record61.rb +278 -0
  25. data/lib/bullet/bullet_xhr.js +63 -0
  26. data/lib/bullet/dependency.rb +54 -34
  27. data/lib/bullet/detector/association.rb +26 -20
  28. data/lib/bullet/detector/base.rb +1 -2
  29. data/lib/bullet/detector/counter_cache.rb +14 -9
  30. data/lib/bullet/detector/n_plus_one_query.rb +27 -17
  31. data/lib/bullet/detector/unused_eager_loading.rb +7 -3
  32. data/lib/bullet/ext/object.rb +5 -3
  33. data/lib/bullet/ext/string.rb +1 -1
  34. data/lib/bullet/mongoid4x.rb +4 -7
  35. data/lib/bullet/mongoid5x.rb +4 -7
  36. data/lib/bullet/mongoid6x.rb +8 -11
  37. data/lib/bullet/mongoid7x.rb +57 -0
  38. data/lib/bullet/notification/base.rb +15 -19
  39. data/lib/bullet/notification/n_plus_one_query.rb +2 -4
  40. data/lib/bullet/notification/unused_eager_loading.rb +2 -4
  41. data/lib/bullet/rack.rb +54 -28
  42. data/lib/bullet/stack_trace_filter.rb +39 -30
  43. data/lib/bullet/version.rb +1 -1
  44. data/lib/generators/bullet/install_generator.rb +26 -26
  45. data/perf/benchmark.rb +8 -14
  46. data/spec/bullet/detector/counter_cache_spec.rb +6 -6
  47. data/spec/bullet/detector/n_plus_one_query_spec.rb +30 -3
  48. data/spec/bullet/detector/unused_eager_loading_spec.rb +19 -6
  49. data/spec/bullet/ext/object_spec.rb +9 -4
  50. data/spec/bullet/notification/base_spec.rb +1 -3
  51. data/spec/bullet/notification/n_plus_one_query_spec.rb +16 -3
  52. data/spec/bullet/notification/unused_eager_loading_spec.rb +5 -1
  53. data/spec/bullet/rack_spec.rb +140 -5
  54. data/spec/bullet/registry/association_spec.rb +2 -2
  55. data/spec/bullet/registry/base_spec.rb +1 -1
  56. data/spec/bullet_spec.rb +10 -29
  57. data/spec/integration/active_record/association_spec.rb +122 -118
  58. data/spec/integration/counter_cache_spec.rb +11 -31
  59. data/spec/integration/mongoid/association_spec.rb +18 -32
  60. data/spec/models/attachment.rb +5 -0
  61. data/spec/models/client.rb +2 -0
  62. data/spec/models/firm.rb +1 -0
  63. data/spec/models/folder.rb +1 -2
  64. data/spec/models/group.rb +3 -0
  65. data/spec/models/page.rb +1 -2
  66. data/spec/models/post.rb +15 -0
  67. data/spec/models/submission.rb +1 -0
  68. data/spec/models/user.rb +1 -0
  69. data/spec/models/writer.rb +1 -2
  70. data/spec/spec_helper.rb +6 -10
  71. data/spec/support/bullet_ext.rb +8 -9
  72. data/spec/support/mongo_seed.rb +2 -16
  73. data/spec/support/sqlite_seed.rb +17 -2
  74. data/test.sh +2 -0
  75. data/update.sh +1 -0
  76. metadata +24 -11
@@ -2,17 +2,10 @@
2
2
 
3
3
  module Bullet
4
4
  module SaveWithBulletSupport
5
- def save(*args)
6
- was_new_record = new_record?
7
- super(*args).tap do |result|
8
- Bullet::Detector::NPlusOneQuery.add_impossible_object(self) if result && was_new_record
9
- end
10
- end
11
-
12
- def save!(*args)
13
- was_new_record = new_record?
14
- super(*args).tap do |result|
15
- Bullet::Detector::NPlusOneQuery.add_impossible_object(self) if result && was_new_record
5
+ def _create_record(*)
6
+ super do
7
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(self)
8
+ yield(self) if block_given?
16
9
  end
17
10
  end
18
11
  end
@@ -20,207 +13,239 @@ module Bullet
20
13
  module ActiveRecord
21
14
  def self.enable
22
15
  require 'active_record'
23
- ::ActiveRecord::Base.extend(Module.new do
24
- def find_by_sql(sql, binds = [], preparable: nil, &block)
25
- result = super
26
- if Bullet.start?
27
- if result.is_a? Array
28
- if result.size > 1
29
- Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
30
- Bullet::Detector::CounterCache.add_possible_objects(result)
31
- elsif result.size == 1
32
- Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)
33
- Bullet::Detector::CounterCache.add_impossible_object(result.first)
16
+ ::ActiveRecord::Base.extend(
17
+ Module.new do
18
+ def find_by_sql(sql, binds = [], preparable: nil, &block)
19
+ result = super
20
+ if Bullet.start?
21
+ if result.is_a? Array
22
+ if result.size > 1
23
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
24
+ Bullet::Detector::CounterCache.add_possible_objects(result)
25
+ elsif result.size == 1
26
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)
27
+ Bullet::Detector::CounterCache.add_impossible_object(result.first)
28
+ end
29
+ elsif result.is_a? ::ActiveRecord::Base
30
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(result)
31
+ Bullet::Detector::CounterCache.add_impossible_object(result)
34
32
  end
35
- elsif result.is_a? ::ActiveRecord::Base
36
- Bullet::Detector::NPlusOneQuery.add_impossible_object(result)
37
- Bullet::Detector::CounterCache.add_impossible_object(result)
38
33
  end
34
+ result
39
35
  end
40
- result
41
36
  end
42
- end)
37
+ )
43
38
 
44
39
  ::ActiveRecord::Base.prepend(SaveWithBulletSupport)
45
40
 
46
- ::ActiveRecord::Relation.prepend(Module.new do
47
- # if select a collection of objects, then these objects have possible to cause N+1 query.
48
- # if select only one object, then the only one object has impossible to cause N+1 query.
49
- def records
50
- result = super
51
- if Bullet.start?
52
- if result.first.class.name !~ /^HABTM_/
53
- if result.size > 1
54
- Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
55
- Bullet::Detector::CounterCache.add_possible_objects(result)
56
- elsif result.size == 1
57
- Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)
58
- Bullet::Detector::CounterCache.add_impossible_object(result.first)
41
+ ::ActiveRecord::Relation.prepend(
42
+ Module.new do
43
+ # if select a collection of objects, then these objects have possible to cause N+1 query.
44
+ # if select only one object, then the only one object has impossible to cause N+1 query.
45
+ def records
46
+ result = super
47
+ if Bullet.start?
48
+ if result.first.class.name !~ /^HABTM_/
49
+ if result.size > 1
50
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
51
+ Bullet::Detector::CounterCache.add_possible_objects(result)
52
+ elsif result.size == 1
53
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)
54
+ Bullet::Detector::CounterCache.add_impossible_object(result.first)
55
+ end
59
56
  end
60
57
  end
58
+ result
61
59
  end
62
- result
63
60
  end
64
- end)
65
-
66
- ::ActiveRecord::Associations::Preloader.prepend(Module.new do
67
- def preloaders_for_one(association, records, scope)
68
- if Bullet.start?
69
- records.compact!
70
- if records.first.class.name !~ /^HABTM_/
71
- records.each do |record|
72
- Bullet::Detector::Association.add_object_associations(record, association)
61
+ )
62
+
63
+ ::ActiveRecord::Associations::Preloader.prepend(
64
+ Module.new do
65
+ def preloaders_for_one(association, records, scope)
66
+ if Bullet.start?
67
+ records.compact!
68
+ if records.first.class.name !~ /^HABTM_/
69
+ records.each { |record| Bullet::Detector::Association.add_object_associations(record, association) }
70
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, association)
73
71
  end
74
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, association)
75
72
  end
73
+ super
76
74
  end
77
- super
78
75
  end
79
- end)
80
-
81
- ::ActiveRecord::FinderMethods.prepend(Module.new do
82
- # add includes in scope
83
- def find_with_associations
84
- return super { |r| yield r } if block_given?
85
- records = super
86
- if Bullet.start?
87
- associations = (eager_load_values + includes_values).uniq
88
- records.each do |record|
89
- Bullet::Detector::Association.add_object_associations(record, associations)
76
+ )
77
+
78
+ ::ActiveRecord::FinderMethods.prepend(
79
+ Module.new do
80
+ # add includes in scope
81
+ def find_with_associations
82
+ return super { |r| yield r } if block_given?
83
+
84
+ records = super
85
+ if Bullet.start?
86
+ associations = (eager_load_values + includes_values).uniq
87
+ records.each { |record| Bullet::Detector::Association.add_object_associations(record, associations) }
88
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
90
89
  end
91
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
90
+ records
92
91
  end
93
- records
94
92
  end
95
- end)
96
-
97
- ::ActiveRecord::Associations::JoinDependency.prepend(Module.new do
98
- def instantiate(result_set, &block)
99
- @bullet_eager_loadings = {}
100
- records = super
101
-
102
- if Bullet.start?
103
- @bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
104
- objects = eager_loadings_hash.keys
105
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
93
+ )
94
+
95
+ ::ActiveRecord::Associations::JoinDependency.prepend(
96
+ Module.new do
97
+ def instantiate(result_set, &block)
98
+ @bullet_eager_loadings = {}
99
+ records = super
100
+
101
+ if Bullet.start?
102
+ @bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
103
+ objects = eager_loadings_hash.keys
104
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(
105
+ objects,
106
+ eager_loadings_hash[objects.first].to_a
107
+ )
108
+ end
106
109
  end
110
+ records
107
111
  end
108
- records
109
- end
110
112
 
111
- def construct(ar_parent, parent, row, rs, seen, model_cache, aliases)
112
- if Bullet.start?
113
- unless ar_parent.nil?
114
- parent.children.each do |node|
115
- key = aliases.column_alias(node, node.primary_key)
116
- id = row[key]
117
- next unless id.nil?
118
- associations = node.reflection.name
119
- Bullet::Detector::Association.add_object_associations(ar_parent, associations)
120
- Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations)
121
- @bullet_eager_loadings[ar_parent.class] ||= {}
122
- @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new
123
- @bullet_eager_loadings[ar_parent.class][ar_parent] << associations
113
+ def construct(ar_parent, parent, row, rs, seen, model_cache, aliases)
114
+ if Bullet.start?
115
+ unless ar_parent.nil?
116
+ parent.children.each do |node|
117
+ key = aliases.column_alias(node, node.primary_key)
118
+ id = row[key]
119
+ next unless id.nil?
120
+
121
+ associations = node.reflection.name
122
+ Bullet::Detector::Association.add_object_associations(ar_parent, associations)
123
+ Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations)
124
+ @bullet_eager_loadings[ar_parent.class] ||= {}
125
+ @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new
126
+ @bullet_eager_loadings[ar_parent.class][ar_parent] << associations
127
+ end
124
128
  end
125
129
  end
130
+
131
+ super
126
132
  end
127
133
 
128
- super
129
- end
134
+ # call join associations
135
+ def construct_model(record, node, row, model_cache, id, aliases)
136
+ result = super
130
137
 
131
- # call join associations
132
- def construct_model(record, node, row, model_cache, id, aliases)
133
- result = super
134
-
135
- if Bullet.start?
136
- associations = node.reflection.name
137
- Bullet::Detector::Association.add_object_associations(record, associations)
138
- Bullet::Detector::NPlusOneQuery.call_association(record, associations)
139
- @bullet_eager_loadings[record.class] ||= {}
140
- @bullet_eager_loadings[record.class][record] ||= Set.new
141
- @bullet_eager_loadings[record.class][record] << associations
142
- end
138
+ if Bullet.start?
139
+ associations = node.reflection.name
140
+ Bullet::Detector::Association.add_object_associations(record, associations)
141
+ Bullet::Detector::NPlusOneQuery.call_association(record, associations)
142
+ @bullet_eager_loadings[record.class] ||= {}
143
+ @bullet_eager_loadings[record.class][record] ||= Set.new
144
+ @bullet_eager_loadings[record.class][record] << associations
145
+ end
143
146
 
144
- result
147
+ result
148
+ end
145
149
  end
146
- end)
147
-
148
- ::ActiveRecord::Associations::CollectionAssociation.prepend(Module.new do
149
- def load_target
150
- records = super
151
-
152
- if Bullet.start?
153
- if is_a? ::ActiveRecord::Associations::ThroughAssociation
154
- Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
155
- association = owner.association through_reflection.name
156
- Array(association.target).each do |through_record|
157
- Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
150
+ )
151
+
152
+ ::ActiveRecord::Associations::CollectionAssociation.prepend(
153
+ Module.new do
154
+ def load_target
155
+ records = super
156
+
157
+ if Bullet.start?
158
+ if is_a? ::ActiveRecord::Associations::ThroughAssociation
159
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
160
+ association = owner.association reflection.through_reflection.name
161
+ Array(association.target).each do |through_record|
162
+ Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
163
+ end
164
+
165
+ if reflection.through_reflection != through_reflection
166
+ Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
167
+ end
158
168
  end
159
- end
160
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) unless @inversed
161
- if records.first.class.name !~ /^HABTM_/
162
- if records.size > 1
163
- Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
164
- Bullet::Detector::CounterCache.add_possible_objects(records)
165
- elsif records.size == 1
166
- Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
167
- Bullet::Detector::CounterCache.add_impossible_object(records.first)
169
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) unless @inversed
170
+ if records.first.class.name !~ /^HABTM_/
171
+ if records.size > 1
172
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
173
+ Bullet::Detector::CounterCache.add_possible_objects(records)
174
+ elsif records.size == 1
175
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
176
+ Bullet::Detector::CounterCache.add_impossible_object(records.first)
177
+ end
168
178
  end
169
179
  end
180
+ records
170
181
  end
171
- records
172
- end
173
182
 
174
- def empty?
175
- if Bullet.start? && !reflection.has_cached_counter?
176
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
183
+ def empty?
184
+ if Bullet.start? && !reflection.has_cached_counter?
185
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
186
+ end
187
+ super
177
188
  end
178
- super
179
- end
180
189
 
181
- def include?(object)
182
- if Bullet.start?
183
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
190
+ def include?(object)
191
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) if Bullet.start?
192
+ super
184
193
  end
185
- super
186
194
  end
187
- end)
188
-
189
- ::ActiveRecord::Associations::SingularAssociation.prepend(Module.new do
190
- # call has_one and belongs_to associations
191
- def target
192
- result = super()
193
- if Bullet.start?
194
- if owner.class.name !~ /^HABTM_/ && !@inversed
195
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
196
- if Bullet::Detector::NPlusOneQuery.impossible?(owner)
197
- Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
198
- else
199
- Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
195
+ )
196
+
197
+ ::ActiveRecord::Associations::SingularAssociation.prepend(
198
+ Module.new do
199
+ # call has_one and belongs_to associations
200
+ def target
201
+ result = super()
202
+
203
+ if Bullet.start?
204
+ if owner.class.name !~ /^HABTM_/ && !@inversed
205
+ if is_a? ::ActiveRecord::Associations::ThroughAssociation
206
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
207
+ association = owner.association reflection.through_reflection.name
208
+ Array(association.target).each do |through_record|
209
+ Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
210
+ end
211
+
212
+ if reflection.through_reflection != through_reflection
213
+ Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
214
+ end
215
+ end
216
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
217
+
218
+ if Bullet::Detector::NPlusOneQuery.impossible?(owner)
219
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
220
+ else
221
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
222
+ end
200
223
  end
201
224
  end
225
+ result
202
226
  end
203
- result
204
227
  end
205
- end)
228
+ )
206
229
 
207
- ::ActiveRecord::Associations::HasManyAssociation.prepend(Module.new do
208
- def empty?
209
- result = super
210
- if Bullet.start? && !reflection.has_cached_counter?
211
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
230
+ ::ActiveRecord::Associations::HasManyAssociation.prepend(
231
+ Module.new do
232
+ def empty?
233
+ result = super
234
+ if Bullet.start? && !reflection.has_cached_counter?
235
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
236
+ end
237
+ result
212
238
  end
213
- result
214
- end
215
239
 
216
- def count_records
217
- result = reflection.has_cached_counter?
218
- if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
219
- Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
240
+ def count_records
241
+ result = reflection.has_cached_counter?
242
+ if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
243
+ Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
244
+ end
245
+ super
220
246
  end
221
- super
222
247
  end
223
- end)
248
+ )
224
249
  end
225
250
  end
226
251
  end
@@ -0,0 +1,278 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bullet
4
+ module SaveWithBulletSupport
5
+ def _create_record(*)
6
+ super do
7
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(self)
8
+ yield(self) if block_given?
9
+ end
10
+ end
11
+ end
12
+
13
+ module ActiveRecord
14
+ def self.enable
15
+ require 'active_record'
16
+ ::ActiveRecord::Base.extend(
17
+ Module.new do
18
+ def find_by_sql(sql, binds = [], preparable: nil, &block)
19
+ result = super
20
+ if Bullet.start?
21
+ if result.is_a? Array
22
+ if result.size > 1
23
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
24
+ Bullet::Detector::CounterCache.add_possible_objects(result)
25
+ elsif result.size == 1
26
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)
27
+ Bullet::Detector::CounterCache.add_impossible_object(result.first)
28
+ end
29
+ elsif result.is_a? ::ActiveRecord::Base
30
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(result)
31
+ Bullet::Detector::CounterCache.add_impossible_object(result)
32
+ end
33
+ end
34
+ result
35
+ end
36
+ end
37
+ )
38
+
39
+ ::ActiveRecord::Base.prepend(SaveWithBulletSupport)
40
+
41
+ ::ActiveRecord::Relation.prepend(
42
+ Module.new do
43
+ # if select a collection of objects, then these objects have possible to cause N+1 query.
44
+ # if select only one object, then the only one object has impossible to cause N+1 query.
45
+ def records
46
+ result = super
47
+ if Bullet.start?
48
+ if result.first.class.name !~ /^HABTM_/
49
+ if result.size > 1
50
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
51
+ Bullet::Detector::CounterCache.add_possible_objects(result)
52
+ elsif result.size == 1
53
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)
54
+ Bullet::Detector::CounterCache.add_impossible_object(result.first)
55
+ end
56
+ end
57
+ end
58
+ result
59
+ end
60
+ end
61
+ )
62
+
63
+ ::ActiveRecord::Associations::Preloader.prepend(
64
+ Module.new do
65
+ def preloaders_for_one(association, records, scope, polymorphic_parent)
66
+ if Bullet.start?
67
+ records.compact!
68
+ if records.first.class.name !~ /^HABTM_/
69
+ records.each { |record| Bullet::Detector::Association.add_object_associations(record, association) }
70
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, association)
71
+ end
72
+ end
73
+ super
74
+ end
75
+
76
+ def preloaders_for_reflection(reflection, records, scope)
77
+ if Bullet.start?
78
+ records.compact!
79
+ if records.first.class.name !~ /^HABTM_/
80
+ records.each { |record| Bullet::Detector::Association.add_object_associations(record, reflection.name) }
81
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, reflection.name)
82
+ end
83
+ end
84
+ super
85
+ end
86
+ end
87
+ )
88
+
89
+ ::ActiveRecord::Associations::Preloader::ThroughAssociation.prepend(
90
+ Module.new do
91
+ def preloaded_records
92
+ if Bullet.start? && !defined?(@preloaded_records)
93
+ source_preloaders.each do |source_preloader|
94
+ reflection_name = source_preloader.send(:reflection).name
95
+ source_preloader.send(:owners).each do |owner|
96
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection_name)
97
+ end
98
+ end
99
+ end
100
+ super
101
+ end
102
+ end
103
+ )
104
+
105
+ ::ActiveRecord::FinderMethods.prepend(
106
+ Module.new do
107
+ # add includes in scope
108
+ def find_with_associations
109
+ return super { |r| yield r } if block_given?
110
+
111
+ records = super
112
+ if Bullet.start?
113
+ associations = (eager_load_values + includes_values).uniq
114
+ records.each { |record| Bullet::Detector::Association.add_object_associations(record, associations) }
115
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
116
+ end
117
+ records
118
+ end
119
+ end
120
+ )
121
+
122
+ ::ActiveRecord::Associations::JoinDependency.prepend(
123
+ Module.new do
124
+ def instantiate(result_set, &block)
125
+ @bullet_eager_loadings = {}
126
+ records = super
127
+
128
+ if Bullet.start?
129
+ @bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
130
+ objects = eager_loadings_hash.keys
131
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(
132
+ objects,
133
+ eager_loadings_hash[objects.first].to_a
134
+ )
135
+ end
136
+ end
137
+ records
138
+ end
139
+
140
+ def construct(ar_parent, parent, row, seen, model_cache)
141
+ if Bullet.start?
142
+ unless ar_parent.nil?
143
+ parent.children.each do |node|
144
+ key = aliases.column_alias(node, node.primary_key)
145
+ id = row[key]
146
+ next unless id.nil?
147
+
148
+ associations = node.reflection.name
149
+ Bullet::Detector::Association.add_object_associations(ar_parent, associations)
150
+ Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations)
151
+ @bullet_eager_loadings[ar_parent.class] ||= {}
152
+ @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new
153
+ @bullet_eager_loadings[ar_parent.class][ar_parent] << associations
154
+ end
155
+ end
156
+ end
157
+
158
+ super
159
+ end
160
+
161
+ # call join associations
162
+ def construct_model(record, node, row, model_cache, id)
163
+ result = super
164
+
165
+ if Bullet.start?
166
+ associations = node.reflection.name
167
+ Bullet::Detector::Association.add_object_associations(record, associations)
168
+ Bullet::Detector::NPlusOneQuery.call_association(record, associations)
169
+ @bullet_eager_loadings[record.class] ||= {}
170
+ @bullet_eager_loadings[record.class][record] ||= Set.new
171
+ @bullet_eager_loadings[record.class][record] << associations
172
+ end
173
+
174
+ result
175
+ end
176
+ end
177
+ )
178
+
179
+ ::ActiveRecord::Associations::CollectionAssociation.prepend(
180
+ Module.new do
181
+ def load_target
182
+ records = super
183
+
184
+ if Bullet.start?
185
+ if is_a? ::ActiveRecord::Associations::ThroughAssociation
186
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
187
+ association = owner.association(reflection.through_reflection.name)
188
+ Array(association.target).each do |through_record|
189
+ Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
190
+ end
191
+
192
+ if reflection.through_reflection != through_reflection
193
+ Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
194
+ end
195
+ end
196
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) unless @inversed
197
+ if records.first.class.name !~ /^HABTM_/
198
+ if records.size > 1
199
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
200
+ Bullet::Detector::CounterCache.add_possible_objects(records)
201
+ elsif records.size == 1
202
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
203
+ Bullet::Detector::CounterCache.add_impossible_object(records.first)
204
+ end
205
+ end
206
+ end
207
+ records
208
+ end
209
+
210
+ def empty?
211
+ if Bullet.start? && !reflection.has_cached_counter?
212
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
213
+ end
214
+ super
215
+ end
216
+
217
+ def include?(object)
218
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) if Bullet.start?
219
+ super
220
+ end
221
+ end
222
+ )
223
+
224
+ ::ActiveRecord::Associations::SingularAssociation.prepend(
225
+ Module.new do
226
+ # call has_one and belongs_to associations
227
+ def target
228
+ result = super()
229
+
230
+ if Bullet.start?
231
+ if owner.class.name !~ /^HABTM_/ && !@inversed
232
+ if is_a? ::ActiveRecord::Associations::ThroughAssociation
233
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
234
+ association = owner.association(reflection.through_reflection.name)
235
+ Array(association.target).each do |through_record|
236
+ Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
237
+ end
238
+
239
+ if reflection.through_reflection != through_reflection
240
+ Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
241
+ end
242
+ end
243
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
244
+
245
+ if Bullet::Detector::NPlusOneQuery.impossible?(owner)
246
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
247
+ else
248
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
249
+ end
250
+ end
251
+ end
252
+ result
253
+ end
254
+ end
255
+ )
256
+
257
+ ::ActiveRecord::Associations::HasManyAssociation.prepend(
258
+ Module.new do
259
+ def empty?
260
+ result = super
261
+ if Bullet.start? && !reflection.has_cached_counter?
262
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
263
+ end
264
+ result
265
+ end
266
+
267
+ def count_records
268
+ result = reflection.has_cached_counter?
269
+ if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
270
+ Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
271
+ end
272
+ super
273
+ end
274
+ end
275
+ )
276
+ end
277
+ end
278
+ end