bullet 5.7.0 → 5.8.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.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -1
  3. data/Gemfile.rails-5.2 +1 -1
  4. data/Guardfile +1 -1
  5. data/README.md +9 -4
  6. data/Rakefile +5 -5
  7. data/bullet.gemspec +11 -6
  8. data/lib/bullet/active_record4.rb +18 -14
  9. data/lib/bullet/active_record41.rb +16 -14
  10. data/lib/bullet/active_record42.rb +24 -22
  11. data/lib/bullet/active_record5.rb +78 -49
  12. data/lib/bullet/active_record52.rb +47 -40
  13. data/lib/bullet/dependency.rb +3 -1
  14. data/lib/bullet/detector/association.rb +16 -14
  15. data/lib/bullet/detector/base.rb +2 -0
  16. data/lib/bullet/detector/counter_cache.rb +10 -7
  17. data/lib/bullet/detector/n_plus_one_query.rb +17 -15
  18. data/lib/bullet/detector/unused_eager_loading.rb +26 -21
  19. data/lib/bullet/detector.rb +2 -0
  20. data/lib/bullet/ext/object.rb +6 -4
  21. data/lib/bullet/ext/string.rb +3 -1
  22. data/lib/bullet/mongoid4x.rb +7 -2
  23. data/lib/bullet/mongoid5x.rb +7 -2
  24. data/lib/bullet/mongoid6x.rb +11 -6
  25. data/lib/bullet/notification/base.rb +21 -15
  26. data/lib/bullet/notification/counter_cache.rb +2 -0
  27. data/lib/bullet/notification/n_plus_one_query.rb +6 -4
  28. data/lib/bullet/notification/unused_eager_loading.rb +6 -4
  29. data/lib/bullet/notification.rb +2 -0
  30. data/lib/bullet/notification_collector.rb +2 -1
  31. data/lib/bullet/rack.rb +19 -15
  32. data/lib/bullet/registry/association.rb +2 -0
  33. data/lib/bullet/registry/base.rb +2 -0
  34. data/lib/bullet/registry/object.rb +2 -0
  35. data/lib/bullet/registry.rb +2 -0
  36. data/lib/bullet/stack_trace_filter.rb +56 -19
  37. data/lib/bullet/version.rb +2 -1
  38. data/lib/bullet.rb +31 -29
  39. data/lib/generators/bullet/install_generator.rb +5 -3
  40. data/perf/benchmark.rb +9 -7
  41. data/rails/init.rb +2 -0
  42. data/spec/bullet/detector/association_spec.rb +2 -0
  43. data/spec/bullet/detector/base_spec.rb +2 -0
  44. data/spec/bullet/detector/counter_cache_spec.rb +2 -0
  45. data/spec/bullet/detector/n_plus_one_query_spec.rb +33 -8
  46. data/spec/bullet/detector/unused_eager_loading_spec.rb +2 -0
  47. data/spec/bullet/ext/object_spec.rb +3 -1
  48. data/spec/bullet/ext/string_spec.rb +2 -0
  49. data/spec/bullet/notification/base_spec.rb +8 -6
  50. data/spec/bullet/notification/counter_cache_spec.rb +3 -1
  51. data/spec/bullet/notification/n_plus_one_query_spec.rb +3 -1
  52. data/spec/bullet/notification/unused_eager_loading_spec.rb +3 -1
  53. data/spec/bullet/notification_collector_spec.rb +2 -0
  54. data/spec/bullet/rack_spec.rb +10 -9
  55. data/spec/bullet/registry/association_spec.rb +4 -2
  56. data/spec/bullet/registry/base_spec.rb +5 -3
  57. data/spec/bullet/registry/object_spec.rb +2 -0
  58. data/spec/bullet_spec.rb +9 -7
  59. data/spec/integration/active_record/association_spec.rb +79 -17
  60. data/spec/integration/counter_cache_spec.rb +4 -2
  61. data/spec/integration/mongoid/association_spec.rb +2 -0
  62. data/spec/models/address.rb +2 -0
  63. data/spec/models/author.rb +2 -0
  64. data/spec/models/base_user.rb +2 -0
  65. data/spec/models/category.rb +2 -0
  66. data/spec/models/city.rb +2 -0
  67. data/spec/models/client.rb +4 -0
  68. data/spec/models/comment.rb +2 -0
  69. data/spec/models/company.rb +2 -0
  70. data/spec/models/country.rb +2 -0
  71. data/spec/models/document.rb +2 -0
  72. data/spec/models/entry.rb +2 -0
  73. data/spec/models/firm.rb +3 -0
  74. data/spec/models/folder.rb +2 -0
  75. data/spec/models/group.rb +4 -0
  76. data/spec/models/mongoid/address.rb +3 -1
  77. data/spec/models/mongoid/category.rb +4 -2
  78. data/spec/models/mongoid/comment.rb +3 -1
  79. data/spec/models/mongoid/company.rb +3 -1
  80. data/spec/models/mongoid/entry.rb +3 -1
  81. data/spec/models/mongoid/post.rb +6 -4
  82. data/spec/models/mongoid/user.rb +2 -0
  83. data/spec/models/newspaper.rb +2 -0
  84. data/spec/models/page.rb +2 -0
  85. data/spec/models/person.rb +2 -0
  86. data/spec/models/pet.rb +2 -0
  87. data/spec/models/post.rb +17 -0
  88. data/spec/models/relationship.rb +2 -0
  89. data/spec/models/reply.rb +2 -0
  90. data/spec/models/student.rb +2 -0
  91. data/spec/models/submission.rb +2 -0
  92. data/spec/models/teacher.rb +2 -0
  93. data/spec/models/user.rb +2 -0
  94. data/spec/models/writer.rb +2 -0
  95. data/spec/spec_helper.rb +3 -1
  96. data/spec/support/bullet_ext.rb +2 -0
  97. data/spec/support/mongo_seed.rb +32 -30
  98. data/spec/support/rack_double.rb +9 -16
  99. data/spec/support/sqlite_seed.rb +84 -75
  100. data/tasks/bullet_tasks.rake +2 -0
  101. metadata +11 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7c9650d2dbc870d2ac04be4ca0e19723ef6a85c2
4
- data.tar.gz: 35839d605fb1e11687836dbcbe12572b1eb07775
3
+ metadata.gz: 0b65855ae5365567a18d1fb6ffdee5046b8cec35
4
+ data.tar.gz: 8df6b7afd18400dd2fe9bf39ec4a537fa04228af
5
5
  SHA512:
6
- metadata.gz: 5064948ab9db44cc9b4082397990a3d91e93dc6986db2767a528a4a05734b1d1ba9231af337624d79d7e312d13e075afb53217191d017a3c511a0889c436d0a0
7
- data.tar.gz: 2c12bd045e5e41b4db0761b07562a763d3be089231d5e420fc3d7d8d27a40f184216c9090667883a606e35fcdfd2eda3f276838e60696c642bff0939ab35e85f
6
+ metadata.gz: 212cf02f1520dfaf752f794e42e9b2611fa0eca8b786bf5ef281c36c416ddf24016a524029614447c6241ebb734a2491ab554fa505544d7a7bc6f2e5b767527d
7
+ data.tar.gz: d2d71ceb54033f58cb6d675ea88268f130a0ea8bcfd7b4987486e374a2d2a3fc9169691ff20a9ca0fd9cf2787656dacc19649a8473879e9d1d68484597ccc18b
data/CHANGELOG.md CHANGED
@@ -1,4 +1,19 @@
1
- # Next Release
1
+ ## Next Release
2
+
3
+ ## 5.8.0 (10/29/2018)
4
+
5
+ * Fix through reflection for rails 5.x
6
+ * Fix false positive in after_save/after_create callbacks
7
+ * Don't triger a preload error on "manual" preloads
8
+ * Avoid Bullet from making extra queries in mongoid6
9
+ * Support option for #first and #last on mongoid6.x
10
+ * Fix duplicate logs in mongoid 4.x and 5.x version
11
+ * Use caller for ruby 1.9 while caller_locations for 2.0+
12
+ * Extend stacktrace matching for sub-file precision
13
+ * Exclude configured bundler path in addition to '/vendor'
14
+ * Fix `caller_path` in `excluded_stacktrace_path`
15
+ * Update `uniform_notifier` dependency to add Sentry support
16
+ * Integrate awesomecode.io and refactor code
2
17
 
3
18
  ## 5.7.0 (12/03/2017)
4
19
 
data/Gemfile.rails-5.2 CHANGED
@@ -2,7 +2,7 @@ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'rails', '~> 5.2.0.beta1'
5
+ gem 'rails', '~> 5.2.0'
6
6
  gem 'sqlite3'
7
7
  gem 'activerecord-jdbcsqlite3-adapter', platforms: [:jruby]
8
8
  gem 'activerecord-import'
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', :version => 2, :all_after_pass => false, :all_on_start => false, :cli => '--color --format nested --fail-fast' do
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
@@ -2,7 +2,7 @@
2
2
 
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
- <a href="https://codeclimate.com/github/flyerhzm/bullet"><img src="https://codeclimate.com/github/flyerhzm/bullet.png" /></a>
5
+ [![AwesomeCode Status for flyerhzm/bullet](https://awesomecode.io/projects/6755235b-e2c1-459e-bf92-b8b13d0c0472/status)](https://awesomecode.io/repos/flyerhzm/bullet)
6
6
  [![Coderwall Endorse](http://api.coderwall.com/flyerhzm/endorsecount.png)](http://coderwall.com/flyerhzm)
7
7
 
8
8
  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 +48,7 @@ Bullet won't do ANYTHING unless you tell it to explicitly. Append to
48
48
  ```ruby
49
49
  config.after_initialize do
50
50
  Bullet.enable = true
51
+ Bullet.sentry = true
51
52
  Bullet.alert = true
52
53
  Bullet.bullet_logger = true
53
54
  Bullet.console = true
@@ -63,7 +64,7 @@ config.after_initialize do
63
64
  Bullet.rollbar = true
64
65
  Bullet.add_footer = true
65
66
  Bullet.stacktrace_includes = [ 'your_gem', 'your_middleware' ]
66
- Bullet.stacktrace_excludes = [ 'their_gem', 'their_middleware' ]
67
+ Bullet.stacktrace_excludes = [ 'their_gem', 'their_middleware', ['my_file.rb', 'my_method'], ['my_file.rb', 16..20] ]
67
68
  Bullet.slack = { webhook_url: 'http://some.slack.url', channel: '#default', username: 'notifier' }
68
69
  end
69
70
  ```
@@ -82,9 +83,12 @@ The code above will enable all of the Bullet notification systems:
82
83
  * `Bullet.bugsnag`: add notifications to bugsnag
83
84
  * `Bullet.airbrake`: add notifications to airbrake
84
85
  * `Bullet.rollbar`: add notifications to rollbar
86
+ * `Bullet.sentry`: add notifications to sentry
85
87
  * `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
88
  * `Bullet.stacktrace_includes`: include paths with any of these substrings in the stack trace, even if they are not in your main app
87
89
  * `Bullet.stacktrace_excludes`: ignore paths with any of these substrings in the stack trace, even if they are not in your main app.
90
+ Each item can be a string (match substring), a regex, or an array where the first item is a path to match, and the second
91
+ item is a line number, a Range of line numbers, or a (bare) method name, to exclude only particular lines in a file.
88
92
  * `Bullet.slack`: add notifications to slack
89
93
  * `Bullet.raise`: raise errors, useful for making your specs fail unless they have optimized queries
90
94
 
@@ -124,10 +128,11 @@ class ApplicationController < ActionController::Base
124
128
  around_action :skip_bullet
125
129
 
126
130
  def skip_bullet
131
+ previous_value = Bullet.enable?
127
132
  Bullet.enable = false
128
133
  yield
129
134
  ensure
130
- Bullet.enable = true
135
+ Bullet.enable = previous_value
131
136
  end
132
137
  end
133
138
  ```
@@ -178,7 +183,7 @@ If you find Bullet does not work for you, *please disable your browser's cache*.
178
183
 
179
184
  ### Profile a job
180
185
 
181
- The Bullet gem uses rack middleware to profile requests. If you want to use Bullet without an http server, like to profile a job, you can use use profile method and fetch warnings
186
+ The Bullet gem uses rack middleware to profile requests. If you want to use Bullet without an http server, like to profile a job, you can use the profile method and fetch warnings
182
187
 
183
188
  ```ruby
184
189
  Bullet.profile do
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
- $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
1
+ $LOAD_PATH.unshift File.expand_path('lib', __dir__)
2
2
  require 'bundler'
3
3
  Bundler.setup
4
4
 
@@ -12,11 +12,11 @@ task :build do
12
12
  system 'gem build bullet.gemspec'
13
13
  end
14
14
 
15
- task :install => :build do
15
+ task install: :build do
16
16
  system "sudo gem install bullet-#{Bullet::VERSION}.gem"
17
17
  end
18
18
 
19
- task :release => :build do
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(--format progress)
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 :default => :spec
51
+ task default: :spec
data/bullet.gemspec CHANGED
@@ -1,5 +1,7 @@
1
- lib = File.expand_path('../lib/', __FILE__)
2
- $:.unshift lib unless $:.include?(lib)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)
3
5
 
4
6
  require 'bullet/version'
5
7
 
@@ -9,19 +11,22 @@ Gem::Specification.new do |s|
9
11
  s.platform = Gem::Platform::RUBY
10
12
  s.authors = ['Richard Huang']
11
13
  s.email = ['flyerhzm@gmail.com']
12
- s.homepage = 'http://github.com/flyerhzm/bullet'
14
+ s.homepage = 'https://github.com/flyerhzm/bullet'
13
15
  s.summary = 'help to kill N+1 queries and unused eager loading.'
14
16
  s.description = 'help to kill N+1 queries and unused eager loading.'
17
+ s.metadata = {
18
+ 'changelog_uri' => 'https://github.com/flyerhzm/bullet/blob/master/CHANGELOG.md',
19
+ 'source_code_uri' => 'https://github.com/flyerhzm/bullet'
20
+ }
15
21
 
16
- s.license = 'MIT'
22
+ s.license = 'MIT'
17
23
 
18
24
  s.required_rubygems_version = '>= 1.3.6'
19
25
 
20
26
  s.add_runtime_dependency 'activesupport', '>= 3.0.0'
21
- s.add_runtime_dependency 'uniform_notifier', '~> 1.10.0'
27
+ s.add_runtime_dependency 'uniform_notifier', '~> 1.11'
22
28
 
23
29
  s.files = `git ls-files`.split("\n")
24
30
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
31
  s.require_paths = ['lib']
26
32
  end
27
-
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bullet
2
4
  module ActiveRecord
3
5
  def self.enable
@@ -46,21 +48,12 @@ module Bullet
46
48
  end
47
49
 
48
50
  ::ActiveRecord::Persistence.class_eval do
49
- def save_with_bullet(*args, &proc)
50
- was_new_record = new_record?
51
- save_without_bullet(*args, &proc).tap do |result|
52
- Bullet::Detector::NPlusOneQuery.add_impossible_object(self) if result && was_new_record
53
- end
54
- end
55
- alias_method_chain :save, :bullet
56
-
57
- def save_with_bullet!(*args, &proc)
58
- was_new_record = new_record?
59
- save_without_bullet!(*args, &proc).tap do |result|
60
- Bullet::Detector::NPlusOneQuery.add_impossible_object(self) if result && was_new_record
51
+ def _create_record_with_bullet(*args)
52
+ _create_record_without_bullet(*args).tap do
53
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(self)
61
54
  end
62
55
  end
63
- alias_method_chain :save!, :bullet
56
+ alias_method_chain :_create_record, :bullet
64
57
  end
65
58
 
66
59
  ::ActiveRecord::Associations::Preloader.class_eval do
@@ -72,6 +65,7 @@ module Bullet
72
65
  if Bullet.start?
73
66
  records = [records].flatten.compact.uniq
74
67
  return if records.empty?
68
+
75
69
  records.each do |record|
76
70
  Bullet::Detector::Association.add_object_associations(record, associations)
77
71
  end
@@ -105,7 +99,7 @@ module Bullet
105
99
  records = origin_instantiate(rows)
106
100
 
107
101
  if Bullet.start?
108
- @bullet_eager_loadings.each do |klazz, eager_loadings_hash|
102
+ @bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
109
103
  objects = eager_loadings_hash.keys
110
104
  Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
111
105
  end
@@ -187,6 +181,7 @@ module Bullet
187
181
  ::ActiveRecord::Associations::HasManyAssociation.class_eval do
188
182
  alias_method :origin_has_cached_counter?, :has_cached_counter?
189
183
 
184
+ # rubocop:disable Style/MethodCallWithoutArgsParentheses
190
185
  def has_cached_counter?(reflection = reflection())
191
186
  result = origin_has_cached_counter?(reflection)
192
187
  if Bullet.start? && !result
@@ -194,6 +189,15 @@ module Bullet
194
189
  end
195
190
  result
196
191
  end
192
+ # rubocop:enable Style/MethodCallWithoutArgsParentheses
193
+ end
194
+
195
+ ::ActiveRecord::Associations::BelongsToAssociation.class_eval do
196
+ def writer_with_bullet(record)
197
+ Bullet::Detector::Association.add_object_associations(owner, reflection.name) if Bullet.start?
198
+ writer_without_bullet(record)
199
+ end
200
+ alias_method_chain :writer, :bullet
197
201
  end
198
202
  end
199
203
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bullet
2
4
  module ActiveRecord
3
5
  def self.enable
@@ -48,21 +50,12 @@ module Bullet
48
50
  end
49
51
 
50
52
  ::ActiveRecord::Persistence.class_eval do
51
- def save_with_bullet(*args, &proc)
52
- was_new_record = new_record?
53
- save_without_bullet(*args, &proc).tap do |result|
54
- Bullet::Detector::NPlusOneQuery.add_impossible_object(self) if result && was_new_record
55
- end
56
- end
57
- alias_method_chain :save, :bullet
58
-
59
- def save_with_bullet!(*args, &proc)
60
- was_new_record = new_record?
61
- save_without_bullet!(*args, &proc).tap do |result|
62
- Bullet::Detector::NPlusOneQuery.add_impossible_object(self) if result && was_new_record
53
+ def _create_record_with_bullet(*args)
54
+ _create_record_without_bullet(*args).tap do
55
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(self)
63
56
  end
64
57
  end
65
- alias_method_chain :save!, :bullet
58
+ alias_method_chain :_create_record, :bullet
66
59
  end
67
60
 
68
61
  ::ActiveRecord::Associations::Preloader.class_eval do
@@ -87,6 +80,7 @@ module Bullet
87
80
  alias_method :origin_find_with_associations, :find_with_associations
88
81
  def find_with_associations
89
82
  return origin_find_with_associations { |r| yield r } if block_given?
83
+
90
84
  records = origin_find_with_associations
91
85
  if Bullet.start?
92
86
  associations = (eager_load_values + includes_values).uniq
@@ -108,7 +102,7 @@ module Bullet
108
102
  records = origin_instantiate(result_set, aliases)
109
103
 
110
104
  if Bullet.start?
111
- @bullet_eager_loadings.each do |klazz, eager_loadings_hash|
105
+ @bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
112
106
  objects = eager_loadings_hash.keys
113
107
  Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
114
108
  end
@@ -185,6 +179,14 @@ module Bullet
185
179
  origin_count_records
186
180
  end
187
181
  end
182
+
183
+ ::ActiveRecord::Associations::BelongsToAssociation.class_eval do
184
+ def writer_with_bullet(record)
185
+ Bullet::Detector::Association.add_object_associations(owner, reflection.name) if Bullet.start?
186
+ writer_without_bullet(record)
187
+ end
188
+ alias_method_chain :writer, :bullet
189
+ end
188
190
  end
189
191
  end
190
192
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bullet
2
4
  module ActiveRecord
3
5
  def self.enable
@@ -42,21 +44,12 @@ module Bullet
42
44
  end
43
45
 
44
46
  ::ActiveRecord::Persistence.class_eval do
45
- def save_with_bullet(*args, &proc)
46
- was_new_record = new_record?
47
- save_without_bullet(*args, &proc).tap do |result|
48
- Bullet::Detector::NPlusOneQuery.add_impossible_object(self) if result && was_new_record
47
+ def _create_record_with_bullet(*args)
48
+ _create_record_without_bullet(*args).tap do
49
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(self)
49
50
  end
50
51
  end
51
- alias_method_chain :save, :bullet
52
-
53
- def save_with_bullet!(*args, &proc)
54
- was_new_record = new_record?
55
- save_without_bullet!(*args, &proc).tap do |result|
56
- Bullet::Detector::NPlusOneQuery.add_impossible_object(self) if result && was_new_record
57
- end
58
- end
59
- alias_method_chain :save!, :bullet
52
+ alias_method_chain :_create_record, :bullet
60
53
  end
61
54
 
62
55
  ::ActiveRecord::Relation.class_eval do
@@ -102,6 +95,7 @@ module Bullet
102
95
  alias_method :origin_find_with_associations, :find_with_associations
103
96
  def find_with_associations
104
97
  return origin_find_with_associations { |r| yield r } if block_given?
98
+
105
99
  records = origin_find_with_associations
106
100
  if Bullet.start?
107
101
  associations = (eager_load_values + includes_values).uniq
@@ -124,7 +118,7 @@ module Bullet
124
118
  records = origin_instantiate(result_set, aliases)
125
119
 
126
120
  if Bullet.start?
127
- @bullet_eager_loadings.each do |klazz, eager_loadings_hash|
121
+ @bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
128
122
  objects = eager_loadings_hash.keys
129
123
  Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
130
124
  end
@@ -138,14 +132,14 @@ module Bullet
138
132
  parent.children.each do |node|
139
133
  key = aliases.column_alias(node, node.primary_key)
140
134
  id = row[key]
141
- if 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
148
- end
135
+ next unless id.nil?
136
+
137
+ associations = node.reflection.name
138
+ Bullet::Detector::Association.add_object_associations(ar_parent, associations)
139
+ Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations)
140
+ @bullet_eager_loadings[ar_parent.class] ||= {}
141
+ @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new
142
+ @bullet_eager_loadings[ar_parent.class][ar_parent] << associations
149
143
  end
150
144
  end
151
145
  end
@@ -246,6 +240,14 @@ module Bullet
246
240
  origin_count_records
247
241
  end
248
242
  end
243
+
244
+ ::ActiveRecord::Associations::BelongsToAssociation.class_eval do
245
+ def writer_with_bullet(record)
246
+ Bullet::Detector::Association.add_object_associations(owner, reflection.name) if Bullet.start?
247
+ writer_without_bullet(record)
248
+ end
249
+ alias_method_chain :writer, :bullet
250
+ end
249
251
  end
250
252
  end
251
253
  end
@@ -1,16 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bullet
2
4
  module SaveWithBulletSupport
3
- def save(*args)
4
- was_new_record = new_record?
5
- super(*args).tap do |result|
6
- Bullet::Detector::NPlusOneQuery.add_impossible_object(self) if result && was_new_record
7
- end
8
- end
9
-
10
- def save!(*args)
11
- was_new_record = new_record?
12
- super(*args).tap do |result|
13
- Bullet::Detector::NPlusOneQuery.add_impossible_object(self) if result && was_new_record
5
+ def _create_record(*)
6
+ super do
7
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(self)
8
+ yield(self) if block_given?
14
9
  end
15
10
  end
16
11
  end
@@ -18,7 +13,7 @@ module Bullet
18
13
  module ActiveRecord
19
14
  def self.enable
20
15
  require 'active_record'
21
- ::ActiveRecord::Base.extend(Module.new {
16
+ ::ActiveRecord::Base.extend(Module.new do
22
17
  def find_by_sql(sql, binds = [], preparable: nil, &block)
23
18
  result = super
24
19
  if Bullet.start?
@@ -37,11 +32,11 @@ module Bullet
37
32
  end
38
33
  result
39
34
  end
40
- })
35
+ end)
41
36
 
42
37
  ::ActiveRecord::Base.prepend(SaveWithBulletSupport)
43
38
 
44
- ::ActiveRecord::Relation.prepend(Module.new {
39
+ ::ActiveRecord::Relation.prepend(Module.new do
45
40
  # if select a collection of objects, then these objects have possible to cause N+1 query.
46
41
  # if select only one object, then the only one object has impossible to cause N+1 query.
47
42
  def records
@@ -59,9 +54,9 @@ module Bullet
59
54
  end
60
55
  result
61
56
  end
62
- })
57
+ end)
63
58
 
64
- ::ActiveRecord::Associations::Preloader.prepend(Module.new {
59
+ ::ActiveRecord::Associations::Preloader.prepend(Module.new do
65
60
  def preloaders_for_one(association, records, scope)
66
61
  if Bullet.start?
67
62
  records.compact!
@@ -74,12 +69,13 @@ module Bullet
74
69
  end
75
70
  super
76
71
  end
77
- })
72
+ end)
78
73
 
79
- ::ActiveRecord::FinderMethods.prepend(Module.new {
74
+ ::ActiveRecord::FinderMethods.prepend(Module.new do
80
75
  # add includes in scope
81
76
  def find_with_associations
82
77
  return super { |r| yield r } if block_given?
78
+
83
79
  records = super
84
80
  if Bullet.start?
85
81
  associations = (eager_load_values + includes_values).uniq
@@ -90,20 +86,37 @@ module Bullet
90
86
  end
91
87
  records
92
88
  end
93
- })
94
-
95
- ::ActiveRecord::Associations::JoinDependency.prepend(Module.new {
96
- def instantiate(result_set, aliases)
97
- @bullet_eager_loadings = {}
98
- records = super
99
-
100
- if Bullet.start?
101
- @bullet_eager_loadings.each do |klazz, eager_loadings_hash|
102
- objects = eager_loadings_hash.keys
103
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
89
+ end)
90
+
91
+ ::ActiveRecord::Associations::JoinDependency.prepend(Module.new do
92
+ if ::ActiveRecord::Associations::JoinDependency.instance_method(:instantiate).parameters.last[0] == :block
93
+ # ActiveRecord >= 5.1.5
94
+ def instantiate(result_set, &block)
95
+ @bullet_eager_loadings = {}
96
+ records = super
97
+
98
+ if Bullet.start?
99
+ @bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
100
+ objects = eager_loadings_hash.keys
101
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
102
+ end
104
103
  end
104
+ records
105
+ end
106
+ else
107
+ # ActiveRecord <= 5.1.4
108
+ def instantiate(result_set, aliases)
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(objects, eager_loadings_hash[objects.first].to_a)
116
+ end
117
+ end
118
+ records
105
119
  end
106
- records
107
120
  end
108
121
 
109
122
  def construct(ar_parent, parent, row, rs, seen, model_cache, aliases)
@@ -112,14 +125,14 @@ module Bullet
112
125
  parent.children.each do |node|
113
126
  key = aliases.column_alias(node, node.primary_key)
114
127
  id = row[key]
115
- if 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
122
- end
128
+ next unless id.nil?
129
+
130
+ associations = node.reflection.name
131
+ Bullet::Detector::Association.add_object_associations(ar_parent, associations)
132
+ Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations)
133
+ @bullet_eager_loadings[ar_parent.class] ||= {}
134
+ @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new
135
+ @bullet_eager_loadings[ar_parent.class][ar_parent] << associations
123
136
  end
124
137
  end
125
138
  end
@@ -142,19 +155,28 @@ module Bullet
142
155
 
143
156
  result
144
157
  end
145
- })
158
+ end)
146
159
 
147
- ::ActiveRecord::Associations::CollectionAssociation.prepend(Module.new {
160
+ ::ActiveRecord::Associations::CollectionAssociation.prepend(Module.new do
148
161
  def load_target
149
162
  records = super
150
163
 
151
164
  if Bullet.start?
152
- if self.is_a? ::ActiveRecord::Associations::ThroughAssociation
153
- Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
154
- association = self.owner.association self.through_reflection.name
165
+ if is_a? ::ActiveRecord::Associations::ThroughAssociation
166
+ refl = reflection.through_reflection
167
+ Bullet::Detector::NPlusOneQuery.call_association(owner, refl.name)
168
+ association = owner.association refl.name
155
169
  Array(association.target).each do |through_record|
156
170
  Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
157
171
  end
172
+
173
+ if refl.through_reflection?
174
+ while refl.through_reflection?
175
+ refl = refl.through_reflection
176
+ end
177
+
178
+ Bullet::Detector::NPlusOneQuery.call_association(owner, refl.name)
179
+ end
158
180
  end
159
181
  Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) unless @inversed
160
182
  if records.first.class.name !~ /^HABTM_/
@@ -183,9 +205,9 @@ module Bullet
183
205
  end
184
206
  super
185
207
  end
186
- })
208
+ end)
187
209
 
188
- ::ActiveRecord::Associations::SingularAssociation.prepend(Module.new {
210
+ ::ActiveRecord::Associations::SingularAssociation.prepend(Module.new do
189
211
  # call has_one and belongs_to associations
190
212
  def target
191
213
  result = super()
@@ -201,9 +223,9 @@ module Bullet
201
223
  end
202
224
  result
203
225
  end
204
- })
226
+ end)
205
227
 
206
- ::ActiveRecord::Associations::HasManyAssociation.prepend(Module.new {
228
+ ::ActiveRecord::Associations::HasManyAssociation.prepend(Module.new do
207
229
  def empty?
208
230
  result = super
209
231
  if Bullet.start? && !reflection.has_cached_counter?
@@ -214,12 +236,19 @@ module Bullet
214
236
 
215
237
  def count_records
216
238
  result = reflection.has_cached_counter?
217
- if Bullet.start? && !result && !self.is_a?(::ActiveRecord::Associations::ThroughAssociation)
239
+ if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
218
240
  Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
219
241
  end
220
242
  super
221
243
  end
222
- })
244
+ end)
245
+
246
+ ::ActiveRecord::Associations::BelongsToAssociation.prepend(Module.new do
247
+ def writer(record)
248
+ Bullet::Detector::Association.add_object_associations(owner, reflection.name) if Bullet.start?
249
+ super
250
+ end
251
+ end)
223
252
  end
224
253
  end
225
254
  end