bullet 8.1.3 → 8.3.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 +4 -4
- data/CHANGELOG.md +20 -1
- data/README.md +19 -6
- data/lib/bullet/active_record70.rb +14 -0
- data/lib/bullet/active_record71.rb +14 -0
- data/lib/bullet/active_record72.rb +14 -0
- data/lib/bullet/active_record80.rb +14 -0
- data/lib/bullet/active_record81.rb +14 -0
- data/lib/bullet/{active_record60.rb → active_record82.rb} +74 -49
- data/lib/bullet/dependency.rb +10 -58
- data/lib/bullet/detector/association.rb +9 -9
- data/lib/bullet/detector/counter_cache.rb +2 -2
- data/lib/bullet/detector/n_plus_one_query.rb +1 -1
- data/lib/bullet/rack.rb +41 -3
- data/lib/bullet/version.rb +1 -1
- data/lib/bullet.rb +68 -28
- metadata +31 -9
- data/lib/bullet/active_record4.rb +0 -197
- data/lib/bullet/active_record41.rb +0 -191
- data/lib/bullet/active_record42.rb +0 -262
- data/lib/bullet/active_record5.rb +0 -294
- data/lib/bullet/active_record52.rb +0 -278
- data/lib/bullet/active_record61.rb +0 -310
data/lib/bullet/rack.rb
CHANGED
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
require 'rack/request'
|
|
4
4
|
require 'json'
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
if RUBY_VERSION.to_i >= 4
|
|
7
|
+
require 'cgi/escape'
|
|
8
|
+
else
|
|
9
|
+
require 'cgi'
|
|
10
|
+
end
|
|
6
11
|
|
|
7
12
|
module Bullet
|
|
8
13
|
class Rack
|
|
@@ -76,8 +81,9 @@ module Bullet
|
|
|
76
81
|
|
|
77
82
|
# Make footer styles work with ContentSecurityPolicy style-src as self
|
|
78
83
|
def footer_style(nonce = nil)
|
|
84
|
+
position_css = footer_position_css
|
|
79
85
|
css = <<~CSS
|
|
80
|
-
details#bullet-footer {cursor: pointer; position: fixed;
|
|
86
|
+
details#bullet-footer {cursor: pointer; position: fixed; #{position_css}; z-index: 9999; background: #fdf2f2; color: #9b1c1c; font-size: 12px; #{footer_border_radius}; border: 1px solid #9b1c1c;}
|
|
81
87
|
details#bullet-footer summary {font-weight: 600; padding: 2px 8px;}
|
|
82
88
|
details#bullet-footer div {padding: 8px; border-top: 1px solid #9b1c1c;}
|
|
83
89
|
CSS
|
|
@@ -88,6 +94,38 @@ module Bullet
|
|
|
88
94
|
end
|
|
89
95
|
end
|
|
90
96
|
|
|
97
|
+
private
|
|
98
|
+
|
|
99
|
+
def footer_position_css
|
|
100
|
+
case Bullet.footer_position
|
|
101
|
+
when 'bottom_left'
|
|
102
|
+
'left: 0px; bottom: 0px'
|
|
103
|
+
when 'bottom_right'
|
|
104
|
+
'right: 0px; bottom: 0px'
|
|
105
|
+
when 'top_left'
|
|
106
|
+
'left: 0px; top: 0px'
|
|
107
|
+
when 'top_right'
|
|
108
|
+
'right: 0px; top: 0px'
|
|
109
|
+
else
|
|
110
|
+
'left: 0px; bottom: 0px'
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def footer_border_radius
|
|
115
|
+
case Bullet.footer_position
|
|
116
|
+
when 'bottom_left'
|
|
117
|
+
'border-radius: 0px 8px 0px 0px'
|
|
118
|
+
when 'bottom_right'
|
|
119
|
+
'border-radius: 8px 0px 0px 0px'
|
|
120
|
+
when 'top_left'
|
|
121
|
+
'border-radius: 0px 0px 8px 0px'
|
|
122
|
+
when 'top_right'
|
|
123
|
+
'border-radius: 0px 0px 0px 8px'
|
|
124
|
+
else
|
|
125
|
+
'border-radius: 0px 8px 0px 0px'
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
91
129
|
def set_header(headers, header_name, header_array)
|
|
92
130
|
# Many proxy applications such as Nginx and AWS ELB limit
|
|
93
131
|
# the size a header to 8KB, so truncate the list of reports to
|
|
@@ -123,7 +161,7 @@ module Bullet
|
|
|
123
161
|
end
|
|
124
162
|
|
|
125
163
|
def html_request?(headers, response)
|
|
126
|
-
headers['Content-Type']&.include?('text/html')
|
|
164
|
+
headers['Content-Type']&.include?('text/html') || false
|
|
127
165
|
end
|
|
128
166
|
|
|
129
167
|
def response_body(response)
|
data/lib/bullet/version.rb
CHANGED
data/lib/bullet.rb
CHANGED
|
@@ -39,7 +39,8 @@ module Bullet
|
|
|
39
39
|
:counter_cache_enable,
|
|
40
40
|
:stacktrace_includes,
|
|
41
41
|
:stacktrace_excludes,
|
|
42
|
-
:skip_html_injection
|
|
42
|
+
:skip_html_injection,
|
|
43
|
+
:footer_position
|
|
43
44
|
attr_reader :safelist
|
|
44
45
|
attr_accessor :add_footer,
|
|
45
46
|
:orm_patches_applied,
|
|
@@ -148,48 +149,83 @@ module Bullet
|
|
|
148
149
|
puts "[Bullet][#{title}] #{message}" if ENV['BULLET_DEBUG'] == 'true'
|
|
149
150
|
end
|
|
150
151
|
|
|
152
|
+
# Fiber storage reaches child fibers; start_request gives each request its own, and the owner check keeps it off new threads.
|
|
153
|
+
def state
|
|
154
|
+
unless Fiber.respond_to?(:[])
|
|
155
|
+
return Thread.current.thread_variable_get(:bullet_state) || Thread.current.thread_variable_set(:bullet_state, {})
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
current = Fiber[:bullet_state]
|
|
159
|
+
return current if current && current[:owner].equal?(Thread.current)
|
|
160
|
+
|
|
161
|
+
Fiber[:bullet_state] = { owner: Thread.current }
|
|
162
|
+
end
|
|
163
|
+
|
|
151
164
|
def start_request
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
+
if Fiber.respond_to?(:[])
|
|
166
|
+
Fiber[:bullet_state] = state.slice(:owner)
|
|
167
|
+
end
|
|
168
|
+
Bullet.state[:bullet_start] = true
|
|
169
|
+
Bullet.state[:bullet_notification_collector] = Bullet::NotificationCollector.new
|
|
170
|
+
|
|
171
|
+
Bullet.state[:bullet_object_associations] = Bullet::Registry::Base.new
|
|
172
|
+
Bullet.state[:bullet_call_object_associations] = Bullet::Registry::Base.new
|
|
173
|
+
Bullet.state[:bullet_possible_objects] = Bullet::Registry::Object.new
|
|
174
|
+
Bullet.state[:bullet_impossible_objects] = Bullet::Registry::Object.new
|
|
175
|
+
Bullet.state[:bullet_inversed_objects] = Bullet::Registry::Base.new
|
|
176
|
+
Bullet.state[:bullet_eager_loadings] = Bullet::Registry::Association.new
|
|
177
|
+
Bullet.state[:bullet_call_stacks] = Bullet::Registry::CallStack.new
|
|
178
|
+
|
|
179
|
+
unless Bullet.state[:bullet_counter_possible_objects]
|
|
180
|
+
Bullet.state[:bullet_counter_possible_objects] = Bullet::Registry::Object.new
|
|
165
181
|
end
|
|
166
182
|
|
|
167
|
-
unless
|
|
168
|
-
|
|
183
|
+
unless Bullet.state[:bullet_counter_impossible_objects]
|
|
184
|
+
Bullet.state[:bullet_counter_impossible_objects] = Bullet::Registry::Object.new
|
|
169
185
|
end
|
|
170
186
|
end
|
|
171
187
|
|
|
172
188
|
def end_request
|
|
173
|
-
|
|
174
|
-
|
|
189
|
+
Bullet.state[:bullet_start] = nil
|
|
190
|
+
Bullet.state[:bullet_notification_collector] = nil
|
|
175
191
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
192
|
+
Bullet.state[:bullet_object_associations] = nil
|
|
193
|
+
Bullet.state[:bullet_call_object_associations] = nil
|
|
194
|
+
Bullet.state[:bullet_possible_objects] = nil
|
|
195
|
+
Bullet.state[:bullet_impossible_objects] = nil
|
|
196
|
+
Bullet.state[:bullet_inversed_objects] = nil
|
|
197
|
+
Bullet.state[:bullet_eager_loadings] = nil
|
|
182
198
|
|
|
183
|
-
|
|
184
|
-
|
|
199
|
+
Bullet.state[:bullet_counter_possible_objects] = nil
|
|
200
|
+
Bullet.state[:bullet_counter_impossible_objects] = nil
|
|
185
201
|
end
|
|
186
202
|
|
|
187
203
|
def start?
|
|
188
|
-
enable? &&
|
|
204
|
+
enable? && Bullet.state[:bullet_start]
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def pause
|
|
208
|
+
Bullet.state[:bullet_start] = false
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def resume
|
|
212
|
+
Bullet.state[:bullet_start] = true
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def paused?
|
|
216
|
+
enable? && !Bullet.state[:bullet_start]
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def skip
|
|
220
|
+
bullet_was_started = start?
|
|
221
|
+
pause if bullet_was_started
|
|
222
|
+
yield
|
|
223
|
+
ensure
|
|
224
|
+
resume if bullet_was_started
|
|
189
225
|
end
|
|
190
226
|
|
|
191
227
|
def notification_collector
|
|
192
|
-
|
|
228
|
+
Bullet.state[:bullet_notification_collector]
|
|
193
229
|
end
|
|
194
230
|
|
|
195
231
|
def notification?
|
|
@@ -259,6 +295,10 @@ module Bullet
|
|
|
259
295
|
UniformNotifier.active_notifiers.include?(UniformNotifier::JavascriptConsole)
|
|
260
296
|
end
|
|
261
297
|
|
|
298
|
+
def footer_position
|
|
299
|
+
@footer_position || 'bottom_left'
|
|
300
|
+
end
|
|
301
|
+
|
|
262
302
|
def inject_into_page?
|
|
263
303
|
return false if defined?(@skip_html_injection) && @skip_html_injection
|
|
264
304
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: bullet
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 8.1
|
|
4
|
+
version: 8.3.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Richard Huang
|
|
@@ -37,6 +37,34 @@ dependencies:
|
|
|
37
37
|
- - "~>"
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
39
|
version: '1.11'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: ostruct
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: logger
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
40
68
|
description: help to kill N+1 queries and unused eager loading.
|
|
41
69
|
email:
|
|
42
70
|
- flyerhzm@gmail.com
|
|
@@ -49,18 +77,12 @@ files:
|
|
|
49
77
|
- README.md
|
|
50
78
|
- lib/bullet.rb
|
|
51
79
|
- lib/bullet/active_job.rb
|
|
52
|
-
- lib/bullet/active_record4.rb
|
|
53
|
-
- lib/bullet/active_record41.rb
|
|
54
|
-
- lib/bullet/active_record42.rb
|
|
55
|
-
- lib/bullet/active_record5.rb
|
|
56
|
-
- lib/bullet/active_record52.rb
|
|
57
|
-
- lib/bullet/active_record60.rb
|
|
58
|
-
- lib/bullet/active_record61.rb
|
|
59
80
|
- lib/bullet/active_record70.rb
|
|
60
81
|
- lib/bullet/active_record71.rb
|
|
61
82
|
- lib/bullet/active_record72.rb
|
|
62
83
|
- lib/bullet/active_record80.rb
|
|
63
84
|
- lib/bullet/active_record81.rb
|
|
85
|
+
- lib/bullet/active_record82.rb
|
|
64
86
|
- lib/bullet/bullet_xhr.js
|
|
65
87
|
- lib/bullet/dependency.rb
|
|
66
88
|
- lib/bullet/detector.rb
|
|
@@ -113,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
113
135
|
- !ruby/object:Gem::Version
|
|
114
136
|
version: 1.3.6
|
|
115
137
|
requirements: []
|
|
116
|
-
rubygems_version: 4.0.
|
|
138
|
+
rubygems_version: 4.0.20
|
|
117
139
|
specification_version: 4
|
|
118
140
|
summary: help to kill N+1 queries and unused eager loading.
|
|
119
141
|
test_files: []
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Bullet
|
|
4
|
-
module ActiveRecord
|
|
5
|
-
def self.enable
|
|
6
|
-
require 'active_record'
|
|
7
|
-
::ActiveRecord::Base.class_eval do
|
|
8
|
-
class << self
|
|
9
|
-
alias_method :origin_find_by_sql, :find_by_sql
|
|
10
|
-
def find_by_sql(sql, binds = [])
|
|
11
|
-
result = origin_find_by_sql(sql, binds)
|
|
12
|
-
if Bullet.start?
|
|
13
|
-
if result.is_a? Array
|
|
14
|
-
if result.size > 1
|
|
15
|
-
Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
|
|
16
|
-
Bullet::Detector::CounterCache.add_possible_objects(result)
|
|
17
|
-
elsif result.size == 1
|
|
18
|
-
Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)
|
|
19
|
-
Bullet::Detector::CounterCache.add_impossible_object(result.first)
|
|
20
|
-
end
|
|
21
|
-
elsif result.is_a? ::ActiveRecord::Base
|
|
22
|
-
Bullet::Detector::NPlusOneQuery.add_impossible_object(result)
|
|
23
|
-
Bullet::Detector::CounterCache.add_impossible_object(result)
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
result
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
::ActiveRecord::Relation.class_eval do
|
|
32
|
-
alias_method :origin_to_a, :to_a
|
|
33
|
-
# if select a collection of objects, then these objects have possible to cause N+1 query.
|
|
34
|
-
# if select only one object, then the only one object has impossible to cause N+1 query.
|
|
35
|
-
def to_a
|
|
36
|
-
records = origin_to_a
|
|
37
|
-
if Bullet.start?
|
|
38
|
-
if records.size > 1
|
|
39
|
-
Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
|
|
40
|
-
Bullet::Detector::CounterCache.add_possible_objects(records)
|
|
41
|
-
elsif records.size == 1
|
|
42
|
-
Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
|
|
43
|
-
Bullet::Detector::CounterCache.add_impossible_object(records.first)
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
records
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
::ActiveRecord::Persistence.class_eval do
|
|
51
|
-
def _create_record_with_bullet(*args)
|
|
52
|
-
_create_record_without_bullet(*args).tap do
|
|
53
|
-
Bullet::Detector::NPlusOneQuery.update_inversed_object(self)
|
|
54
|
-
Bullet::Detector::NPlusOneQuery.add_impossible_object(self)
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
alias_method_chain :_create_record, :bullet
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
::ActiveRecord::Associations::Preloader.class_eval do
|
|
61
|
-
# include query for one to many associations.
|
|
62
|
-
# keep this eager loadings.
|
|
63
|
-
alias_method :origin_initialize, :initialize
|
|
64
|
-
def initialize(records, associations, preload_scope = nil)
|
|
65
|
-
origin_initialize(records, associations, preload_scope)
|
|
66
|
-
|
|
67
|
-
if Bullet.start?
|
|
68
|
-
records = [records].flatten.compact.uniq
|
|
69
|
-
return if records.empty?
|
|
70
|
-
|
|
71
|
-
records.each { |record| Bullet::Detector::Association.add_object_associations(record, associations) }
|
|
72
|
-
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
::ActiveRecord::FinderMethods.class_eval do
|
|
78
|
-
# add includes in scope
|
|
79
|
-
alias_method :origin_find_with_associations, :find_with_associations
|
|
80
|
-
def find_with_associations
|
|
81
|
-
records = origin_find_with_associations
|
|
82
|
-
if Bullet.start?
|
|
83
|
-
associations = (eager_load_values + includes_values).uniq
|
|
84
|
-
records.each { |record| Bullet::Detector::Association.add_object_associations(record, associations) }
|
|
85
|
-
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
|
|
86
|
-
end
|
|
87
|
-
records
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
::ActiveRecord::Associations::JoinDependency.class_eval do
|
|
92
|
-
alias_method :origin_instantiate, :instantiate
|
|
93
|
-
alias_method :origin_construct_association, :construct_association
|
|
94
|
-
|
|
95
|
-
def instantiate(rows)
|
|
96
|
-
@bullet_eager_loadings = {}
|
|
97
|
-
records = origin_instantiate(rows)
|
|
98
|
-
|
|
99
|
-
if Bullet.start?
|
|
100
|
-
@bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
|
|
101
|
-
objects = eager_loadings_hash.keys
|
|
102
|
-
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
|
|
103
|
-
end
|
|
104
|
-
end
|
|
105
|
-
records
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
# call join associations
|
|
109
|
-
def construct_association(record, join, row)
|
|
110
|
-
result = origin_construct_association(record, join, row)
|
|
111
|
-
|
|
112
|
-
if Bullet.start?
|
|
113
|
-
associations = [join.reflection.name]
|
|
114
|
-
if join.reflection.nested?
|
|
115
|
-
associations << join.reflection.through_reflection.name
|
|
116
|
-
end
|
|
117
|
-
associations.each do |association|
|
|
118
|
-
Bullet::Detector::Association.add_object_associations(record, association)
|
|
119
|
-
Bullet::Detector::NPlusOneQuery.call_association(record, association)
|
|
120
|
-
@bullet_eager_loadings[record.class] ||= {}
|
|
121
|
-
@bullet_eager_loadings[record.class][record] ||= Set.new
|
|
122
|
-
@bullet_eager_loadings[record.class][record] << association
|
|
123
|
-
end
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
result
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
::ActiveRecord::Associations::CollectionAssociation.class_eval do
|
|
131
|
-
# call one to many associations
|
|
132
|
-
alias_method :origin_load_target, :load_target
|
|
133
|
-
def load_target
|
|
134
|
-
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) if Bullet.start?
|
|
135
|
-
origin_load_target
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
alias_method :origin_include?, :include?
|
|
139
|
-
def include?(object)
|
|
140
|
-
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) if Bullet.start?
|
|
141
|
-
origin_include?(object)
|
|
142
|
-
end
|
|
143
|
-
end
|
|
144
|
-
|
|
145
|
-
::ActiveRecord::Associations::HasManyAssociation.class_eval do
|
|
146
|
-
alias_method :origin_empty?, :empty?
|
|
147
|
-
def empty?
|
|
148
|
-
if Bullet.start? && !loaded? && !has_cached_counter?(@reflection)
|
|
149
|
-
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
|
|
150
|
-
end
|
|
151
|
-
origin_empty?
|
|
152
|
-
end
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
::ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
|
|
156
|
-
alias_method :origin_empty?, :empty?
|
|
157
|
-
def empty?
|
|
158
|
-
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) if Bullet.start? && !loaded?
|
|
159
|
-
origin_empty?
|
|
160
|
-
end
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
::ActiveRecord::Associations::SingularAssociation.class_eval do
|
|
164
|
-
# call has_one and belongs_to associations
|
|
165
|
-
alias_method :origin_reader, :reader
|
|
166
|
-
def reader(force_reload = false)
|
|
167
|
-
result = origin_reader(force_reload)
|
|
168
|
-
if Bullet.start?
|
|
169
|
-
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name, inversed: @inversed)
|
|
170
|
-
Bullet::Detector::NPlusOneQuery.add_possible_objects(result) unless @inversed
|
|
171
|
-
end
|
|
172
|
-
result
|
|
173
|
-
end
|
|
174
|
-
end
|
|
175
|
-
|
|
176
|
-
::ActiveRecord::Associations::HasManyAssociation.class_eval do
|
|
177
|
-
alias_method :origin_has_cached_counter?, :has_cached_counter?
|
|
178
|
-
|
|
179
|
-
def has_cached_counter?(reflection = reflection())
|
|
180
|
-
result = origin_has_cached_counter?(reflection)
|
|
181
|
-
Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name) if Bullet.start? && !result
|
|
182
|
-
result
|
|
183
|
-
end
|
|
184
|
-
end
|
|
185
|
-
|
|
186
|
-
::ActiveRecord::Associations::CollectionProxy.class_eval do
|
|
187
|
-
def count(column_name = nil, options = {})
|
|
188
|
-
if Bullet.start?
|
|
189
|
-
Bullet::Detector::CounterCache.add_counter_cache(proxy_association.owner, proxy_association.reflection.name)
|
|
190
|
-
Bullet::Detector::NPlusOneQuery.call_association(proxy_association.owner, proxy_association.reflection.name)
|
|
191
|
-
end
|
|
192
|
-
super(column_name, options)
|
|
193
|
-
end
|
|
194
|
-
end
|
|
195
|
-
end
|
|
196
|
-
end
|
|
197
|
-
end
|
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Bullet
|
|
4
|
-
module ActiveRecord
|
|
5
|
-
def self.enable
|
|
6
|
-
require 'active_record'
|
|
7
|
-
::ActiveRecord::Base.class_eval do
|
|
8
|
-
class << self
|
|
9
|
-
alias_method :origin_find_by_sql, :find_by_sql
|
|
10
|
-
def find_by_sql(sql, binds = [])
|
|
11
|
-
result = origin_find_by_sql(sql, binds)
|
|
12
|
-
if Bullet.start?
|
|
13
|
-
if result.is_a? Array
|
|
14
|
-
if result.size > 1
|
|
15
|
-
Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
|
|
16
|
-
Bullet::Detector::CounterCache.add_possible_objects(result)
|
|
17
|
-
elsif result.size == 1
|
|
18
|
-
Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)
|
|
19
|
-
Bullet::Detector::CounterCache.add_impossible_object(result.first)
|
|
20
|
-
end
|
|
21
|
-
elsif result.is_a? ::ActiveRecord::Base
|
|
22
|
-
Bullet::Detector::NPlusOneQuery.add_impossible_object(result)
|
|
23
|
-
Bullet::Detector::CounterCache.add_impossible_object(result)
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
result
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
::ActiveRecord::Relation.class_eval do
|
|
32
|
-
alias_method :origin_to_a, :to_a
|
|
33
|
-
|
|
34
|
-
# if select a collection of objects, then these objects have possible to cause N+1 query.
|
|
35
|
-
# if select only one object, then the only one object has impossible to cause N+1 query.
|
|
36
|
-
def to_a
|
|
37
|
-
records = origin_to_a
|
|
38
|
-
if Bullet.start?
|
|
39
|
-
if records.first.class.name !~ /^HABTM_/
|
|
40
|
-
if records.size > 1
|
|
41
|
-
Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
|
|
42
|
-
Bullet::Detector::CounterCache.add_possible_objects(records)
|
|
43
|
-
elsif records.size == 1
|
|
44
|
-
Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
|
|
45
|
-
Bullet::Detector::CounterCache.add_impossible_object(records.first)
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
records
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
::ActiveRecord::Persistence.class_eval do
|
|
54
|
-
def _create_record_with_bullet(*args)
|
|
55
|
-
_create_record_without_bullet(*args).tap do
|
|
56
|
-
Bullet::Detector::NPlusOneQuery.update_inversed_object(self)
|
|
57
|
-
Bullet::Detector::NPlusOneQuery.add_impossible_object(self)
|
|
58
|
-
end
|
|
59
|
-
end
|
|
60
|
-
alias_method_chain :_create_record, :bullet
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
::ActiveRecord::Associations::Preloader.class_eval do
|
|
64
|
-
alias_method :origin_preloaders_on, :preloaders_on
|
|
65
|
-
|
|
66
|
-
def preloaders_on(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
|
-
origin_preloaders_on(association, records, scope)
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
::ActiveRecord::FinderMethods.class_eval do
|
|
79
|
-
# add includes in scope
|
|
80
|
-
alias_method :origin_find_with_associations, :find_with_associations
|
|
81
|
-
def find_with_associations
|
|
82
|
-
return origin_find_with_associations { |r| yield r } if block_given?
|
|
83
|
-
|
|
84
|
-
records = origin_find_with_associations
|
|
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)
|
|
89
|
-
end
|
|
90
|
-
records
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
::ActiveRecord::Associations::JoinDependency.class_eval do
|
|
95
|
-
alias_method :origin_instantiate, :instantiate
|
|
96
|
-
alias_method :origin_construct_model, :construct_model
|
|
97
|
-
|
|
98
|
-
def instantiate(result_set, aliases)
|
|
99
|
-
@bullet_eager_loadings = {}
|
|
100
|
-
records = origin_instantiate(result_set, aliases)
|
|
101
|
-
|
|
102
|
-
if Bullet.start?
|
|
103
|
-
@bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
|
|
104
|
-
objects = eager_loadings_hash.keys
|
|
105
|
-
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
|
|
106
|
-
end
|
|
107
|
-
end
|
|
108
|
-
records
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
# call join associations
|
|
112
|
-
def construct_model(record, node, row, model_cache, id, aliases)
|
|
113
|
-
result = origin_construct_model(record, node, row, model_cache, id, aliases)
|
|
114
|
-
|
|
115
|
-
if Bullet.start?
|
|
116
|
-
associations = [node.reflection.name]
|
|
117
|
-
if node.reflection.nested?
|
|
118
|
-
associations << node.reflection.through_reflection.name
|
|
119
|
-
end
|
|
120
|
-
associations.each do |association|
|
|
121
|
-
Bullet::Detector::Association.add_object_associations(record, association)
|
|
122
|
-
Bullet::Detector::NPlusOneQuery.call_association(record, association)
|
|
123
|
-
@bullet_eager_loadings[record.class] ||= {}
|
|
124
|
-
@bullet_eager_loadings[record.class][record] ||= Set.new
|
|
125
|
-
@bullet_eager_loadings[record.class][record] << association
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
result
|
|
130
|
-
end
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
::ActiveRecord::Associations::CollectionAssociation.class_eval do
|
|
134
|
-
# call one to many associations
|
|
135
|
-
alias_method :origin_load_target, :load_target
|
|
136
|
-
def load_target
|
|
137
|
-
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name, inversed: @inversed) if Bullet.start?
|
|
138
|
-
origin_load_target
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
alias_method :origin_empty?, :empty?
|
|
142
|
-
def empty?
|
|
143
|
-
if Bullet.start? && !has_cached_counter?(@reflection)
|
|
144
|
-
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
|
|
145
|
-
end
|
|
146
|
-
origin_empty?
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
alias_method :origin_include?, :include?
|
|
150
|
-
def include?(object)
|
|
151
|
-
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) if Bullet.start?
|
|
152
|
-
origin_include?(object)
|
|
153
|
-
end
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
::ActiveRecord::Associations::SingularAssociation.class_eval do
|
|
157
|
-
# call has_one and belongs_to associations
|
|
158
|
-
alias_method :origin_reader, :reader
|
|
159
|
-
def reader(force_reload = false)
|
|
160
|
-
result = origin_reader(force_reload)
|
|
161
|
-
if Bullet.start?
|
|
162
|
-
if @owner.class.name !~ /^HABTM_/
|
|
163
|
-
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name, inversed: @inversed)
|
|
164
|
-
Bullet::Detector::NPlusOneQuery.add_possible_objects(result) unless @inversed
|
|
165
|
-
end
|
|
166
|
-
end
|
|
167
|
-
result
|
|
168
|
-
end
|
|
169
|
-
end
|
|
170
|
-
|
|
171
|
-
::ActiveRecord::Associations::HasManyAssociation.class_eval do
|
|
172
|
-
alias_method :origin_count_records, :count_records
|
|
173
|
-
def count_records
|
|
174
|
-
result = has_cached_counter?
|
|
175
|
-
Bullet::Detector::CounterCache.add_counter_cache(@owner, @reflection.name) if Bullet.start? && !result
|
|
176
|
-
origin_count_records
|
|
177
|
-
end
|
|
178
|
-
end
|
|
179
|
-
|
|
180
|
-
::ActiveRecord::Associations::CollectionProxy.class_eval do
|
|
181
|
-
def count(column_name = nil, options = {})
|
|
182
|
-
if Bullet.start?
|
|
183
|
-
Bullet::Detector::CounterCache.add_counter_cache(proxy_association.owner, proxy_association.reflection.name)
|
|
184
|
-
Bullet::Detector::NPlusOneQuery.call_association(proxy_association.owner, proxy_association.reflection.name)
|
|
185
|
-
end
|
|
186
|
-
super(column_name, options)
|
|
187
|
-
end
|
|
188
|
-
end
|
|
189
|
-
end
|
|
190
|
-
end
|
|
191
|
-
end
|