hyper-pure-sys 0.0.1
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.
- checksums.yaml +7 -0
- data/bullet-8.1.3/CHANGELOG.md +374 -0
- data/bullet-8.1.3/MIT-LICENSE +20 -0
- data/bullet-8.1.3/README.md +525 -0
- data/bullet-8.1.3/lib/bullet/active_job.rb +13 -0
- data/bullet-8.1.3/lib/bullet/active_record4.rb +197 -0
- data/bullet-8.1.3/lib/bullet/active_record41.rb +191 -0
- data/bullet-8.1.3/lib/bullet/active_record42.rb +262 -0
- data/bullet-8.1.3/lib/bullet/active_record5.rb +294 -0
- data/bullet-8.1.3/lib/bullet/active_record52.rb +278 -0
- data/bullet-8.1.3/lib/bullet/active_record60.rb +310 -0
- data/bullet-8.1.3/lib/bullet/active_record61.rb +310 -0
- data/bullet-8.1.3/lib/bullet/active_record70.rb +319 -0
- data/bullet-8.1.3/lib/bullet/active_record71.rb +319 -0
- data/bullet-8.1.3/lib/bullet/active_record72.rb +319 -0
- data/bullet-8.1.3/lib/bullet/active_record80.rb +319 -0
- data/bullet-8.1.3/lib/bullet/active_record81.rb +321 -0
- data/bullet-8.1.3/lib/bullet/bullet_xhr.js +64 -0
- data/bullet-8.1.3/lib/bullet/dependency.rb +165 -0
- data/bullet-8.1.3/lib/bullet/detector/association.rb +92 -0
- data/bullet-8.1.3/lib/bullet/detector/base.rb +8 -0
- data/bullet-8.1.3/lib/bullet/detector/counter_cache.rb +69 -0
- data/bullet-8.1.3/lib/bullet/detector/n_plus_one_query.rb +148 -0
- data/bullet-8.1.3/lib/bullet/detector/unused_eager_loading.rb +100 -0
- data/bullet-8.1.3/lib/bullet/detector.rb +11 -0
- data/bullet-8.1.3/lib/bullet/ext/object.rb +37 -0
- data/bullet-8.1.3/lib/bullet/ext/string.rb +14 -0
- data/bullet-8.1.3/lib/bullet/mongoid4x.rb +59 -0
- data/bullet-8.1.3/lib/bullet/mongoid5x.rb +59 -0
- data/bullet-8.1.3/lib/bullet/mongoid6x.rb +59 -0
- data/bullet-8.1.3/lib/bullet/mongoid7x.rb +74 -0
- data/bullet-8.1.3/lib/bullet/mongoid8x.rb +61 -0
- data/bullet-8.1.3/lib/bullet/mongoid9x.rb +74 -0
- data/bullet-8.1.3/lib/bullet/notification/base.rb +92 -0
- data/bullet-8.1.3/lib/bullet/notification/counter_cache.rb +15 -0
- data/bullet-8.1.3/lib/bullet/notification/n_plus_one_query.rb +31 -0
- data/bullet-8.1.3/lib/bullet/notification/unused_eager_loading.rb +31 -0
- data/bullet-8.1.3/lib/bullet/notification.rb +13 -0
- data/bullet-8.1.3/lib/bullet/notification_collector.rb +25 -0
- data/bullet-8.1.3/lib/bullet/rack.rb +186 -0
- data/bullet-8.1.3/lib/bullet/registry/association.rb +16 -0
- data/bullet-8.1.3/lib/bullet/registry/base.rb +46 -0
- data/bullet-8.1.3/lib/bullet/registry/call_stack.rb +17 -0
- data/bullet-8.1.3/lib/bullet/registry/object.rb +18 -0
- data/bullet-8.1.3/lib/bullet/registry.rb +10 -0
- data/bullet-8.1.3/lib/bullet/stack_trace_filter.rb +67 -0
- data/bullet-8.1.3/lib/bullet/version.rb +5 -0
- data/bullet-8.1.3/lib/bullet.rb +289 -0
- data/bullet-8.1.3/lib/generators/bullet/install_generator.rb +47 -0
- data/bullet-8.1.3/tasks/bullet_tasks.rake +11 -0
- data/hyper-pure-sys.gemspec +12 -0
- metadata +91 -0
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bullet
|
|
4
|
+
module SaveWithBulletSupport
|
|
5
|
+
def _create_record(*)
|
|
6
|
+
super do
|
|
7
|
+
Bullet::Detector::NPlusOneQuery.update_inversed_object(self)
|
|
8
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(self)
|
|
9
|
+
yield(self) if block_given?
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
module ActiveRecord
|
|
15
|
+
def self.enable
|
|
16
|
+
require 'active_record'
|
|
17
|
+
::ActiveRecord::Base.extend(
|
|
18
|
+
Module.new do
|
|
19
|
+
def find_by_sql(sql, binds = [], preparable: nil, &block)
|
|
20
|
+
result = super
|
|
21
|
+
if Bullet.start?
|
|
22
|
+
if result.is_a? Array
|
|
23
|
+
if result.size > 1
|
|
24
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
|
|
25
|
+
Bullet::Detector::CounterCache.add_possible_objects(result)
|
|
26
|
+
elsif result.size == 1
|
|
27
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)
|
|
28
|
+
Bullet::Detector::CounterCache.add_impossible_object(result.first)
|
|
29
|
+
end
|
|
30
|
+
elsif result.is_a? ::ActiveRecord::Base
|
|
31
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result)
|
|
32
|
+
Bullet::Detector::CounterCache.add_impossible_object(result)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
result
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
::ActiveRecord::Base.prepend(SaveWithBulletSupport)
|
|
41
|
+
|
|
42
|
+
::ActiveRecord::Relation.prepend(
|
|
43
|
+
Module.new do
|
|
44
|
+
# if select a collection of objects, then these objects have possible to cause N+1 query.
|
|
45
|
+
# if select only one object, then the only one object has impossible to cause N+1 query.
|
|
46
|
+
def records
|
|
47
|
+
result = super
|
|
48
|
+
if Bullet.start?
|
|
49
|
+
if result.first.class.name !~ /^HABTM_/
|
|
50
|
+
if result.size > 1
|
|
51
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
|
|
52
|
+
Bullet::Detector::CounterCache.add_possible_objects(result)
|
|
53
|
+
elsif result.size == 1
|
|
54
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)
|
|
55
|
+
Bullet::Detector::CounterCache.add_impossible_object(result.first)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
result
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
::ActiveRecord::Associations::Preloader.prepend(
|
|
65
|
+
Module.new do
|
|
66
|
+
def preloaders_for_one(association, records, scope)
|
|
67
|
+
if Bullet.start?
|
|
68
|
+
records.compact!
|
|
69
|
+
if records.first.class.name !~ /^HABTM_/
|
|
70
|
+
records.each { |record| Bullet::Detector::Association.add_object_associations(record, association) }
|
|
71
|
+
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, association)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
super
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
::ActiveRecord::FinderMethods.prepend(
|
|
80
|
+
Module.new do
|
|
81
|
+
# add includes in scope
|
|
82
|
+
def find_with_associations
|
|
83
|
+
return super { |r| yield r } if block_given?
|
|
84
|
+
|
|
85
|
+
records = super
|
|
86
|
+
if Bullet.start?
|
|
87
|
+
associations = (eager_load_values + includes_values).uniq
|
|
88
|
+
records.each { |record| Bullet::Detector::Association.add_object_associations(record, associations) }
|
|
89
|
+
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
|
|
90
|
+
end
|
|
91
|
+
records
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
::ActiveRecord::Associations::JoinDependency.prepend(
|
|
97
|
+
Module.new do
|
|
98
|
+
if ::ActiveRecord::Associations::JoinDependency.instance_method(:instantiate).parameters.last[0] == :block
|
|
99
|
+
# ActiveRecord >= 5.1.5
|
|
100
|
+
def instantiate(result_set, &block)
|
|
101
|
+
@bullet_eager_loadings = {}
|
|
102
|
+
records = super
|
|
103
|
+
|
|
104
|
+
if Bullet.start?
|
|
105
|
+
@bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
|
|
106
|
+
objects = eager_loadings_hash.keys
|
|
107
|
+
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(
|
|
108
|
+
objects,
|
|
109
|
+
eager_loadings_hash[objects.first].to_a
|
|
110
|
+
)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
records
|
|
114
|
+
end
|
|
115
|
+
else
|
|
116
|
+
# ActiveRecord <= 5.1.4
|
|
117
|
+
def instantiate(result_set, aliases)
|
|
118
|
+
@bullet_eager_loadings = {}
|
|
119
|
+
records = super
|
|
120
|
+
|
|
121
|
+
if Bullet.start?
|
|
122
|
+
@bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
|
|
123
|
+
objects = eager_loadings_hash.keys
|
|
124
|
+
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(
|
|
125
|
+
objects,
|
|
126
|
+
eager_loadings_hash[objects.first].to_a
|
|
127
|
+
)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
records
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def construct(ar_parent, parent, row, rs, seen, model_cache, aliases)
|
|
135
|
+
if Bullet.start?
|
|
136
|
+
unless ar_parent.nil?
|
|
137
|
+
parent.children.each do |node|
|
|
138
|
+
key = aliases.column_alias(node, node.primary_key)
|
|
139
|
+
id = row[key]
|
|
140
|
+
next unless id.nil?
|
|
141
|
+
|
|
142
|
+
associations = [node.reflection.name]
|
|
143
|
+
if node.reflection.through_reflection?
|
|
144
|
+
associations << node.reflection.through_reflection.name
|
|
145
|
+
end
|
|
146
|
+
associations.each do |association|
|
|
147
|
+
Bullet::Detector::Association.add_object_associations(ar_parent, association)
|
|
148
|
+
Bullet::Detector::NPlusOneQuery.call_association(ar_parent, association)
|
|
149
|
+
@bullet_eager_loadings[ar_parent.class] ||= {}
|
|
150
|
+
@bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new
|
|
151
|
+
@bullet_eager_loadings[ar_parent.class][ar_parent] << association
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
super
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# call join associations
|
|
161
|
+
def construct_model(record, node, row, model_cache, id, aliases)
|
|
162
|
+
result = super
|
|
163
|
+
|
|
164
|
+
if Bullet.start?
|
|
165
|
+
associations = [node.reflection.name]
|
|
166
|
+
if node.reflection.through_reflection?
|
|
167
|
+
associations << node.reflection.through_reflection.name
|
|
168
|
+
end
|
|
169
|
+
associations.each do |association|
|
|
170
|
+
Bullet::Detector::Association.add_object_associations(record, association)
|
|
171
|
+
Bullet::Detector::NPlusOneQuery.call_association(record, association)
|
|
172
|
+
@bullet_eager_loadings[record.class] ||= {}
|
|
173
|
+
@bullet_eager_loadings[record.class][record] ||= Set.new
|
|
174
|
+
@bullet_eager_loadings[record.class][record] << association
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
result
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
::ActiveRecord::Associations::CollectionAssociation.prepend(
|
|
184
|
+
Module.new do
|
|
185
|
+
def load_target
|
|
186
|
+
records = super
|
|
187
|
+
|
|
188
|
+
if Bullet.start?
|
|
189
|
+
if is_a? ::ActiveRecord::Associations::ThroughAssociation
|
|
190
|
+
refl = reflection.through_reflection
|
|
191
|
+
association = owner.association(refl.name)
|
|
192
|
+
if association.loaded?
|
|
193
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, refl.name)
|
|
194
|
+
Array.wrap(association.target).each do |through_record|
|
|
195
|
+
Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
if refl.through_reflection?
|
|
199
|
+
refl = refl.through_reflection while refl.through_reflection?
|
|
200
|
+
|
|
201
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, refl.name)
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, inversed: @inversed)
|
|
206
|
+
if records.first.class.name !~ /^HABTM_/
|
|
207
|
+
if records.size > 1
|
|
208
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
|
|
209
|
+
Bullet::Detector::CounterCache.add_possible_objects(records)
|
|
210
|
+
elsif records.size == 1
|
|
211
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
|
|
212
|
+
Bullet::Detector::CounterCache.add_impossible_object(records.first)
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
records
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def empty?
|
|
220
|
+
if Bullet.start? && !reflection.has_cached_counter?
|
|
221
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
|
222
|
+
end
|
|
223
|
+
super
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def include?(object)
|
|
227
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) if Bullet.start?
|
|
228
|
+
super
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
::ActiveRecord::Associations::SingularAssociation.prepend(
|
|
234
|
+
Module.new do
|
|
235
|
+
# call has_one and belongs_to associations
|
|
236
|
+
def target
|
|
237
|
+
result = super()
|
|
238
|
+
|
|
239
|
+
if Bullet.start?
|
|
240
|
+
if owner.class.name !~ /^HABTM_/
|
|
241
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, inversed: @inversed)
|
|
242
|
+
unless @inversed
|
|
243
|
+
if Bullet::Detector::NPlusOneQuery.impossible?(owner)
|
|
244
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
|
|
245
|
+
else
|
|
246
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
result
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
::ActiveRecord::Associations::HasManyAssociation.prepend(
|
|
257
|
+
Module.new do
|
|
258
|
+
def empty?
|
|
259
|
+
if Bullet.start? && !reflection.has_cached_counter?
|
|
260
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, caller_locations)
|
|
261
|
+
end
|
|
262
|
+
super
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def count_records
|
|
266
|
+
result = reflection.has_cached_counter?
|
|
267
|
+
if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
|
|
268
|
+
Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
|
|
269
|
+
end
|
|
270
|
+
super
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
::ActiveRecord::Associations::CollectionProxy.prepend(
|
|
276
|
+
Module.new do
|
|
277
|
+
def count(column_name = nil)
|
|
278
|
+
if Bullet.start? && !proxy_association.is_a?(::ActiveRecord::Associations::ThroughAssociation)
|
|
279
|
+
Bullet::Detector::CounterCache.add_counter_cache(
|
|
280
|
+
proxy_association.owner,
|
|
281
|
+
proxy_association.reflection.name
|
|
282
|
+
)
|
|
283
|
+
Bullet::Detector::NPlusOneQuery.call_association(
|
|
284
|
+
proxy_association.owner,
|
|
285
|
+
proxy_association.reflection.name
|
|
286
|
+
)
|
|
287
|
+
end
|
|
288
|
+
super(column_name)
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
)
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
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.update_inversed_object(self)
|
|
8
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(self)
|
|
9
|
+
yield(self) if block_given?
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
module ActiveRecord
|
|
15
|
+
def self.enable
|
|
16
|
+
require 'active_record'
|
|
17
|
+
::ActiveRecord::Base.extend(
|
|
18
|
+
Module.new do
|
|
19
|
+
def find_by_sql(sql, binds = [], preparable: nil, &block)
|
|
20
|
+
result = super
|
|
21
|
+
if Bullet.start?
|
|
22
|
+
if result.is_a? Array
|
|
23
|
+
if result.size > 1
|
|
24
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
|
|
25
|
+
Bullet::Detector::CounterCache.add_possible_objects(result)
|
|
26
|
+
elsif result.size == 1
|
|
27
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)
|
|
28
|
+
Bullet::Detector::CounterCache.add_impossible_object(result.first)
|
|
29
|
+
end
|
|
30
|
+
elsif result.is_a? ::ActiveRecord::Base
|
|
31
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result)
|
|
32
|
+
Bullet::Detector::CounterCache.add_impossible_object(result)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
result
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
::ActiveRecord::Base.prepend(SaveWithBulletSupport)
|
|
41
|
+
|
|
42
|
+
::ActiveRecord::Relation.prepend(
|
|
43
|
+
Module.new do
|
|
44
|
+
# if select a collection of objects, then these objects have possible to cause N+1 query.
|
|
45
|
+
# if select only one object, then the only one object has impossible to cause N+1 query.
|
|
46
|
+
def records
|
|
47
|
+
result = super
|
|
48
|
+
if Bullet.start?
|
|
49
|
+
if result.first.class.name !~ /^HABTM_/
|
|
50
|
+
if result.size > 1
|
|
51
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
|
|
52
|
+
Bullet::Detector::CounterCache.add_possible_objects(result)
|
|
53
|
+
elsif result.size == 1
|
|
54
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)
|
|
55
|
+
Bullet::Detector::CounterCache.add_impossible_object(result.first)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
result
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
::ActiveRecord::Associations::Preloader.prepend(
|
|
65
|
+
Module.new do
|
|
66
|
+
def preloaders_for_one(association, records, scope)
|
|
67
|
+
if Bullet.start?
|
|
68
|
+
records.compact!
|
|
69
|
+
if records.first.class.name !~ /^HABTM_/
|
|
70
|
+
records.each { |record| Bullet::Detector::Association.add_object_associations(record, association) }
|
|
71
|
+
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, association)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
super
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
::ActiveRecord::Associations::JoinDependency.prepend(
|
|
80
|
+
Module.new do
|
|
81
|
+
def instantiate(result_set, &block)
|
|
82
|
+
@bullet_eager_loadings = {}
|
|
83
|
+
records = super
|
|
84
|
+
|
|
85
|
+
if Bullet.start?
|
|
86
|
+
@bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
|
|
87
|
+
objects = eager_loadings_hash.keys
|
|
88
|
+
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(
|
|
89
|
+
objects,
|
|
90
|
+
eager_loadings_hash[objects.first].to_a
|
|
91
|
+
)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
records
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def construct(ar_parent, parent, row, rs, seen, model_cache, aliases)
|
|
98
|
+
if Bullet.start?
|
|
99
|
+
unless ar_parent.nil?
|
|
100
|
+
parent.children.each do |node|
|
|
101
|
+
key = aliases.column_alias(node, node.primary_key)
|
|
102
|
+
id = row[key]
|
|
103
|
+
next unless id.nil?
|
|
104
|
+
|
|
105
|
+
associations = [node.reflection.name]
|
|
106
|
+
if node.reflection.through_reflection?
|
|
107
|
+
associations << node.reflection.through_reflection.name
|
|
108
|
+
end
|
|
109
|
+
associations.each do |association|
|
|
110
|
+
Bullet::Detector::Association.add_object_associations(ar_parent, association)
|
|
111
|
+
Bullet::Detector::NPlusOneQuery.call_association(ar_parent, association)
|
|
112
|
+
@bullet_eager_loadings[ar_parent.class] ||= {}
|
|
113
|
+
@bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new
|
|
114
|
+
@bullet_eager_loadings[ar_parent.class][ar_parent] << association
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
super
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# call join associations
|
|
124
|
+
def construct_model(record, node, row, model_cache, id, aliases)
|
|
125
|
+
result = super
|
|
126
|
+
|
|
127
|
+
if Bullet.start?
|
|
128
|
+
associations = [node.reflection.name]
|
|
129
|
+
if node.reflection.through_reflection?
|
|
130
|
+
associations << node.reflection.through_reflection.name
|
|
131
|
+
end
|
|
132
|
+
associations.each do |association|
|
|
133
|
+
Bullet::Detector::Association.add_object_associations(record, association)
|
|
134
|
+
Bullet::Detector::NPlusOneQuery.call_association(record, association)
|
|
135
|
+
@bullet_eager_loadings[record.class] ||= {}
|
|
136
|
+
@bullet_eager_loadings[record.class][record] ||= Set.new
|
|
137
|
+
@bullet_eager_loadings[record.class][record] << association
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
result
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
::ActiveRecord::Associations::Association.prepend(
|
|
147
|
+
Module.new do
|
|
148
|
+
def inversed_from(record)
|
|
149
|
+
if Bullet.start?
|
|
150
|
+
Bullet::Detector::NPlusOneQuery.add_inversed_object(owner, reflection.name)
|
|
151
|
+
end
|
|
152
|
+
super
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
::ActiveRecord::Associations::CollectionAssociation.prepend(
|
|
158
|
+
Module.new do
|
|
159
|
+
def load_target
|
|
160
|
+
records = super
|
|
161
|
+
|
|
162
|
+
if Bullet.start?
|
|
163
|
+
if is_a? ::ActiveRecord::Associations::ThroughAssociation
|
|
164
|
+
association = owner.association(reflection.through_reflection.name)
|
|
165
|
+
if association.loaded?
|
|
166
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
|
|
167
|
+
Array.wrap(association.target).each do |through_record|
|
|
168
|
+
Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
if reflection.through_reflection != through_reflection
|
|
172
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, inversed: @inversed)
|
|
177
|
+
if records.first.class.name !~ /^HABTM_/
|
|
178
|
+
if records.size > 1
|
|
179
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
|
|
180
|
+
Bullet::Detector::CounterCache.add_possible_objects(records)
|
|
181
|
+
elsif records.size == 1
|
|
182
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
|
|
183
|
+
Bullet::Detector::CounterCache.add_impossible_object(records.first)
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
records
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def empty?
|
|
191
|
+
if Bullet.start? && !reflection.has_cached_counter?
|
|
192
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
|
193
|
+
end
|
|
194
|
+
super
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def include?(object)
|
|
198
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) if Bullet.start?
|
|
199
|
+
super
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
::ActiveRecord::Associations::SingularAssociation.prepend(
|
|
205
|
+
Module.new do
|
|
206
|
+
# call has_one and belongs_to associations
|
|
207
|
+
def target
|
|
208
|
+
result = super()
|
|
209
|
+
|
|
210
|
+
if Bullet.start?
|
|
211
|
+
if owner.class.name !~ /^HABTM_/
|
|
212
|
+
unless @inversed
|
|
213
|
+
if is_a? ::ActiveRecord::Associations::ThroughAssociation
|
|
214
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
|
|
215
|
+
association = owner.association reflection.through_reflection.name
|
|
216
|
+
Array.wrap(association.target).each do |through_record|
|
|
217
|
+
Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
if reflection.through_reflection != through_reflection
|
|
221
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, inversed: @inversed)
|
|
226
|
+
unless @inversed
|
|
227
|
+
if Bullet::Detector::NPlusOneQuery.impossible?(owner)
|
|
228
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
|
|
229
|
+
else
|
|
230
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
result
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
::ActiveRecord::Associations::HasManyAssociation.prepend(
|
|
241
|
+
Module.new do
|
|
242
|
+
def empty?
|
|
243
|
+
if Bullet.start? && !reflection.has_cached_counter?
|
|
244
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, caller_locations)
|
|
245
|
+
end
|
|
246
|
+
super
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def count_records
|
|
250
|
+
result = reflection.has_cached_counter?
|
|
251
|
+
if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
|
|
252
|
+
Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
|
|
253
|
+
end
|
|
254
|
+
super
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
::ActiveRecord::Associations::CollectionProxy.prepend(
|
|
260
|
+
Module.new do
|
|
261
|
+
def count(column_name = nil)
|
|
262
|
+
if Bullet.start? && !proxy_association.is_a?(::ActiveRecord::Associations::ThroughAssociation)
|
|
263
|
+
Bullet::Detector::CounterCache.add_counter_cache(
|
|
264
|
+
proxy_association.owner,
|
|
265
|
+
proxy_association.reflection.name
|
|
266
|
+
)
|
|
267
|
+
Bullet::Detector::NPlusOneQuery.call_association(
|
|
268
|
+
proxy_association.owner,
|
|
269
|
+
proxy_association.reflection.name
|
|
270
|
+
)
|
|
271
|
+
end
|
|
272
|
+
super(column_name)
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
)
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
end
|