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.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/bullet-8.1.3/CHANGELOG.md +374 -0
  3. data/bullet-8.1.3/MIT-LICENSE +20 -0
  4. data/bullet-8.1.3/README.md +525 -0
  5. data/bullet-8.1.3/lib/bullet/active_job.rb +13 -0
  6. data/bullet-8.1.3/lib/bullet/active_record4.rb +197 -0
  7. data/bullet-8.1.3/lib/bullet/active_record41.rb +191 -0
  8. data/bullet-8.1.3/lib/bullet/active_record42.rb +262 -0
  9. data/bullet-8.1.3/lib/bullet/active_record5.rb +294 -0
  10. data/bullet-8.1.3/lib/bullet/active_record52.rb +278 -0
  11. data/bullet-8.1.3/lib/bullet/active_record60.rb +310 -0
  12. data/bullet-8.1.3/lib/bullet/active_record61.rb +310 -0
  13. data/bullet-8.1.3/lib/bullet/active_record70.rb +319 -0
  14. data/bullet-8.1.3/lib/bullet/active_record71.rb +319 -0
  15. data/bullet-8.1.3/lib/bullet/active_record72.rb +319 -0
  16. data/bullet-8.1.3/lib/bullet/active_record80.rb +319 -0
  17. data/bullet-8.1.3/lib/bullet/active_record81.rb +321 -0
  18. data/bullet-8.1.3/lib/bullet/bullet_xhr.js +64 -0
  19. data/bullet-8.1.3/lib/bullet/dependency.rb +165 -0
  20. data/bullet-8.1.3/lib/bullet/detector/association.rb +92 -0
  21. data/bullet-8.1.3/lib/bullet/detector/base.rb +8 -0
  22. data/bullet-8.1.3/lib/bullet/detector/counter_cache.rb +69 -0
  23. data/bullet-8.1.3/lib/bullet/detector/n_plus_one_query.rb +148 -0
  24. data/bullet-8.1.3/lib/bullet/detector/unused_eager_loading.rb +100 -0
  25. data/bullet-8.1.3/lib/bullet/detector.rb +11 -0
  26. data/bullet-8.1.3/lib/bullet/ext/object.rb +37 -0
  27. data/bullet-8.1.3/lib/bullet/ext/string.rb +14 -0
  28. data/bullet-8.1.3/lib/bullet/mongoid4x.rb +59 -0
  29. data/bullet-8.1.3/lib/bullet/mongoid5x.rb +59 -0
  30. data/bullet-8.1.3/lib/bullet/mongoid6x.rb +59 -0
  31. data/bullet-8.1.3/lib/bullet/mongoid7x.rb +74 -0
  32. data/bullet-8.1.3/lib/bullet/mongoid8x.rb +61 -0
  33. data/bullet-8.1.3/lib/bullet/mongoid9x.rb +74 -0
  34. data/bullet-8.1.3/lib/bullet/notification/base.rb +92 -0
  35. data/bullet-8.1.3/lib/bullet/notification/counter_cache.rb +15 -0
  36. data/bullet-8.1.3/lib/bullet/notification/n_plus_one_query.rb +31 -0
  37. data/bullet-8.1.3/lib/bullet/notification/unused_eager_loading.rb +31 -0
  38. data/bullet-8.1.3/lib/bullet/notification.rb +13 -0
  39. data/bullet-8.1.3/lib/bullet/notification_collector.rb +25 -0
  40. data/bullet-8.1.3/lib/bullet/rack.rb +186 -0
  41. data/bullet-8.1.3/lib/bullet/registry/association.rb +16 -0
  42. data/bullet-8.1.3/lib/bullet/registry/base.rb +46 -0
  43. data/bullet-8.1.3/lib/bullet/registry/call_stack.rb +17 -0
  44. data/bullet-8.1.3/lib/bullet/registry/object.rb +18 -0
  45. data/bullet-8.1.3/lib/bullet/registry.rb +10 -0
  46. data/bullet-8.1.3/lib/bullet/stack_trace_filter.rb +67 -0
  47. data/bullet-8.1.3/lib/bullet/version.rb +5 -0
  48. data/bullet-8.1.3/lib/bullet.rb +289 -0
  49. data/bullet-8.1.3/lib/generators/bullet/install_generator.rb +47 -0
  50. data/bullet-8.1.3/tasks/bullet_tasks.rake +11 -0
  51. data/hyper-pure-sys.gemspec +12 -0
  52. metadata +91 -0
@@ -0,0 +1,321 @@
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(*args, **kwargs, &block)
20
+ result = super(*args, **kwargs, &block)
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::Batch.prepend(
65
+ Module.new do
66
+ def call
67
+ if Bullet.start?
68
+ @preloaders.each do |preloader|
69
+ preloader.records.each { |record|
70
+ Bullet::Detector::Association.add_object_associations(record, preloader.associations)
71
+ }
72
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(preloader.records, preloader.associations)
73
+ end
74
+ end
75
+ super
76
+ end
77
+ end
78
+ )
79
+
80
+ ::ActiveRecord::Associations::Preloader::Branch.prepend(
81
+ Module.new do
82
+ def preloaders_for_reflection(reflection, reflection_records)
83
+ if Bullet.start?
84
+ reflection_records.compact!
85
+ if reflection_records.first.class.name !~ /^HABTM_/
86
+ reflection_records.each { |record|
87
+ Bullet::Detector::Association.add_object_associations(record, reflection.name)
88
+ }
89
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(reflection_records, reflection.name)
90
+ end
91
+ end
92
+ super
93
+ end
94
+ end
95
+ )
96
+
97
+ ::ActiveRecord::Associations::Preloader::ThroughAssociation.prepend(
98
+ Module.new do
99
+ def source_preloaders
100
+ if Bullet.start? && !defined?(@source_preloaders)
101
+ preloaders = super
102
+ preloaders.each do |preloader|
103
+ reflection_name = preloader.send(:reflection).name
104
+ preloader.send(:owners).each do |owner|
105
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection_name)
106
+ end
107
+ end
108
+ else
109
+ super
110
+ end
111
+ end
112
+ end
113
+ )
114
+
115
+ ::ActiveRecord::Associations::JoinDependency.prepend(
116
+ Module.new do
117
+ def instantiate(*args, **kwargs, &block)
118
+ @bullet_eager_loadings = {}
119
+ records = super(*args, **kwargs, &block)
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
+
133
+ def construct(*args, **kwargs, &block)
134
+ ar_parent, parent, row = args
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(*args, **kwargs, &block)
158
+ end
159
+
160
+ # call join associations
161
+ def construct_model(*args, **kwargs, &block)
162
+ record, node = args
163
+ result = super(*args, **kwargs, &block)
164
+
165
+ if Bullet.start?
166
+ associations = [node.reflection.name]
167
+ if node.reflection.through_reflection?
168
+ associations << node.reflection.through_reflection.name
169
+ end
170
+ associations.each do |association|
171
+ Bullet::Detector::Association.add_object_associations(record, association)
172
+ Bullet::Detector::NPlusOneQuery.call_association(record, association)
173
+ @bullet_eager_loadings[record.class] ||= {}
174
+ @bullet_eager_loadings[record.class][record] ||= Set.new
175
+ @bullet_eager_loadings[record.class][record] << association
176
+ end
177
+ end
178
+
179
+ result
180
+ end
181
+ end
182
+ )
183
+
184
+ ::ActiveRecord::Associations::Association.prepend(
185
+ Module.new do
186
+ def inversed_from(record)
187
+ if Bullet.start?
188
+ Bullet::Detector::NPlusOneQuery.add_inversed_object(owner, reflection.name)
189
+ end
190
+ super
191
+ end
192
+
193
+ def inversed_from_queries(record)
194
+ if Bullet.start? && inversable?(record)
195
+ Bullet::Detector::NPlusOneQuery.add_inversed_object(owner, reflection.name)
196
+ end
197
+ super
198
+ end
199
+ end
200
+ )
201
+
202
+ ::ActiveRecord::Associations::CollectionAssociation.prepend(
203
+ Module.new do
204
+ def load_target
205
+ records = super
206
+
207
+ if Bullet.start?
208
+ if is_a? ::ActiveRecord::Associations::ThroughAssociation
209
+ association = owner.association(reflection.through_reflection.name)
210
+ if association.loaded?
211
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
212
+ Array.wrap(association.target).each do |through_record|
213
+ Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
214
+ end
215
+
216
+ if reflection.through_reflection != through_reflection
217
+ Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
218
+ end
219
+ end
220
+ end
221
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
222
+ if records.first.class.name !~ /^HABTM_/
223
+ if records.size > 1
224
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
225
+ Bullet::Detector::CounterCache.add_possible_objects(records)
226
+ elsif records.size == 1
227
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
228
+ Bullet::Detector::CounterCache.add_impossible_object(records.first)
229
+ end
230
+ end
231
+ end
232
+ records
233
+ end
234
+
235
+ def empty?
236
+ if Bullet.start? && !reflection.has_cached_counter?
237
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, caller_locations)
238
+ end
239
+ super
240
+ end
241
+
242
+ def include?(object)
243
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, caller_locations) if Bullet.start?
244
+ super
245
+ end
246
+ end
247
+ )
248
+
249
+ ::ActiveRecord::Associations::SingularAssociation.prepend(
250
+ Module.new do
251
+ # call has_one and belongs_to associations
252
+ def reader
253
+ result = super
254
+
255
+ if Bullet.start?
256
+ if owner.class.name !~ /^HABTM_/
257
+ if is_a? ::ActiveRecord::Associations::ThroughAssociation
258
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
259
+ association = owner.association(reflection.through_reflection.name)
260
+ Array.wrap(association.target).each do |through_record|
261
+ Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
262
+ end
263
+
264
+ if reflection.through_reflection != through_reflection
265
+ Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
266
+ end
267
+ end
268
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
269
+
270
+ if Bullet::Detector::NPlusOneQuery.impossible?(owner)
271
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
272
+ else
273
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
274
+ end
275
+ end
276
+ end
277
+ result
278
+ end
279
+ end
280
+ )
281
+
282
+ ::ActiveRecord::Associations::HasManyAssociation.prepend(
283
+ Module.new do
284
+ def empty?
285
+ if Bullet.start? && !reflection.has_cached_counter?
286
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, caller_locations)
287
+ end
288
+ super
289
+ end
290
+
291
+ def count_records
292
+ result = reflection.has_cached_counter?
293
+ if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
294
+ Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
295
+ end
296
+ super
297
+ end
298
+ end
299
+ )
300
+
301
+ ::ActiveRecord::Associations::CollectionProxy.prepend(
302
+ Module.new do
303
+ def count(column_name = nil)
304
+ if Bullet.start? && !proxy_association.is_a?(::ActiveRecord::Associations::ThroughAssociation)
305
+ Bullet::Detector::CounterCache.add_counter_cache(
306
+ proxy_association.owner,
307
+ proxy_association.reflection.name
308
+ )
309
+ Bullet::Detector::NPlusOneQuery.call_association(
310
+ proxy_association.owner,
311
+ proxy_association.reflection.name,
312
+ caller_locations
313
+ )
314
+ end
315
+ super(column_name)
316
+ end
317
+ end
318
+ )
319
+ end
320
+ end
321
+ end
@@ -0,0 +1,64 @@
1
+ (function () {
2
+ var oldOpen = window.XMLHttpRequest.prototype.open;
3
+ var oldSend = window.XMLHttpRequest.prototype.send;
4
+
5
+ /**
6
+ * Return early if we've already extended prototype. This prevents
7
+ * "maximum call stack exceeded" errors when used with Turbolinks.
8
+ * See https://github.com/flyerhzm/bullet/issues/454
9
+ */
10
+ if (isBulletInitiated()) return;
11
+
12
+ function isBulletInitiated() {
13
+ return oldOpen.name == "bulletXHROpen" && oldSend.name == "bulletXHRSend";
14
+ }
15
+ function bulletXHROpen(_, url) {
16
+ this._storedUrl = url;
17
+ return Reflect.apply(oldOpen, this, arguments);
18
+ }
19
+ function bulletXHRSend() {
20
+ if (this.onload) {
21
+ this._storedOnload = this.onload;
22
+ }
23
+ this.onload = null;
24
+ this.addEventListener("load", bulletXHROnload);
25
+ return Reflect.apply(oldSend, this, arguments);
26
+ }
27
+ function bulletXHROnload() {
28
+ if (
29
+ this._storedUrl.startsWith(window.location.protocol + "//" + window.location.host) ||
30
+ !this._storedUrl.startsWith("http") // For relative paths
31
+ ) {
32
+ var bulletFooterText = this.getResponseHeader("X-bullet-footer-text");
33
+ if (bulletFooterText) {
34
+ setTimeout(function () {
35
+ var oldHtml = document.querySelector("#bullet-footer").innerHTML.split("<br>");
36
+ var header = oldHtml[0];
37
+ oldHtml = oldHtml.slice(1, oldHtml.length);
38
+ var newHtml = oldHtml.concat(JSON.parse(bulletFooterText));
39
+ newHtml = newHtml.slice(newHtml.length - 10, newHtml.length); // rotate through 10 most recent
40
+ document.querySelector("#bullet-footer").innerHTML = `${header}<br>${newHtml.join("<br>")}`;
41
+ }, 0);
42
+ }
43
+ var bulletConsoleText = this.getResponseHeader("X-bullet-console-text");
44
+ if (bulletConsoleText && typeof console !== "undefined" && console.log) {
45
+ setTimeout(function () {
46
+ JSON.parse(bulletConsoleText).forEach((message) => {
47
+ if (console.groupCollapsed && console.groupEnd) {
48
+ console.groupCollapsed("Uniform Notifier");
49
+ console.log(message);
50
+ console.groupEnd();
51
+ } else {
52
+ console.log(message);
53
+ }
54
+ });
55
+ }, 0);
56
+ }
57
+ }
58
+ if (this._storedOnload) {
59
+ return Reflect.apply(this._storedOnload, this, arguments);
60
+ }
61
+ }
62
+ window.XMLHttpRequest.prototype.open = bulletXHROpen;
63
+ window.XMLHttpRequest.prototype.send = bulletXHRSend;
64
+ })();
@@ -0,0 +1,165 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bullet
4
+ module Dependency
5
+ def mongoid?
6
+ @mongoid ||= defined?(::Mongoid)
7
+ end
8
+
9
+ def active_record?
10
+ @active_record ||= defined?(::ActiveRecord)
11
+ end
12
+
13
+ def active_record_version
14
+ @active_record_version ||=
15
+ begin
16
+ if active_record40?
17
+ 'active_record4'
18
+ elsif active_record41?
19
+ 'active_record41'
20
+ elsif active_record42?
21
+ 'active_record42'
22
+ elsif active_record50?
23
+ 'active_record5'
24
+ elsif active_record51?
25
+ 'active_record5'
26
+ elsif active_record52?
27
+ 'active_record52'
28
+ elsif active_record60?
29
+ 'active_record60'
30
+ elsif active_record61?
31
+ 'active_record61'
32
+ elsif active_record70?
33
+ 'active_record70'
34
+ elsif active_record71?
35
+ 'active_record71'
36
+ elsif active_record72?
37
+ 'active_record72'
38
+ elsif active_record80?
39
+ 'active_record80'
40
+ elsif active_record81?
41
+ 'active_record81'
42
+ else
43
+ raise "Bullet does not support active_record #{::ActiveRecord::VERSION::STRING} yet"
44
+ end
45
+ end
46
+ end
47
+
48
+ def mongoid_version
49
+ @mongoid_version ||=
50
+ begin
51
+ if mongoid4x?
52
+ 'mongoid4x'
53
+ elsif mongoid5x?
54
+ 'mongoid5x'
55
+ elsif mongoid6x?
56
+ 'mongoid6x'
57
+ elsif mongoid7x?
58
+ 'mongoid7x'
59
+ elsif mongoid8x?
60
+ 'mongoid8x'
61
+ elsif mongoid9x?
62
+ 'mongoid9x'
63
+ else
64
+ raise "Bullet does not support mongoid #{::Mongoid::VERSION} yet"
65
+ end
66
+ end
67
+ end
68
+
69
+ def active_record4?
70
+ active_record? && ::ActiveRecord::VERSION::MAJOR == 4
71
+ end
72
+
73
+ def active_record5?
74
+ active_record? && ::ActiveRecord::VERSION::MAJOR == 5
75
+ end
76
+
77
+ def active_record6?
78
+ active_record? && ::ActiveRecord::VERSION::MAJOR == 6
79
+ end
80
+
81
+ def active_record7?
82
+ active_record? && ::ActiveRecord::VERSION::MAJOR == 7
83
+ end
84
+
85
+ def active_record8?
86
+ active_record? && ::ActiveRecord::VERSION::MAJOR == 8
87
+ end
88
+
89
+ def active_record40?
90
+ active_record4? && ::ActiveRecord::VERSION::MINOR == 0
91
+ end
92
+
93
+ def active_record41?
94
+ active_record4? && ::ActiveRecord::VERSION::MINOR == 1
95
+ end
96
+
97
+ def active_record42?
98
+ active_record4? && ::ActiveRecord::VERSION::MINOR == 2
99
+ end
100
+
101
+ def active_record50?
102
+ active_record5? && ::ActiveRecord::VERSION::MINOR == 0
103
+ end
104
+
105
+ def active_record51?
106
+ active_record5? && ::ActiveRecord::VERSION::MINOR == 1
107
+ end
108
+
109
+ def active_record52?
110
+ active_record5? && ::ActiveRecord::VERSION::MINOR == 2
111
+ end
112
+
113
+ def active_record60?
114
+ active_record6? && ::ActiveRecord::VERSION::MINOR == 0
115
+ end
116
+
117
+ def active_record61?
118
+ active_record6? && ::ActiveRecord::VERSION::MINOR == 1
119
+ end
120
+
121
+ def active_record70?
122
+ active_record7? && ::ActiveRecord::VERSION::MINOR == 0
123
+ end
124
+
125
+ def active_record71?
126
+ active_record7? && ::ActiveRecord::VERSION::MINOR == 1
127
+ end
128
+
129
+ def active_record72?
130
+ active_record7? && ::ActiveRecord::VERSION::MINOR == 2
131
+ end
132
+
133
+ def active_record80?
134
+ active_record8? && ::ActiveRecord::VERSION::MINOR == 0
135
+ end
136
+
137
+ def active_record81?
138
+ active_record8? && ::ActiveRecord::VERSION::MINOR == 1
139
+ end
140
+
141
+ def mongoid4x?
142
+ mongoid? && ::Mongoid::VERSION =~ /\A4/
143
+ end
144
+
145
+ def mongoid5x?
146
+ mongoid? && ::Mongoid::VERSION =~ /\A5/
147
+ end
148
+
149
+ def mongoid6x?
150
+ mongoid? && ::Mongoid::VERSION =~ /\A6/
151
+ end
152
+
153
+ def mongoid7x?
154
+ mongoid? && ::Mongoid::VERSION =~ /\A7/
155
+ end
156
+
157
+ def mongoid8x?
158
+ mongoid? && ::Mongoid::VERSION =~ /\A8/
159
+ end
160
+
161
+ def mongoid9x?
162
+ mongoid? && ::Mongoid::VERSION =~ /\A9/
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ using Bullet::Ext::Object
4
+
5
+ module Bullet
6
+ module Detector
7
+ class Association < Base
8
+ class << self
9
+ def add_object_associations(object, associations)
10
+ return unless Bullet.start?
11
+ return if !Bullet.n_plus_one_query_enable? && !Bullet.unused_eager_loading_enable?
12
+ return unless object.bullet_primary_key_value
13
+
14
+ Bullet.debug(
15
+ 'Detector::Association#add_object_associations',
16
+ "object: #{object.bullet_key}, associations: #{associations}"
17
+ )
18
+ call_stacks.add(object.bullet_key)
19
+ object_associations.add(object.bullet_key, associations)
20
+ end
21
+
22
+ def add_call_object_associations(object, associations)
23
+ return unless Bullet.start?
24
+ return if !Bullet.n_plus_one_query_enable? && !Bullet.unused_eager_loading_enable?
25
+ return unless object.bullet_primary_key_value
26
+
27
+ Bullet.debug(
28
+ 'Detector::Association#add_call_object_associations',
29
+ "object: #{object.bullet_key}, associations: #{associations}"
30
+ )
31
+ call_stacks.add(object.bullet_key)
32
+ call_object_associations.add(object.bullet_key, associations)
33
+ end
34
+
35
+ # possible_objects keep the class to object relationships
36
+ # that the objects may cause N+1 query.
37
+ # e.g. { Post => ["Post:1", "Post:2"] }
38
+ def possible_objects
39
+ Thread.current.thread_variable_get(:bullet_possible_objects)
40
+ end
41
+
42
+ # impossible_objects keep the class to objects relationships
43
+ # that the objects may not cause N+1 query.
44
+ # e.g. { Post => ["Post:1", "Post:2"] }
45
+ # if find collection returns only one object, then the object is impossible object,
46
+ # impossible_objects are used to avoid treating 1+1 query to N+1 query.
47
+ def impossible_objects
48
+ Thread.current.thread_variable_get(:bullet_impossible_objects)
49
+ end
50
+
51
+ private
52
+
53
+ # object_associations keep the object relationships
54
+ # that the object has many associations.
55
+ # e.g. { "Post:1" => [:comments] }
56
+ # the object_associations keep all associations that may be or may no be
57
+ # unpreload associations or unused preload associations.
58
+ def object_associations
59
+ Thread.current.thread_variable_get(:bullet_object_associations)
60
+ end
61
+
62
+ # call_object_associations keep the object relationships
63
+ # that object.associations is called.
64
+ # e.g. { "Post:1" => [:comments] }
65
+ # they are used to detect unused preload associations.
66
+ def call_object_associations
67
+ Thread.current.thread_variable_get(:bullet_call_object_associations)
68
+ end
69
+
70
+ # inversed_objects keeps object relationships
71
+ # that association is inversed.
72
+ # e.g. { "Comment:1" => ["post"] }
73
+ def inversed_objects
74
+ Thread.current.thread_variable_get(:bullet_inversed_objects)
75
+ end
76
+
77
+ # eager_loadings keep the object relationships
78
+ # that the associations are preloaded by find :include.
79
+ # e.g. { ["Post:1", "Post:2"] => [:comments, :user] }
80
+ def eager_loadings
81
+ Thread.current.thread_variable_get(:bullet_eager_loadings)
82
+ end
83
+
84
+ # call_stacks keeps stacktraces where querie-objects were called from.
85
+ # e.g. { 'Object:111' => [SomeProject/app/controllers/...] }
86
+ def call_stacks
87
+ Thread.current.thread_variable_get(:bullet_call_stacks)
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bullet
4
+ module Detector
5
+ class Base
6
+ end
7
+ end
8
+ end