bullet 5.7.0 → 5.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/Gemfile.rails-5.2 +1 -1
- data/Guardfile +1 -1
- data/README.md +3 -0
- data/Rakefile +4 -4
- data/bullet.gemspec +2 -3
- data/lib/bullet.rb +23 -23
- data/lib/bullet/active_record4.rb +3 -1
- data/lib/bullet/active_record41.rb +1 -1
- data/lib/bullet/active_record42.rb +8 -9
- data/lib/bullet/active_record5.rb +27 -28
- data/lib/bullet/active_record52.rb +27 -28
- data/lib/bullet/detector/association.rb +12 -12
- data/lib/bullet/detector/counter_cache.rb +7 -7
- data/lib/bullet/detector/n_plus_one_query.rb +6 -6
- data/lib/bullet/detector/unused_eager_loading.rb +23 -21
- data/lib/bullet/ext/object.rb +4 -4
- data/lib/bullet/ext/string.rb +1 -1
- data/lib/bullet/notification/base.rb +14 -14
- data/lib/bullet/notification/n_plus_one_query.rb +4 -4
- data/lib/bullet/notification/unused_eager_loading.rb +4 -4
- data/lib/bullet/notification_collector.rb +0 -1
- data/lib/bullet/version.rb +1 -2
- data/perf/benchmark.rb +7 -7
- data/spec/bullet/detector/n_plus_one_query_spec.rb +8 -8
- data/spec/bullet/ext/object_spec.rb +1 -1
- data/spec/bullet/notification/base_spec.rb +6 -6
- data/spec/bullet/notification/counter_cache_spec.rb +1 -1
- data/spec/bullet/notification/n_plus_one_query_spec.rb +1 -1
- data/spec/bullet/notification/unused_eager_loading_spec.rb +1 -1
- data/spec/bullet/rack_spec.rb +9 -10
- data/spec/bullet/registry/association_spec.rb +2 -2
- data/spec/bullet/registry/base_spec.rb +3 -3
- data/spec/bullet_spec.rb +7 -7
- data/spec/integration/active_record/association_spec.rb +16 -16
- data/spec/integration/counter_cache_spec.rb +2 -2
- data/spec/models/mongoid/address.rb +1 -1
- data/spec/models/mongoid/category.rb +2 -2
- data/spec/models/mongoid/comment.rb +1 -1
- data/spec/models/mongoid/company.rb +1 -1
- data/spec/models/mongoid/entry.rb +1 -1
- data/spec/models/mongoid/post.rb +4 -4
- data/spec/spec_helper.rb +1 -1
- data/spec/support/mongo_seed.rb +23 -23
- data/spec/support/rack_double.rb +7 -16
- data/spec/support/sqlite_seed.rb +73 -73
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 45f885605935c340cac5cf587663a77526f4b9d5
|
4
|
+
data.tar.gz: 4982228f2d3e7722bb920e515a16aa65906c21d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2cca58f89a5fc6bf1154d82d301f54748b3822aebb8fbcbada75cb71daeffce1d9e5b6d8bb163c0844947933e2ecf12eb48b9af2ad37223b1f3c113209ab8b09
|
7
|
+
data.tar.gz: 7f1c4692b84a07a285af404764eee88849f01cfed587e732d30d2f9056ce99a34b319a6093c591bb148715603a39725ac3ed6e8dccc8f61f6d425f39d5d841a5
|
data/CHANGELOG.md
CHANGED
data/Gemfile.rails-5.2
CHANGED
data/Guardfile
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# A sample Guardfile
|
2
2
|
# More info at https://github.com/guard/guard#readme
|
3
3
|
|
4
|
-
guard 'rspec', :
|
4
|
+
guard 'rspec', version: 2, all_after_pass: false, all_on_start: false, cli: '--color --format nested --fail-fast' do
|
5
5
|
watch(%r{^spec/.+_spec\.rb$})
|
6
6
|
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
7
7
|
watch('spec/spec_helper.rb') { 'spec' }
|
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/bullet.png)](http://badge.fury.io/rb/bullet)
|
4
4
|
[![Build Status](https://secure.travis-ci.org/flyerhzm/bullet.png)](http://travis-ci.org/flyerhzm/bullet)
|
5
5
|
<a href="https://codeclimate.com/github/flyerhzm/bullet"><img src="https://codeclimate.com/github/flyerhzm/bullet.png" /></a>
|
6
|
+
[ ![AwesomeCode Status for flyerhzm/bullet](https://awesomecode.io/projects/6755235b-e2c1-459e-bf92-b8b13d0c0472/status)](https://awesomecode.io/projects/2)
|
6
7
|
[![Coderwall Endorse](http://api.coderwall.com/flyerhzm/endorsecount.png)](http://coderwall.com/flyerhzm)
|
7
8
|
|
8
9
|
The Bullet gem is designed to help you increase your application's performance by reducing the number of queries it makes. It will watch your queries while you develop your application and notify you when you should add eager loading (N+1 queries), when you're using eager loading that isn't necessary and when you should use counter cache.
|
@@ -48,6 +49,7 @@ Bullet won't do ANYTHING unless you tell it to explicitly. Append to
|
|
48
49
|
```ruby
|
49
50
|
config.after_initialize do
|
50
51
|
Bullet.enable = true
|
52
|
+
Bullet.sentry = true
|
51
53
|
Bullet.alert = true
|
52
54
|
Bullet.bullet_logger = true
|
53
55
|
Bullet.console = true
|
@@ -82,6 +84,7 @@ The code above will enable all of the Bullet notification systems:
|
|
82
84
|
* `Bullet.bugsnag`: add notifications to bugsnag
|
83
85
|
* `Bullet.airbrake`: add notifications to airbrake
|
84
86
|
* `Bullet.rollbar`: add notifications to rollbar
|
87
|
+
* `Bullet.sentry`: add notifications to sentry
|
85
88
|
* `Bullet.add_footer`: adds the details in the bottom left corner of the page. Double click the footer or use close button to hide footer.
|
86
89
|
* `Bullet.stacktrace_includes`: include paths with any of these substrings in the stack trace, even if they are not in your main app
|
87
90
|
* `Bullet.stacktrace_excludes`: ignore paths with any of these substrings in the stack trace, even if they are not in your main app.
|
data/Rakefile
CHANGED
@@ -12,11 +12,11 @@ task :build do
|
|
12
12
|
system 'gem build bullet.gemspec'
|
13
13
|
end
|
14
14
|
|
15
|
-
task :
|
15
|
+
task install: :build do
|
16
16
|
system "sudo gem install bullet-#{Bullet::VERSION}.gem"
|
17
17
|
end
|
18
18
|
|
19
|
-
task :
|
19
|
+
task release: :build do
|
20
20
|
puts "Tagging #{Bullet::VERSION}..."
|
21
21
|
system "git tag -a #{Bullet::VERSION} -m 'Tagging #{Bullet::VERSION}'"
|
22
22
|
puts 'Pushing to Github...'
|
@@ -30,7 +30,7 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
|
|
30
30
|
end
|
31
31
|
|
32
32
|
RSpec::Core::RakeTask.new('spec:progress') do |spec|
|
33
|
-
spec.rspec_opts = %w
|
33
|
+
spec.rspec_opts = %w[--format progress]
|
34
34
|
spec.pattern = 'spec/**/*_spec.rb'
|
35
35
|
end
|
36
36
|
|
@@ -48,4 +48,4 @@ rescue LoadError
|
|
48
48
|
puts 'RDocTask is not supported for this platform'
|
49
49
|
end
|
50
50
|
|
51
|
-
task :
|
51
|
+
task default: :spec
|
data/bullet.gemspec
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
lib = File.expand_path('../lib/', __FILE__)
|
2
|
-
|
2
|
+
$LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)
|
3
3
|
|
4
4
|
require 'bullet/version'
|
5
5
|
|
@@ -18,10 +18,9 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.required_rubygems_version = '>= 1.3.6'
|
19
19
|
|
20
20
|
s.add_runtime_dependency 'activesupport', '>= 3.0.0'
|
21
|
-
s.add_runtime_dependency 'uniform_notifier', '~> 1.
|
21
|
+
s.add_runtime_dependency 'uniform_notifier', '~> 1.11.0'
|
22
22
|
|
23
23
|
s.files = `git ls-files`.split("\n")
|
24
24
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
25
|
s.require_paths = ['lib']
|
26
26
|
end
|
27
|
-
|
data/lib/bullet.rb
CHANGED
@@ -29,13 +29,13 @@ module Bullet
|
|
29
29
|
end
|
30
30
|
|
31
31
|
class << self
|
32
|
-
attr_writer :
|
33
|
-
attr_reader :
|
32
|
+
attr_writer :n_plus_one_query_enable, :unused_eager_loading_enable, :counter_cache_enable, :stacktrace_includes, :stacktrace_excludes
|
33
|
+
attr_reader :whitelist
|
34
34
|
attr_accessor :add_footer, :orm_pathches_applied
|
35
35
|
|
36
36
|
available_notifiers = UniformNotifier::AVAILABLE_NOTIFIERS.map { |notifier| "#{notifier}=" }
|
37
|
-
available_notifiers << { :
|
38
|
-
delegate
|
37
|
+
available_notifiers << { to: UniformNotifier }
|
38
|
+
delegate(*available_notifiers)
|
39
39
|
|
40
40
|
def raise=(should_raise)
|
41
41
|
UniformNotifier.raise = (should_raise ? Notification::UnoptimizedQueryError : false)
|
@@ -62,15 +62,15 @@ module Bullet
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def n_plus_one_query_enable?
|
65
|
-
|
65
|
+
enable? && !!@n_plus_one_query_enable
|
66
66
|
end
|
67
67
|
|
68
68
|
def unused_eager_loading_enable?
|
69
|
-
|
69
|
+
enable? && !!@unused_eager_loading_enable
|
70
70
|
end
|
71
71
|
|
72
72
|
def counter_cache_enable?
|
73
|
-
|
73
|
+
enable? && !!@counter_cache_enable
|
74
74
|
end
|
75
75
|
|
76
76
|
def stacktrace_includes
|
@@ -91,7 +91,7 @@ module Bullet
|
|
91
91
|
reset_whitelist
|
92
92
|
@whitelist[options[:type]][options[:class_name]] ||= []
|
93
93
|
@whitelist[options[:type]][options[:class_name]].delete(options[:association].to_sym)
|
94
|
-
@whitelist[options[:type]].delete_if { |
|
94
|
+
@whitelist[options[:type]].delete_if { |_key, val| val.empty? }
|
95
95
|
end
|
96
96
|
|
97
97
|
def get_whitelist_associations(type, class_name)
|
@@ -99,7 +99,7 @@ module Bullet
|
|
99
99
|
end
|
100
100
|
|
101
101
|
def reset_whitelist
|
102
|
-
@whitelist ||= { :
|
102
|
+
@whitelist ||= { n_plus_one_query: {}, unused_eager_loading: {}, counter_cache: {} }
|
103
103
|
end
|
104
104
|
|
105
105
|
def clear_whitelist
|
@@ -109,7 +109,7 @@ module Bullet
|
|
109
109
|
def bullet_logger=(active)
|
110
110
|
if active
|
111
111
|
require 'fileutils'
|
112
|
-
root_path =
|
112
|
+
root_path = (rails? ? Rails.root.to_s : Dir.pwd).to_s
|
113
113
|
FileUtils.mkdir_p(root_path + '/log')
|
114
114
|
bullet_log_file = File.open("#{root_path}/log/bullet.log", 'a+')
|
115
115
|
bullet_log_file.sync = true
|
@@ -219,23 +219,23 @@ module Bullet
|
|
219
219
|
|
220
220
|
private
|
221
221
|
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
end
|
222
|
+
def for_each_active_notifier_with_notification
|
223
|
+
UniformNotifier.active_notifiers.each do |notifier|
|
224
|
+
notification_collector.collection.each do |notification|
|
225
|
+
notification.notifier = notifier
|
226
|
+
yield notification
|
228
227
|
end
|
229
228
|
end
|
229
|
+
end
|
230
230
|
|
231
|
-
|
232
|
-
|
231
|
+
def build_request_uri(env)
|
232
|
+
return "#{env['REQUEST_METHOD']} #{env['REQUEST_URI']}" if env['REQUEST_URI']
|
233
233
|
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
end
|
234
|
+
if env['QUERY_STRING'].present?
|
235
|
+
"#{env['REQUEST_METHOD']} #{env['PATH_INFO']}?#{env['QUERY_STRING']}"
|
236
|
+
else
|
237
|
+
"#{env['REQUEST_METHOD']} #{env['PATH_INFO']}"
|
239
238
|
end
|
239
|
+
end
|
240
240
|
end
|
241
241
|
end
|
@@ -105,7 +105,7 @@ module Bullet
|
|
105
105
|
records = origin_instantiate(rows)
|
106
106
|
|
107
107
|
if Bullet.start?
|
108
|
-
@bullet_eager_loadings.each do |
|
108
|
+
@bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
|
109
109
|
objects = eager_loadings_hash.keys
|
110
110
|
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
|
111
111
|
end
|
@@ -187,6 +187,7 @@ module Bullet
|
|
187
187
|
::ActiveRecord::Associations::HasManyAssociation.class_eval do
|
188
188
|
alias_method :origin_has_cached_counter?, :has_cached_counter?
|
189
189
|
|
190
|
+
# rubocop:disable Style/MethodCallWithoutArgsParentheses
|
190
191
|
def has_cached_counter?(reflection = reflection())
|
191
192
|
result = origin_has_cached_counter?(reflection)
|
192
193
|
if Bullet.start? && !result
|
@@ -194,6 +195,7 @@ module Bullet
|
|
194
195
|
end
|
195
196
|
result
|
196
197
|
end
|
198
|
+
# rubocop:enable Style/MethodCallWithoutArgsParentheses
|
197
199
|
end
|
198
200
|
end
|
199
201
|
end
|
@@ -108,7 +108,7 @@ module Bullet
|
|
108
108
|
records = origin_instantiate(result_set, aliases)
|
109
109
|
|
110
110
|
if Bullet.start?
|
111
|
-
@bullet_eager_loadings.each do |
|
111
|
+
@bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
|
112
112
|
objects = eager_loadings_hash.keys
|
113
113
|
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
|
114
114
|
end
|
@@ -124,7 +124,7 @@ module Bullet
|
|
124
124
|
records = origin_instantiate(result_set, aliases)
|
125
125
|
|
126
126
|
if Bullet.start?
|
127
|
-
@bullet_eager_loadings.each do |
|
127
|
+
@bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
|
128
128
|
objects = eager_loadings_hash.keys
|
129
129
|
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
|
130
130
|
end
|
@@ -138,14 +138,13 @@ module Bullet
|
|
138
138
|
parent.children.each do |node|
|
139
139
|
key = aliases.column_alias(node, node.primary_key)
|
140
140
|
id = row[key]
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
end
|
141
|
+
next unless id.nil?
|
142
|
+
associations = node.reflection.name
|
143
|
+
Bullet::Detector::Association.add_object_associations(ar_parent, associations)
|
144
|
+
Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations)
|
145
|
+
@bullet_eager_loadings[ar_parent.class] ||= {}
|
146
|
+
@bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new
|
147
|
+
@bullet_eager_loadings[ar_parent.class][ar_parent] << associations
|
149
148
|
end
|
150
149
|
end
|
151
150
|
end
|
@@ -18,7 +18,7 @@ module Bullet
|
|
18
18
|
module ActiveRecord
|
19
19
|
def self.enable
|
20
20
|
require 'active_record'
|
21
|
-
::ActiveRecord::Base.extend(Module.new
|
21
|
+
::ActiveRecord::Base.extend(Module.new do
|
22
22
|
def find_by_sql(sql, binds = [], preparable: nil, &block)
|
23
23
|
result = super
|
24
24
|
if Bullet.start?
|
@@ -37,11 +37,11 @@ module Bullet
|
|
37
37
|
end
|
38
38
|
result
|
39
39
|
end
|
40
|
-
|
40
|
+
end)
|
41
41
|
|
42
42
|
::ActiveRecord::Base.prepend(SaveWithBulletSupport)
|
43
43
|
|
44
|
-
::ActiveRecord::Relation.prepend(Module.new
|
44
|
+
::ActiveRecord::Relation.prepend(Module.new do
|
45
45
|
# if select a collection of objects, then these objects have possible to cause N+1 query.
|
46
46
|
# if select only one object, then the only one object has impossible to cause N+1 query.
|
47
47
|
def records
|
@@ -59,9 +59,9 @@ module Bullet
|
|
59
59
|
end
|
60
60
|
result
|
61
61
|
end
|
62
|
-
|
62
|
+
end)
|
63
63
|
|
64
|
-
::ActiveRecord::Associations::Preloader.prepend(Module.new
|
64
|
+
::ActiveRecord::Associations::Preloader.prepend(Module.new do
|
65
65
|
def preloaders_for_one(association, records, scope)
|
66
66
|
if Bullet.start?
|
67
67
|
records.compact!
|
@@ -74,9 +74,9 @@ module Bullet
|
|
74
74
|
end
|
75
75
|
super
|
76
76
|
end
|
77
|
-
|
77
|
+
end)
|
78
78
|
|
79
|
-
::ActiveRecord::FinderMethods.prepend(Module.new
|
79
|
+
::ActiveRecord::FinderMethods.prepend(Module.new do
|
80
80
|
# add includes in scope
|
81
81
|
def find_with_associations
|
82
82
|
return super { |r| yield r } if block_given?
|
@@ -90,15 +90,15 @@ module Bullet
|
|
90
90
|
end
|
91
91
|
records
|
92
92
|
end
|
93
|
-
|
93
|
+
end)
|
94
94
|
|
95
|
-
::ActiveRecord::Associations::JoinDependency.prepend(Module.new
|
95
|
+
::ActiveRecord::Associations::JoinDependency.prepend(Module.new do
|
96
96
|
def instantiate(result_set, aliases)
|
97
97
|
@bullet_eager_loadings = {}
|
98
98
|
records = super
|
99
99
|
|
100
100
|
if Bullet.start?
|
101
|
-
@bullet_eager_loadings.each do |
|
101
|
+
@bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
|
102
102
|
objects = eager_loadings_hash.keys
|
103
103
|
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
|
104
104
|
end
|
@@ -112,14 +112,13 @@ module Bullet
|
|
112
112
|
parent.children.each do |node|
|
113
113
|
key = aliases.column_alias(node, node.primary_key)
|
114
114
|
id = row[key]
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
end
|
115
|
+
next unless id.nil?
|
116
|
+
associations = node.reflection.name
|
117
|
+
Bullet::Detector::Association.add_object_associations(ar_parent, associations)
|
118
|
+
Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations)
|
119
|
+
@bullet_eager_loadings[ar_parent.class] ||= {}
|
120
|
+
@bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new
|
121
|
+
@bullet_eager_loadings[ar_parent.class][ar_parent] << associations
|
123
122
|
end
|
124
123
|
end
|
125
124
|
end
|
@@ -142,16 +141,16 @@ module Bullet
|
|
142
141
|
|
143
142
|
result
|
144
143
|
end
|
145
|
-
|
144
|
+
end)
|
146
145
|
|
147
|
-
::ActiveRecord::Associations::CollectionAssociation.prepend(Module.new
|
146
|
+
::ActiveRecord::Associations::CollectionAssociation.prepend(Module.new do
|
148
147
|
def load_target
|
149
148
|
records = super
|
150
149
|
|
151
150
|
if Bullet.start?
|
152
|
-
if
|
151
|
+
if is_a? ::ActiveRecord::Associations::ThroughAssociation
|
153
152
|
Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
|
154
|
-
association =
|
153
|
+
association = owner.association through_reflection.name
|
155
154
|
Array(association.target).each do |through_record|
|
156
155
|
Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
|
157
156
|
end
|
@@ -183,9 +182,9 @@ module Bullet
|
|
183
182
|
end
|
184
183
|
super
|
185
184
|
end
|
186
|
-
|
185
|
+
end)
|
187
186
|
|
188
|
-
::ActiveRecord::Associations::SingularAssociation.prepend(Module.new
|
187
|
+
::ActiveRecord::Associations::SingularAssociation.prepend(Module.new do
|
189
188
|
# call has_one and belongs_to associations
|
190
189
|
def target
|
191
190
|
result = super()
|
@@ -201,9 +200,9 @@ module Bullet
|
|
201
200
|
end
|
202
201
|
result
|
203
202
|
end
|
204
|
-
|
203
|
+
end)
|
205
204
|
|
206
|
-
::ActiveRecord::Associations::HasManyAssociation.prepend(Module.new
|
205
|
+
::ActiveRecord::Associations::HasManyAssociation.prepend(Module.new do
|
207
206
|
def empty?
|
208
207
|
result = super
|
209
208
|
if Bullet.start? && !reflection.has_cached_counter?
|
@@ -214,12 +213,12 @@ module Bullet
|
|
214
213
|
|
215
214
|
def count_records
|
216
215
|
result = reflection.has_cached_counter?
|
217
|
-
if Bullet.start? && !result && !
|
216
|
+
if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
|
218
217
|
Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
|
219
218
|
end
|
220
219
|
super
|
221
220
|
end
|
222
|
-
|
221
|
+
end)
|
223
222
|
end
|
224
223
|
end
|
225
224
|
end
|
@@ -18,7 +18,7 @@ module Bullet
|
|
18
18
|
module ActiveRecord
|
19
19
|
def self.enable
|
20
20
|
require 'active_record'
|
21
|
-
::ActiveRecord::Base.extend(Module.new
|
21
|
+
::ActiveRecord::Base.extend(Module.new do
|
22
22
|
def find_by_sql(sql, binds = [], preparable: nil, &block)
|
23
23
|
result = super
|
24
24
|
if Bullet.start?
|
@@ -37,11 +37,11 @@ module Bullet
|
|
37
37
|
end
|
38
38
|
result
|
39
39
|
end
|
40
|
-
|
40
|
+
end)
|
41
41
|
|
42
42
|
::ActiveRecord::Base.prepend(SaveWithBulletSupport)
|
43
43
|
|
44
|
-
::ActiveRecord::Relation.prepend(Module.new
|
44
|
+
::ActiveRecord::Relation.prepend(Module.new do
|
45
45
|
# if select a collection of objects, then these objects have possible to cause N+1 query.
|
46
46
|
# if select only one object, then the only one object has impossible to cause N+1 query.
|
47
47
|
def records
|
@@ -59,9 +59,9 @@ module Bullet
|
|
59
59
|
end
|
60
60
|
result
|
61
61
|
end
|
62
|
-
|
62
|
+
end)
|
63
63
|
|
64
|
-
::ActiveRecord::Associations::Preloader.prepend(Module.new
|
64
|
+
::ActiveRecord::Associations::Preloader.prepend(Module.new do
|
65
65
|
def preloaders_for_one(association, records, scope)
|
66
66
|
if Bullet.start?
|
67
67
|
records.compact!
|
@@ -74,9 +74,9 @@ module Bullet
|
|
74
74
|
end
|
75
75
|
super
|
76
76
|
end
|
77
|
-
|
77
|
+
end)
|
78
78
|
|
79
|
-
::ActiveRecord::FinderMethods.prepend(Module.new
|
79
|
+
::ActiveRecord::FinderMethods.prepend(Module.new do
|
80
80
|
# add includes in scope
|
81
81
|
def find_with_associations
|
82
82
|
return super { |r| yield r } if block_given?
|
@@ -90,15 +90,15 @@ module Bullet
|
|
90
90
|
end
|
91
91
|
records
|
92
92
|
end
|
93
|
-
|
93
|
+
end)
|
94
94
|
|
95
|
-
::ActiveRecord::Associations::JoinDependency.prepend(Module.new
|
95
|
+
::ActiveRecord::Associations::JoinDependency.prepend(Module.new do
|
96
96
|
def instantiate(result_set, &block)
|
97
97
|
@bullet_eager_loadings = {}
|
98
98
|
records = super
|
99
99
|
|
100
100
|
if Bullet.start?
|
101
|
-
@bullet_eager_loadings.each do |
|
101
|
+
@bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
|
102
102
|
objects = eager_loadings_hash.keys
|
103
103
|
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
|
104
104
|
end
|
@@ -112,14 +112,13 @@ module Bullet
|
|
112
112
|
parent.children.each do |node|
|
113
113
|
key = aliases.column_alias(node, node.primary_key)
|
114
114
|
id = row[key]
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
end
|
115
|
+
next unless id.nil?
|
116
|
+
associations = node.reflection.name
|
117
|
+
Bullet::Detector::Association.add_object_associations(ar_parent, associations)
|
118
|
+
Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations)
|
119
|
+
@bullet_eager_loadings[ar_parent.class] ||= {}
|
120
|
+
@bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new
|
121
|
+
@bullet_eager_loadings[ar_parent.class][ar_parent] << associations
|
123
122
|
end
|
124
123
|
end
|
125
124
|
end
|
@@ -142,16 +141,16 @@ module Bullet
|
|
142
141
|
|
143
142
|
result
|
144
143
|
end
|
145
|
-
|
144
|
+
end)
|
146
145
|
|
147
|
-
::ActiveRecord::Associations::CollectionAssociation.prepend(Module.new
|
146
|
+
::ActiveRecord::Associations::CollectionAssociation.prepend(Module.new do
|
148
147
|
def load_target
|
149
148
|
records = super
|
150
149
|
|
151
150
|
if Bullet.start?
|
152
|
-
if
|
151
|
+
if is_a? ::ActiveRecord::Associations::ThroughAssociation
|
153
152
|
Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
|
154
|
-
association =
|
153
|
+
association = owner.association through_reflection.name
|
155
154
|
Array(association.target).each do |through_record|
|
156
155
|
Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
|
157
156
|
end
|
@@ -183,9 +182,9 @@ module Bullet
|
|
183
182
|
end
|
184
183
|
super
|
185
184
|
end
|
186
|
-
|
185
|
+
end)
|
187
186
|
|
188
|
-
::ActiveRecord::Associations::SingularAssociation.prepend(Module.new
|
187
|
+
::ActiveRecord::Associations::SingularAssociation.prepend(Module.new do
|
189
188
|
# call has_one and belongs_to associations
|
190
189
|
def target
|
191
190
|
result = super()
|
@@ -201,9 +200,9 @@ module Bullet
|
|
201
200
|
end
|
202
201
|
result
|
203
202
|
end
|
204
|
-
|
203
|
+
end)
|
205
204
|
|
206
|
-
::ActiveRecord::Associations::HasManyAssociation.prepend(Module.new
|
205
|
+
::ActiveRecord::Associations::HasManyAssociation.prepend(Module.new do
|
207
206
|
def empty?
|
208
207
|
result = super
|
209
208
|
if Bullet.start? && !reflection.has_cached_counter?
|
@@ -214,12 +213,12 @@ module Bullet
|
|
214
213
|
|
215
214
|
def count_records
|
216
215
|
result = reflection.has_cached_counter?
|
217
|
-
if Bullet.start? && !result && !
|
216
|
+
if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
|
218
217
|
Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
|
219
218
|
end
|
220
219
|
super
|
221
220
|
end
|
222
|
-
|
221
|
+
end)
|
223
222
|
end
|
224
223
|
end
|
225
224
|
end
|