bullet 6.0.2 → 6.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -1
- data/CHANGELOG.md +7 -0
- data/README.md +4 -2
- data/lib/bullet.rb +25 -16
- data/lib/bullet/active_job.rb +1 -7
- data/lib/bullet/active_record4.rb +9 -23
- data/lib/bullet/active_record41.rb +7 -19
- data/lib/bullet/active_record42.rb +8 -16
- data/lib/bullet/active_record5.rb +188 -170
- data/lib/bullet/active_record52.rb +176 -161
- data/lib/bullet/active_record60.rb +183 -175
- data/lib/bullet/bullet_xhr.js +3 -9
- data/lib/bullet/dependency.rb +36 -34
- data/lib/bullet/detector/association.rb +24 -18
- data/lib/bullet/detector/base.rb +1 -2
- data/lib/bullet/detector/counter_cache.rb +10 -6
- data/lib/bullet/detector/n_plus_one_query.rb +18 -8
- data/lib/bullet/detector/unused_eager_loading.rb +5 -2
- data/lib/bullet/mongoid4x.rb +2 -6
- data/lib/bullet/mongoid5x.rb +2 -6
- data/lib/bullet/mongoid6x.rb +2 -6
- data/lib/bullet/mongoid7x.rb +2 -6
- 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/rack.rb +5 -3
- data/lib/bullet/stack_trace_filter.rb +5 -10
- data/lib/bullet/version.rb +1 -1
- data/lib/generators/bullet/install_generator.rb +4 -2
- data/perf/benchmark.rb +8 -14
- data/spec/bullet/detector/counter_cache_spec.rb +5 -5
- data/spec/bullet/detector/n_plus_one_query_spec.rb +7 -3
- data/spec/bullet/detector/unused_eager_loading_spec.rb +29 -12
- data/spec/bullet/notification/base_spec.rb +1 -3
- data/spec/bullet/notification/n_plus_one_query_spec.rb +18 -3
- data/spec/bullet/notification/unused_eager_loading_spec.rb +5 -1
- data/spec/bullet/rack_spec.rb +20 -5
- data/spec/bullet/registry/association_spec.rb +2 -2
- data/spec/bullet/registry/base_spec.rb +1 -1
- data/spec/bullet_spec.rb +10 -29
- data/spec/integration/active_record/association_spec.rb +42 -122
- data/spec/integration/counter_cache_spec.rb +10 -30
- data/spec/integration/mongoid/association_spec.rb +18 -32
- data/spec/models/folder.rb +1 -2
- data/spec/models/group.rb +1 -2
- data/spec/models/page.rb +1 -2
- data/spec/models/writer.rb +1 -2
- data/spec/spec_helper.rb +6 -10
- data/spec/support/bullet_ext.rb +8 -9
- data/spec/support/mongo_seed.rb +2 -16
- metadata +2 -2
@@ -13,213 +13,228 @@ module Bullet
|
|
13
13
|
module ActiveRecord
|
14
14
|
def self.enable
|
15
15
|
require 'active_record'
|
16
|
-
::ActiveRecord::Base.extend(
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
if
|
21
|
-
if result.
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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)
|
27
32
|
end
|
28
|
-
elsif result.is_a? ::ActiveRecord::Base
|
29
|
-
Bullet::Detector::NPlusOneQuery.add_impossible_object(result)
|
30
|
-
Bullet::Detector::CounterCache.add_impossible_object(result)
|
31
33
|
end
|
34
|
+
result
|
32
35
|
end
|
33
|
-
result
|
34
36
|
end
|
35
|
-
|
37
|
+
)
|
36
38
|
|
37
39
|
::ActiveRecord::Base.prepend(SaveWithBulletSupport)
|
38
40
|
|
39
|
-
::ActiveRecord::Relation.prepend(
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
if
|
46
|
-
if result.
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
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
|
52
56
|
end
|
53
57
|
end
|
58
|
+
result
|
54
59
|
end
|
55
|
-
result
|
56
60
|
end
|
57
|
-
|
58
|
-
|
59
|
-
::ActiveRecord::Associations::Preloader.prepend(
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
records.
|
65
|
-
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)
|
66
71
|
end
|
67
|
-
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, association)
|
68
72
|
end
|
73
|
+
super
|
69
74
|
end
|
70
|
-
super
|
71
75
|
end
|
72
|
-
|
73
|
-
|
74
|
-
::ActiveRecord::FinderMethods.prepend(
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
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)
|
84
89
|
end
|
85
|
-
|
90
|
+
records
|
86
91
|
end
|
87
|
-
records
|
88
92
|
end
|
89
|
-
|
90
|
-
|
91
|
-
::ActiveRecord::Associations::JoinDependency.prepend(
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
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
|
100
109
|
end
|
110
|
+
records
|
101
111
|
end
|
102
|
-
records
|
103
|
-
end
|
104
112
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
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
|
119
128
|
end
|
120
129
|
end
|
130
|
+
|
131
|
+
super
|
121
132
|
end
|
122
133
|
|
123
|
-
|
124
|
-
|
134
|
+
# call join associations
|
135
|
+
def construct_model(record, node, row, model_cache, id, aliases)
|
136
|
+
result = super
|
125
137
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
@bullet_eager_loadings[record.class] ||= {}
|
135
|
-
@bullet_eager_loadings[record.class][record] ||= Set.new
|
136
|
-
@bullet_eager_loadings[record.class][record] << associations
|
137
|
-
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
|
138
146
|
|
139
|
-
|
147
|
+
result
|
148
|
+
end
|
140
149
|
end
|
141
|
-
|
142
|
-
|
143
|
-
::ActiveRecord::Associations::CollectionAssociation.prepend(
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
if
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
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
|
153
168
|
end
|
154
|
-
|
155
|
-
if
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
Bullet::Detector::CounterCache.add_possible_objects(records)
|
164
|
-
elsif records.size == 1
|
165
|
-
Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
|
166
|
-
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
|
167
178
|
end
|
168
179
|
end
|
180
|
+
records
|
169
181
|
end
|
170
|
-
records
|
171
|
-
end
|
172
182
|
|
173
|
-
|
174
|
-
|
175
|
-
|
183
|
+
def empty?
|
184
|
+
if Bullet.start? && !reflection.has_cached_counter?
|
185
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
186
|
+
end
|
187
|
+
super
|
176
188
|
end
|
177
|
-
super
|
178
|
-
end
|
179
189
|
|
180
|
-
|
181
|
-
|
182
|
-
|
190
|
+
def include?(object)
|
191
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) if Bullet.start?
|
192
|
+
super
|
183
193
|
end
|
184
|
-
super
|
185
194
|
end
|
186
|
-
|
187
|
-
|
188
|
-
::ActiveRecord::Associations::SingularAssociation.prepend(
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
if
|
196
|
-
Bullet::Detector::NPlusOneQuery.
|
197
|
-
|
198
|
-
Bullet::Detector::NPlusOneQuery.
|
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
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
206
|
+
|
207
|
+
if Bullet::Detector::NPlusOneQuery.impossible?(owner)
|
208
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
|
209
|
+
else
|
210
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
|
211
|
+
end
|
199
212
|
end
|
200
213
|
end
|
214
|
+
result
|
201
215
|
end
|
202
|
-
result
|
203
216
|
end
|
204
|
-
|
217
|
+
)
|
205
218
|
|
206
|
-
::ActiveRecord::Associations::HasManyAssociation.prepend(
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
Bullet
|
219
|
+
::ActiveRecord::Associations::HasManyAssociation.prepend(
|
220
|
+
Module.new do
|
221
|
+
def empty?
|
222
|
+
result = super
|
223
|
+
if Bullet.start? && !reflection.has_cached_counter?
|
224
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
225
|
+
end
|
226
|
+
result
|
211
227
|
end
|
212
|
-
result
|
213
|
-
end
|
214
228
|
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
229
|
+
def count_records
|
230
|
+
result = reflection.has_cached_counter?
|
231
|
+
if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
|
232
|
+
Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
|
233
|
+
end
|
234
|
+
super
|
219
235
|
end
|
220
|
-
super
|
221
236
|
end
|
222
|
-
|
237
|
+
)
|
223
238
|
end
|
224
239
|
end
|
225
240
|
end
|
@@ -13,247 +13,255 @@ module Bullet
|
|
13
13
|
module ActiveRecord
|
14
14
|
def self.enable
|
15
15
|
require 'active_record'
|
16
|
-
::ActiveRecord::Base.extend(
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
if
|
21
|
-
if result.
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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)
|
27
32
|
end
|
28
|
-
elsif result.is_a? ::ActiveRecord::Base
|
29
|
-
Bullet::Detector::NPlusOneQuery.add_impossible_object(result)
|
30
|
-
Bullet::Detector::CounterCache.add_impossible_object(result)
|
31
33
|
end
|
34
|
+
result
|
32
35
|
end
|
33
|
-
result
|
34
36
|
end
|
35
|
-
|
37
|
+
)
|
36
38
|
|
37
39
|
::ActiveRecord::Base.prepend(SaveWithBulletSupport)
|
38
40
|
|
39
|
-
::ActiveRecord::Relation.prepend(
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
if
|
46
|
-
if result.
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
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
|
52
56
|
end
|
53
57
|
end
|
58
|
+
result
|
54
59
|
end
|
55
|
-
result
|
56
60
|
end
|
57
|
-
|
61
|
+
)
|
58
62
|
|
59
|
-
::ActiveRecord::Associations::Preloader.prepend(
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
records.
|
65
|
-
Bullet::Detector::Association.add_object_associations(record, association)
|
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)
|
66
71
|
end
|
67
|
-
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, association)
|
68
72
|
end
|
73
|
+
super
|
69
74
|
end
|
70
|
-
super
|
71
|
-
end
|
72
75
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
Bullet::Detector::
|
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)
|
79
82
|
end
|
80
|
-
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, reflection.name)
|
81
83
|
end
|
84
|
+
super
|
82
85
|
end
|
83
|
-
super
|
84
86
|
end
|
85
|
-
|
87
|
+
)
|
86
88
|
|
87
|
-
::ActiveRecord::Associations::Preloader::ThroughAssociation.prepend(
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
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
|
94
98
|
end
|
95
99
|
end
|
100
|
+
super
|
96
101
|
end
|
97
|
-
super
|
98
102
|
end
|
99
|
-
|
103
|
+
)
|
100
104
|
|
101
|
-
::ActiveRecord::FinderMethods.prepend(
|
102
|
-
|
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?
|
105
110
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
Bullet::Detector::
|
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)
|
111
116
|
end
|
112
|
-
|
117
|
+
records
|
113
118
|
end
|
114
|
-
records
|
115
119
|
end
|
116
|
-
|
120
|
+
)
|
117
121
|
|
118
|
-
::ActiveRecord::Associations::JoinDependency.prepend(
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
+
::ActiveRecord::Associations::JoinDependency.prepend(
|
123
|
+
Module.new do
|
124
|
+
def instantiate(result_set, &block)
|
125
|
+
@bullet_eager_loadings = {}
|
126
|
+
records = super
|
122
127
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
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
|
127
136
|
end
|
137
|
+
records
|
128
138
|
end
|
129
|
-
records
|
130
|
-
end
|
131
139
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
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?
|
139
147
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
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
|
146
155
|
end
|
147
156
|
end
|
157
|
+
|
158
|
+
super
|
148
159
|
end
|
149
160
|
|
150
|
-
|
151
|
-
|
161
|
+
# call join associations
|
162
|
+
def construct_model(record, node, row, model_cache, id)
|
163
|
+
result = super
|
152
164
|
|
153
|
-
|
154
|
-
|
155
|
-
|
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
|
156
173
|
|
157
|
-
|
158
|
-
associations = node.reflection.name
|
159
|
-
Bullet::Detector::Association.add_object_associations(record, associations)
|
160
|
-
Bullet::Detector::NPlusOneQuery.call_association(record, associations)
|
161
|
-
@bullet_eager_loadings[record.class] ||= {}
|
162
|
-
@bullet_eager_loadings[record.class][record] ||= Set.new
|
163
|
-
@bullet_eager_loadings[record.class][record] << associations
|
174
|
+
result
|
164
175
|
end
|
165
|
-
|
166
|
-
result
|
167
176
|
end
|
168
|
-
|
177
|
+
)
|
169
178
|
|
170
|
-
::ActiveRecord::Associations::CollectionAssociation.prepend(
|
171
|
-
|
172
|
-
|
179
|
+
::ActiveRecord::Associations::CollectionAssociation.prepend(
|
180
|
+
Module.new do
|
181
|
+
def load_target
|
182
|
+
records = super
|
173
183
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
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
|
181
191
|
|
182
|
-
|
183
|
-
|
192
|
+
if reflection.through_reflection != through_reflection
|
193
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
|
194
|
+
end
|
184
195
|
end
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
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
|
194
205
|
end
|
195
206
|
end
|
207
|
+
records
|
196
208
|
end
|
197
|
-
records
|
198
|
-
end
|
199
209
|
|
200
|
-
|
201
|
-
|
202
|
-
|
210
|
+
def empty?
|
211
|
+
if Bullet.start? && !reflection.has_cached_counter?
|
212
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
213
|
+
end
|
214
|
+
super
|
203
215
|
end
|
204
|
-
super
|
205
|
-
end
|
206
216
|
|
207
|
-
|
208
|
-
|
209
|
-
|
217
|
+
def include?(object)
|
218
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) if Bullet.start?
|
219
|
+
super
|
210
220
|
end
|
211
|
-
super
|
212
221
|
end
|
213
|
-
|
222
|
+
)
|
214
223
|
|
215
|
-
::ActiveRecord::Associations::SingularAssociation.prepend(
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
if
|
223
|
-
Bullet::Detector::NPlusOneQuery.
|
224
|
-
|
225
|
-
Bullet::Detector::NPlusOneQuery.
|
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
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
233
|
+
|
234
|
+
if Bullet::Detector::NPlusOneQuery.impossible?(owner)
|
235
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
|
236
|
+
else
|
237
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
|
238
|
+
end
|
226
239
|
end
|
227
240
|
end
|
241
|
+
result
|
228
242
|
end
|
229
|
-
result
|
230
243
|
end
|
231
|
-
|
244
|
+
)
|
232
245
|
|
233
|
-
::ActiveRecord::Associations::HasManyAssociation.prepend(
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
Bullet
|
246
|
+
::ActiveRecord::Associations::HasManyAssociation.prepend(
|
247
|
+
Module.new do
|
248
|
+
def empty?
|
249
|
+
result = super
|
250
|
+
if Bullet.start? && !reflection.has_cached_counter?
|
251
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
252
|
+
end
|
253
|
+
result
|
238
254
|
end
|
239
|
-
result
|
240
|
-
end
|
241
255
|
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
256
|
+
def count_records
|
257
|
+
result = reflection.has_cached_counter?
|
258
|
+
if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
|
259
|
+
Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
|
260
|
+
end
|
261
|
+
super
|
246
262
|
end
|
247
|
-
super
|
248
|
-
end
|
249
|
-
end)
|
250
|
-
|
251
|
-
::ActiveRecord::Associations::BelongsToAssociation.prepend(Module.new do
|
252
|
-
def writer(record)
|
253
|
-
Bullet::Detector::Association.add_object_associations(owner, reflection.name) if Bullet.start?
|
254
|
-
super
|
255
263
|
end
|
256
|
-
|
264
|
+
)
|
257
265
|
end
|
258
266
|
end
|
259
267
|
end
|