bullet 8.1.2 → 8.2.0

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.
@@ -1,303 +0,0 @@
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, polymorphic_parent)
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
-
77
- def preloaders_for_reflection(reflection, records, scope)
78
- if Bullet.start?
79
- records.compact!
80
- if records.first.class.name !~ /^HABTM_/
81
- records.each { |record| Bullet::Detector::Association.add_object_associations(record, reflection.name) }
82
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, reflection.name)
83
- end
84
- end
85
- super
86
- end
87
- end
88
- )
89
-
90
- ::ActiveRecord::Associations::Preloader::ThroughAssociation.prepend(
91
- Module.new do
92
- def preloaded_records
93
- if Bullet.start? && !defined?(@preloaded_records)
94
- source_preloaders.each do |source_preloader|
95
- reflection_name = source_preloader.send(:reflection).name
96
- source_preloader.send(:owners).each do |owner|
97
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection_name)
98
- end
99
- end
100
- end
101
- super
102
- end
103
- end
104
- )
105
-
106
- ::ActiveRecord::Associations::JoinDependency.prepend(
107
- Module.new do
108
- def instantiate(result_set, &block)
109
- @bullet_eager_loadings = {}
110
- records = super
111
-
112
- if Bullet.start?
113
- @bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
114
- objects = eager_loadings_hash.keys
115
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(
116
- objects,
117
- eager_loadings_hash[objects.first].to_a
118
- )
119
- end
120
- end
121
- records
122
- end
123
-
124
- def construct(ar_parent, parent, row, seen, model_cache)
125
- if Bullet.start?
126
- unless ar_parent.nil?
127
- parent.children.each do |node|
128
- key = aliases.column_alias(node, node.primary_key)
129
- id = row[key]
130
- next unless id.nil?
131
-
132
- associations = [node.reflection.name]
133
- if node.reflection.through_reflection?
134
- associations << node.reflection.through_reflection.name
135
- end
136
- associations.each do |association|
137
- Bullet::Detector::Association.add_object_associations(ar_parent, association)
138
- Bullet::Detector::NPlusOneQuery.call_association(ar_parent, association)
139
- @bullet_eager_loadings[ar_parent.class] ||= {}
140
- @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new
141
- @bullet_eager_loadings[ar_parent.class][ar_parent] << association
142
- end
143
- end
144
- end
145
- end
146
-
147
- super
148
- end
149
-
150
- # call join associations
151
- def construct_model(record, node, row, model_cache, id)
152
- result = super
153
-
154
- if Bullet.start?
155
- associations = [node.reflection.name]
156
- if node.reflection.through_reflection?
157
- associations << node.reflection.through_reflection.name
158
- end
159
- associations.each do |association|
160
- Bullet::Detector::Association.add_object_associations(record, association)
161
- Bullet::Detector::NPlusOneQuery.call_association(record, association)
162
- @bullet_eager_loadings[record.class] ||= {}
163
- @bullet_eager_loadings[record.class][record] ||= Set.new
164
- @bullet_eager_loadings[record.class][record] << association
165
- end
166
- end
167
-
168
- result
169
- end
170
- end
171
- )
172
-
173
- ::ActiveRecord::Associations::Association.prepend(
174
- Module.new do
175
- def inversed_from(record)
176
- if Bullet.start?
177
- Bullet::Detector::NPlusOneQuery.add_inversed_object(owner, reflection.name)
178
- end
179
- super
180
- end
181
- end
182
- )
183
-
184
- ::ActiveRecord::Associations::CollectionAssociation.prepend(
185
- Module.new do
186
- def load_target
187
- records = super
188
-
189
- if Bullet.start?
190
- if is_a? ::ActiveRecord::Associations::ThroughAssociation
191
- association = owner.association(reflection.through_reflection.name)
192
- if association.loaded?
193
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.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 reflection.through_reflection != through_reflection
199
- Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
200
- end
201
- end
202
- end
203
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) unless @inversed
204
- if records.first.class.name !~ /^HABTM_/
205
- if records.size > 1
206
- Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
207
- Bullet::Detector::CounterCache.add_possible_objects(records)
208
- elsif records.size == 1
209
- Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
210
- Bullet::Detector::CounterCache.add_impossible_object(records.first)
211
- end
212
- end
213
- end
214
- records
215
- end
216
-
217
- def empty?
218
- if Bullet.start? && !reflection.has_cached_counter?
219
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, caller_locations)
220
- end
221
- super
222
- end
223
-
224
- def include?(object)
225
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, caller_locations) if Bullet.start?
226
- super
227
- end
228
- end
229
- )
230
-
231
- ::ActiveRecord::Associations::SingularAssociation.prepend(
232
- Module.new do
233
- # call has_one and belongs_to associations
234
- def target
235
- result = super()
236
-
237
- if Bullet.start?
238
- if owner.class.name !~ /^HABTM_/ && !@inversed
239
- if is_a? ::ActiveRecord::Associations::ThroughAssociation
240
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
241
- association = owner.association(reflection.through_reflection.name)
242
- Array.wrap(association.target).each do |through_record|
243
- Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
244
- end
245
-
246
- if reflection.through_reflection != through_reflection
247
- Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
248
- end
249
- end
250
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
251
-
252
- if Bullet::Detector::NPlusOneQuery.impossible?(owner)
253
- Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
254
- else
255
- Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
256
- end
257
- end
258
- end
259
- result
260
- end
261
- end
262
- )
263
-
264
- ::ActiveRecord::Associations::HasManyAssociation.prepend(
265
- Module.new do
266
- def empty?
267
- if Bullet.start? && !reflection.has_cached_counter?
268
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, caller_locations)
269
- end
270
- super
271
- end
272
-
273
- def count_records
274
- result = reflection.has_cached_counter?
275
- if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
276
- Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
277
- end
278
- super
279
- end
280
- end
281
- )
282
-
283
- ::ActiveRecord::Associations::CollectionProxy.prepend(
284
- Module.new do
285
- def count(column_name = nil)
286
- if Bullet.start? && !proxy_association.is_a?(::ActiveRecord::Associations::ThroughAssociation)
287
- Bullet::Detector::CounterCache.add_counter_cache(
288
- proxy_association.owner,
289
- proxy_association.reflection.name
290
- )
291
- Bullet::Detector::NPlusOneQuery.call_association(
292
- proxy_association.owner,
293
- proxy_association.reflection.name,
294
- caller_locations
295
- )
296
- end
297
- super(column_name)
298
- end
299
- end
300
- )
301
- end
302
- end
303
- end
@@ -1,303 +0,0 @@
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, polymorphic_parent)
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
-
77
- def preloaders_for_reflection(reflection, records, scope)
78
- if Bullet.start?
79
- records.compact!
80
- if records.first.class.name !~ /^HABTM_/
81
- records.each { |record| Bullet::Detector::Association.add_object_associations(record, reflection.name) }
82
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, reflection.name)
83
- end
84
- end
85
- super
86
- end
87
- end
88
- )
89
-
90
- ::ActiveRecord::Associations::Preloader::ThroughAssociation.prepend(
91
- Module.new do
92
- def preloaded_records
93
- if Bullet.start? && !defined?(@preloaded_records)
94
- source_preloaders.each do |source_preloader|
95
- reflection_name = source_preloader.send(:reflection).name
96
- source_preloader.send(:owners).each do |owner|
97
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection_name)
98
- end
99
- end
100
- end
101
- super
102
- end
103
- end
104
- )
105
-
106
- ::ActiveRecord::Associations::JoinDependency.prepend(
107
- Module.new do
108
- def instantiate(result_set, strict_loading_value, &block)
109
- @bullet_eager_loadings = {}
110
- records = super
111
-
112
- if Bullet.start?
113
- @bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
114
- objects = eager_loadings_hash.keys
115
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(
116
- objects,
117
- eager_loadings_hash[objects.first].to_a
118
- )
119
- end
120
- end
121
- records
122
- end
123
-
124
- def construct(ar_parent, parent, row, seen, model_cache, strict_loading_value)
125
- if Bullet.start?
126
- unless ar_parent.nil?
127
- parent.children.each do |node|
128
- key = aliases.column_alias(node, node.primary_key)
129
- id = row[key]
130
- next unless id.nil?
131
-
132
- associations = [node.reflection.name]
133
- if node.reflection.through_reflection?
134
- associations << node.reflection.through_reflection.name
135
- end
136
- associations.each do |association|
137
- Bullet::Detector::Association.add_object_associations(ar_parent, association)
138
- Bullet::Detector::NPlusOneQuery.call_association(ar_parent, association)
139
- @bullet_eager_loadings[ar_parent.class] ||= {}
140
- @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new
141
- @bullet_eager_loadings[ar_parent.class][ar_parent] << association
142
- end
143
- end
144
- end
145
- end
146
-
147
- super
148
- end
149
-
150
- # call join associations
151
- def construct_model(record, node, row, model_cache, id, strict_loading_value)
152
- result = super
153
-
154
- if Bullet.start?
155
- associations = [node.reflection.name]
156
- if node.reflection.through_reflection?
157
- associations << node.reflection.through_reflection.name
158
- end
159
- associations.each do |association|
160
- Bullet::Detector::Association.add_object_associations(record, association)
161
- Bullet::Detector::NPlusOneQuery.call_association(record, association)
162
- @bullet_eager_loadings[record.class] ||= {}
163
- @bullet_eager_loadings[record.class][record] ||= Set.new
164
- @bullet_eager_loadings[record.class][record] << association
165
- end
166
- end
167
-
168
- result
169
- end
170
- end
171
- )
172
-
173
- ::ActiveRecord::Associations::Association.prepend(
174
- Module.new do
175
- def inversed_from(record)
176
- if Bullet.start?
177
- Bullet::Detector::NPlusOneQuery.add_inversed_object(owner, reflection.name)
178
- end
179
- super
180
- end
181
- end
182
- )
183
-
184
- ::ActiveRecord::Associations::CollectionAssociation.prepend(
185
- Module.new do
186
- def load_target
187
- records = super
188
-
189
- if Bullet.start?
190
- if is_a? ::ActiveRecord::Associations::ThroughAssociation
191
- association = owner.association(reflection.through_reflection.name)
192
- if association.loaded?
193
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.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 reflection.through_reflection != through_reflection
199
- Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
200
- end
201
- end
202
- end
203
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) unless @inversed
204
- if records.first.class.name !~ /^HABTM_/
205
- if records.size > 1
206
- Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
207
- Bullet::Detector::CounterCache.add_possible_objects(records)
208
- elsif records.size == 1
209
- Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
210
- Bullet::Detector::CounterCache.add_impossible_object(records.first)
211
- end
212
- end
213
- end
214
- records
215
- end
216
-
217
- def empty?
218
- if Bullet.start? && !reflection.has_cached_counter?
219
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, caller_locations)
220
- end
221
- super
222
- end
223
-
224
- def include?(object)
225
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, caller_locations) if Bullet.start?
226
- super
227
- end
228
- end
229
- )
230
-
231
- ::ActiveRecord::Associations::SingularAssociation.prepend(
232
- Module.new do
233
- # call has_one and belongs_to associations
234
- def target
235
- result = super()
236
-
237
- if Bullet.start?
238
- if owner.class.name !~ /^HABTM_/ && !@inversed
239
- if is_a? ::ActiveRecord::Associations::ThroughAssociation
240
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
241
- association = owner.association(reflection.through_reflection.name)
242
- Array.wrap(association.target).each do |through_record|
243
- Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
244
- end
245
-
246
- if reflection.through_reflection != through_reflection
247
- Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
248
- end
249
- end
250
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
251
-
252
- if Bullet::Detector::NPlusOneQuery.impossible?(owner)
253
- Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
254
- else
255
- Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
256
- end
257
- end
258
- end
259
- result
260
- end
261
- end
262
- )
263
-
264
- ::ActiveRecord::Associations::HasManyAssociation.prepend(
265
- Module.new do
266
- def empty?
267
- if Bullet.start? && !reflection.has_cached_counter?
268
- Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, caller_locations)
269
- end
270
- super
271
- end
272
-
273
- def count_records
274
- result = reflection.has_cached_counter?
275
- if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
276
- Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
277
- end
278
- super
279
- end
280
- end
281
- )
282
-
283
- ::ActiveRecord::Associations::CollectionProxy.prepend(
284
- Module.new do
285
- def count(column_name = nil)
286
- if Bullet.start? && !proxy_association.is_a?(::ActiveRecord::Associations::ThroughAssociation)
287
- Bullet::Detector::CounterCache.add_counter_cache(
288
- proxy_association.owner,
289
- proxy_association.reflection.name
290
- )
291
- Bullet::Detector::NPlusOneQuery.call_association(
292
- proxy_association.owner,
293
- proxy_association.reflection.name,
294
- caller_locations
295
- )
296
- end
297
- super(column_name)
298
- end
299
- end
300
- )
301
- end
302
- end
303
- end