bullet 5.9.0 → 7.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/main.yml +82 -0
- data/CHANGELOG.md +72 -0
- data/Gemfile.rails-4.0 +1 -1
- data/Gemfile.rails-4.1 +1 -1
- data/Gemfile.rails-4.2 +1 -1
- data/Gemfile.rails-5.0 +1 -1
- data/Gemfile.rails-5.1 +1 -1
- data/Gemfile.rails-5.2 +1 -1
- data/Gemfile.rails-6.0 +15 -0
- data/Gemfile.rails-6.1 +15 -0
- data/Gemfile.rails-7.0 +10 -0
- data/MIT-LICENSE +1 -1
- data/README.md +59 -33
- data/lib/bullet/active_job.rb +13 -0
- data/lib/bullet/active_record4.rb +9 -32
- data/lib/bullet/active_record41.rb +8 -27
- data/lib/bullet/active_record42.rb +9 -24
- data/lib/bullet/active_record5.rb +190 -179
- data/lib/bullet/active_record52.rb +184 -169
- data/lib/bullet/active_record60.rb +274 -0
- data/lib/bullet/active_record61.rb +274 -0
- data/lib/bullet/active_record70.rb +277 -0
- data/lib/bullet/bullet_xhr.js +64 -0
- data/lib/bullet/dependency.rb +60 -36
- data/lib/bullet/detector/association.rb +26 -20
- data/lib/bullet/detector/counter_cache.rb +15 -11
- data/lib/bullet/detector/n_plus_one_query.rb +24 -14
- data/lib/bullet/detector/unused_eager_loading.rb +8 -5
- data/lib/bullet/ext/object.rb +4 -2
- data/lib/bullet/mongoid4x.rb +3 -7
- data/lib/bullet/mongoid5x.rb +3 -7
- data/lib/bullet/mongoid6x.rb +3 -7
- data/lib/bullet/mongoid7x.rb +34 -23
- data/lib/bullet/notification/base.rb +14 -18
- data/lib/bullet/notification/n_plus_one_query.rb +2 -4
- data/lib/bullet/notification/unused_eager_loading.rb +2 -4
- data/lib/bullet/notification.rb +2 -1
- data/lib/bullet/rack.rb +55 -27
- data/lib/bullet/stack_trace_filter.rb +11 -19
- data/lib/bullet/version.rb +1 -1
- data/lib/bullet.rb +68 -42
- data/lib/generators/bullet/install_generator.rb +22 -23
- data/perf/benchmark.rb +11 -14
- data/spec/bullet/detector/counter_cache_spec.rb +6 -6
- data/spec/bullet/detector/n_plus_one_query_spec.rb +8 -4
- data/spec/bullet/detector/unused_eager_loading_spec.rb +25 -8
- data/spec/bullet/ext/object_spec.rb +10 -5
- data/spec/bullet/notification/base_spec.rb +5 -7
- data/spec/bullet/notification/n_plus_one_query_spec.rb +16 -3
- data/spec/bullet/notification/unused_eager_loading_spec.rb +5 -1
- data/spec/bullet/rack_spec.rb +161 -11
- data/spec/bullet/registry/association_spec.rb +2 -2
- data/spec/bullet/registry/base_spec.rb +1 -1
- data/spec/bullet_spec.rb +25 -44
- data/spec/integration/active_record/association_spec.rb +115 -144
- data/spec/integration/counter_cache_spec.rb +14 -34
- data/spec/integration/mongoid/association_spec.rb +19 -33
- data/spec/models/attachment.rb +5 -0
- data/spec/models/deal.rb +5 -0
- data/spec/models/post.rb +2 -0
- data/spec/models/role.rb +7 -0
- data/spec/models/submission.rb +1 -0
- data/spec/models/user.rb +2 -0
- data/spec/spec_helper.rb +4 -10
- data/spec/support/bullet_ext.rb +8 -9
- data/spec/support/mongo_seed.rb +3 -16
- data/spec/support/sqlite_seed.rb +38 -0
- data/test.sh +3 -0
- metadata +21 -8
- data/.travis.yml +0 -12
@@ -0,0 +1,274 @@
|
|
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::Associations::JoinDependency.prepend(
|
106
|
+
Module.new do
|
107
|
+
def instantiate(result_set, &block)
|
108
|
+
@bullet_eager_loadings = {}
|
109
|
+
records = super
|
110
|
+
|
111
|
+
if Bullet.start?
|
112
|
+
@bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
|
113
|
+
objects = eager_loadings_hash.keys
|
114
|
+
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(
|
115
|
+
objects,
|
116
|
+
eager_loadings_hash[objects.first].to_a
|
117
|
+
)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
records
|
121
|
+
end
|
122
|
+
|
123
|
+
def construct(ar_parent, parent, row, seen, model_cache)
|
124
|
+
if Bullet.start?
|
125
|
+
unless ar_parent.nil?
|
126
|
+
parent.children.each do |node|
|
127
|
+
key = aliases.column_alias(node, node.primary_key)
|
128
|
+
id = row[key]
|
129
|
+
next unless id.nil?
|
130
|
+
|
131
|
+
associations = node.reflection.name
|
132
|
+
Bullet::Detector::Association.add_object_associations(ar_parent, associations)
|
133
|
+
Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations)
|
134
|
+
@bullet_eager_loadings[ar_parent.class] ||= {}
|
135
|
+
@bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new
|
136
|
+
@bullet_eager_loadings[ar_parent.class][ar_parent] << associations
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
super
|
142
|
+
end
|
143
|
+
|
144
|
+
# call join associations
|
145
|
+
def construct_model(record, node, row, model_cache, id)
|
146
|
+
result = super
|
147
|
+
|
148
|
+
if Bullet.start?
|
149
|
+
associations = node.reflection.name
|
150
|
+
Bullet::Detector::Association.add_object_associations(record, associations)
|
151
|
+
Bullet::Detector::NPlusOneQuery.call_association(record, associations)
|
152
|
+
@bullet_eager_loadings[record.class] ||= {}
|
153
|
+
@bullet_eager_loadings[record.class][record] ||= Set.new
|
154
|
+
@bullet_eager_loadings[record.class][record] << associations
|
155
|
+
end
|
156
|
+
|
157
|
+
result
|
158
|
+
end
|
159
|
+
end
|
160
|
+
)
|
161
|
+
|
162
|
+
::ActiveRecord::Associations::Association.prepend(
|
163
|
+
Module.new do
|
164
|
+
def inversed_from(record)
|
165
|
+
if Bullet.start?
|
166
|
+
Bullet::Detector::NPlusOneQuery.add_inversed_object(owner, reflection.name)
|
167
|
+
end
|
168
|
+
super
|
169
|
+
end
|
170
|
+
end
|
171
|
+
)
|
172
|
+
|
173
|
+
::ActiveRecord::Associations::CollectionAssociation.prepend(
|
174
|
+
Module.new do
|
175
|
+
def load_target
|
176
|
+
records = super
|
177
|
+
|
178
|
+
if Bullet.start?
|
179
|
+
if is_a? ::ActiveRecord::Associations::ThroughAssociation
|
180
|
+
association = owner.association(reflection.through_reflection.name)
|
181
|
+
if association.loaded?
|
182
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
|
183
|
+
Array.wrap(association.target).each do |through_record|
|
184
|
+
Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
if reflection.through_reflection != through_reflection
|
189
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) unless @inversed
|
193
|
+
if records.first.class.name !~ /^HABTM_/
|
194
|
+
if records.size > 1
|
195
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
|
196
|
+
Bullet::Detector::CounterCache.add_possible_objects(records)
|
197
|
+
elsif records.size == 1
|
198
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
|
199
|
+
Bullet::Detector::CounterCache.add_impossible_object(records.first)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
records
|
204
|
+
end
|
205
|
+
|
206
|
+
def empty?
|
207
|
+
if Bullet.start? && !reflection.has_cached_counter?
|
208
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
209
|
+
end
|
210
|
+
super
|
211
|
+
end
|
212
|
+
|
213
|
+
def include?(object)
|
214
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) if Bullet.start?
|
215
|
+
super
|
216
|
+
end
|
217
|
+
end
|
218
|
+
)
|
219
|
+
|
220
|
+
::ActiveRecord::Associations::SingularAssociation.prepend(
|
221
|
+
Module.new do
|
222
|
+
# call has_one and belongs_to associations
|
223
|
+
def target
|
224
|
+
result = super()
|
225
|
+
|
226
|
+
if Bullet.start?
|
227
|
+
if owner.class.name !~ /^HABTM_/ && !@inversed
|
228
|
+
if is_a? ::ActiveRecord::Associations::ThroughAssociation
|
229
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
|
230
|
+
association = owner.association(reflection.through_reflection.name)
|
231
|
+
Array.wrap(association.target).each do |through_record|
|
232
|
+
Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
|
233
|
+
end
|
234
|
+
|
235
|
+
if reflection.through_reflection != through_reflection
|
236
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
|
237
|
+
end
|
238
|
+
end
|
239
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
240
|
+
|
241
|
+
if Bullet::Detector::NPlusOneQuery.impossible?(owner)
|
242
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
|
243
|
+
else
|
244
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
result
|
249
|
+
end
|
250
|
+
end
|
251
|
+
)
|
252
|
+
|
253
|
+
::ActiveRecord::Associations::HasManyAssociation.prepend(
|
254
|
+
Module.new do
|
255
|
+
def empty?
|
256
|
+
result = super
|
257
|
+
if Bullet.start? && !reflection.has_cached_counter?
|
258
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
259
|
+
end
|
260
|
+
result
|
261
|
+
end
|
262
|
+
|
263
|
+
def count_records
|
264
|
+
result = reflection.has_cached_counter?
|
265
|
+
if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
|
266
|
+
Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
|
267
|
+
end
|
268
|
+
super
|
269
|
+
end
|
270
|
+
end
|
271
|
+
)
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|
@@ -0,0 +1,274 @@
|
|
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::Associations::JoinDependency.prepend(
|
106
|
+
Module.new do
|
107
|
+
def instantiate(result_set, strict_loading_value, &block)
|
108
|
+
@bullet_eager_loadings = {}
|
109
|
+
records = super
|
110
|
+
|
111
|
+
if Bullet.start?
|
112
|
+
@bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
|
113
|
+
objects = eager_loadings_hash.keys
|
114
|
+
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(
|
115
|
+
objects,
|
116
|
+
eager_loadings_hash[objects.first].to_a
|
117
|
+
)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
records
|
121
|
+
end
|
122
|
+
|
123
|
+
def construct(ar_parent, parent, row, seen, model_cache, strict_loading_value)
|
124
|
+
if Bullet.start?
|
125
|
+
unless ar_parent.nil?
|
126
|
+
parent.children.each do |node|
|
127
|
+
key = aliases.column_alias(node, node.primary_key)
|
128
|
+
id = row[key]
|
129
|
+
next unless id.nil?
|
130
|
+
|
131
|
+
associations = node.reflection.name
|
132
|
+
Bullet::Detector::Association.add_object_associations(ar_parent, associations)
|
133
|
+
Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations)
|
134
|
+
@bullet_eager_loadings[ar_parent.class] ||= {}
|
135
|
+
@bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new
|
136
|
+
@bullet_eager_loadings[ar_parent.class][ar_parent] << associations
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
super
|
142
|
+
end
|
143
|
+
|
144
|
+
# call join associations
|
145
|
+
def construct_model(record, node, row, model_cache, id, strict_loading_value)
|
146
|
+
result = super
|
147
|
+
|
148
|
+
if Bullet.start?
|
149
|
+
associations = node.reflection.name
|
150
|
+
Bullet::Detector::Association.add_object_associations(record, associations)
|
151
|
+
Bullet::Detector::NPlusOneQuery.call_association(record, associations)
|
152
|
+
@bullet_eager_loadings[record.class] ||= {}
|
153
|
+
@bullet_eager_loadings[record.class][record] ||= Set.new
|
154
|
+
@bullet_eager_loadings[record.class][record] << associations
|
155
|
+
end
|
156
|
+
|
157
|
+
result
|
158
|
+
end
|
159
|
+
end
|
160
|
+
)
|
161
|
+
|
162
|
+
::ActiveRecord::Associations::Association.prepend(
|
163
|
+
Module.new do
|
164
|
+
def inversed_from(record)
|
165
|
+
if Bullet.start?
|
166
|
+
Bullet::Detector::NPlusOneQuery.add_inversed_object(owner, reflection.name)
|
167
|
+
end
|
168
|
+
super
|
169
|
+
end
|
170
|
+
end
|
171
|
+
)
|
172
|
+
|
173
|
+
::ActiveRecord::Associations::CollectionAssociation.prepend(
|
174
|
+
Module.new do
|
175
|
+
def load_target
|
176
|
+
records = super
|
177
|
+
|
178
|
+
if Bullet.start?
|
179
|
+
if is_a? ::ActiveRecord::Associations::ThroughAssociation
|
180
|
+
association = owner.association(reflection.through_reflection.name)
|
181
|
+
if association.loaded?
|
182
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
|
183
|
+
Array.wrap(association.target).each do |through_record|
|
184
|
+
Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
if reflection.through_reflection != through_reflection
|
189
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) unless @inversed
|
193
|
+
if records.first.class.name !~ /^HABTM_/
|
194
|
+
if records.size > 1
|
195
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
|
196
|
+
Bullet::Detector::CounterCache.add_possible_objects(records)
|
197
|
+
elsif records.size == 1
|
198
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
|
199
|
+
Bullet::Detector::CounterCache.add_impossible_object(records.first)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
records
|
204
|
+
end
|
205
|
+
|
206
|
+
def empty?
|
207
|
+
if Bullet.start? && !reflection.has_cached_counter?
|
208
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
209
|
+
end
|
210
|
+
super
|
211
|
+
end
|
212
|
+
|
213
|
+
def include?(object)
|
214
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) if Bullet.start?
|
215
|
+
super
|
216
|
+
end
|
217
|
+
end
|
218
|
+
)
|
219
|
+
|
220
|
+
::ActiveRecord::Associations::SingularAssociation.prepend(
|
221
|
+
Module.new do
|
222
|
+
# call has_one and belongs_to associations
|
223
|
+
def target
|
224
|
+
result = super()
|
225
|
+
|
226
|
+
if Bullet.start?
|
227
|
+
if owner.class.name !~ /^HABTM_/ && !@inversed
|
228
|
+
if is_a? ::ActiveRecord::Associations::ThroughAssociation
|
229
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
|
230
|
+
association = owner.association(reflection.through_reflection.name)
|
231
|
+
Array.wrap(association.target).each do |through_record|
|
232
|
+
Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
|
233
|
+
end
|
234
|
+
|
235
|
+
if reflection.through_reflection != through_reflection
|
236
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
|
237
|
+
end
|
238
|
+
end
|
239
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
240
|
+
|
241
|
+
if Bullet::Detector::NPlusOneQuery.impossible?(owner)
|
242
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
|
243
|
+
else
|
244
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
result
|
249
|
+
end
|
250
|
+
end
|
251
|
+
)
|
252
|
+
|
253
|
+
::ActiveRecord::Associations::HasManyAssociation.prepend(
|
254
|
+
Module.new do
|
255
|
+
def empty?
|
256
|
+
result = super
|
257
|
+
if Bullet.start? && !reflection.has_cached_counter?
|
258
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
259
|
+
end
|
260
|
+
result
|
261
|
+
end
|
262
|
+
|
263
|
+
def count_records
|
264
|
+
result = reflection.has_cached_counter?
|
265
|
+
if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
|
266
|
+
Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
|
267
|
+
end
|
268
|
+
super
|
269
|
+
end
|
270
|
+
end
|
271
|
+
)
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|