bullet 5.5.0 → 5.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f8cac0418ea4a1bb9f8abc6c12e84022f2f118d
4
- data.tar.gz: ebdf99115037dec22a5480a63d3bed5f15956dda
3
+ metadata.gz: '06945d7fd724e3e1f6355de32b078fac99e6366a'
4
+ data.tar.gz: ab28a1e628416d945ec40ad21b0ff3e8e4dd50ac
5
5
  SHA512:
6
- metadata.gz: 0904f996964a403a495af310bab8a0612e614eb72a71d08646948508c25d4bf36d2b939da65156a5fc81595477820e0f123aa2a88d618fe97e28e81a6b6f8ada
7
- data.tar.gz: d491e12ccce38a26a5e3d27d3f6b84acafe34bb7b6aff56b78a8e22a73a5a3abf88443110587de0080e50b32ef70d8ca387eab5faac0b1bec16efed04bf3acc1
6
+ metadata.gz: 33e0206bd7d7624e3ca311f48274708fcc8030a23ec77954007faaa6b2b14b1d4fd3a7f75c18a8150399565770dd19e252856c2cc94907c6abd3147488280bbf
7
+ data.tar.gz: cac678c258a0d278c92cbff1bb5e820d9ec71d628a55573588d3fb100a2ded60d28e496c7906632d438f367835d41b923aa98828028693183610840a22c5d114
@@ -3,6 +3,7 @@ language: ruby
3
3
  rvm:
4
4
  - 2.3.0
5
5
  gemfile:
6
+ - Gemfile.rails-5.1
6
7
  - Gemfile.rails-5.0
7
8
  - Gemfile.rails-4.2
8
9
  - Gemfile.rails-4.1
@@ -1,5 +1,11 @@
1
1
  # Next Release
2
2
 
3
+ ## 5.5.1 (03/01/2016)
4
+
5
+ * Fix inverse_of for rails 5
6
+ * Fix detect file attachment for rack #319
7
+ * Fix `ActiveRecord::Associations::SingularAssociation#reader` usage for Rails 5.1
8
+
3
9
  ## 5.5.0 (12/30/2016)
4
10
 
5
11
  * Display http request method #311
@@ -0,0 +1,15 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'rails', '5.1.0.beta.1'
6
+ gem 'sqlite3'
7
+ gem 'activerecord-jdbcsqlite3-adapter', platforms: [:jruby]
8
+ gem 'activerecord-import'
9
+
10
+ gem "rspec"
11
+
12
+ platforms :rbx do
13
+ gem 'rubysl', '~> 2.0'
14
+ gem 'rubinius-developer_tools'
15
+ end
data/Hacking.md CHANGED
@@ -28,7 +28,7 @@ So the flow of a request goes like this:
28
28
  the notification collector
29
29
  2. The request is handled by Rails, and the installed ActiveRecord extensions
30
30
  trigger Detector callbacks
31
- 3. Detectors once called, will determine whether something noteworthy happend.
31
+ 3. Detectors once called, will determine whether something noteworthy happened.
32
32
  If yes, then a Notification is created and stored in the notification collector.
33
33
  4. Rails finishes handling the request
34
34
  5. For each notification in the collector, Bullet will iterate over each
data/README.md CHANGED
@@ -13,7 +13,7 @@ Bullet gem now supports **activerecord** >= 4.0 and **mongoid** >= 4.0.
13
13
 
14
14
  If you use activerecord 2.x, please use bullet <= 4.5.0
15
15
 
16
- If you use activerecord 3.x, plesae use bullet < 5.5.0
16
+ If you use activerecord 3.x, please use bullet < 5.5.0
17
17
 
18
18
  ## External Introduction
19
19
 
@@ -34,7 +34,7 @@ or add it into a Gemfile (Bundler):
34
34
 
35
35
 
36
36
  ```ruby
37
- gem "bullet", :group => "development"
37
+ gem 'bullet', group: 'development'
38
38
  ```
39
39
 
40
40
  **Note**: make sure `bullet` gem is added after activerecord (rails) and
@@ -70,23 +70,24 @@ end
70
70
 
71
71
  The notifier of Bullet is a wrap of [uniform_notifier](https://github.com/flyerhzm/uniform_notifier)
72
72
 
73
- The code above will enable all seven of the Bullet notification systems:
73
+ The code above will enable all of the Bullet notification systems:
74
74
  * `Bullet.enable`: enable Bullet gem, otherwise do nothing
75
75
  * `Bullet.alert`: pop up a JavaScript alert in the browser
76
76
  * `Bullet.bullet_logger`: log to the Bullet log file (Rails.root/log/bullet.log)
77
+ * `Bullet.console`: log warnings to your browser's console.log (Safari/Webkit browsers or Firefox w/Firebug installed)
78
+ * `Bullet.growl`: pop up Growl warnings if your system has Growl installed. Requires a little bit of configuration
79
+ * `Bullet.xmpp`: send XMPP/Jabber notifications to the receiver indicated. Note that the code will currently not handle the adding of contacts, so you will need to make both accounts indicated know each other manually before you will receive any notifications. If you restart the development server frequently, the 'coming online' sound for the Bullet account may start to annoy - in this case set :show_online_status to false; you will still get notifications, but the Bullet account won't announce it's online status anymore.
77
80
  * `Bullet.rails_logger`: add warnings directly to the Rails log
78
81
  * `Bullet.honeybadger`: add notifications to Honeybadger
79
82
  * `Bullet.bugsnag`: add notifications to bugsnag
80
83
  * `Bullet.airbrake`: add notifications to airbrake
81
84
  * `Bullet.rollbar`: add notifications to rollbar
82
- * `Bullet.console`: log warnings to your browser's console.log (Safari/Webkit browsers or Firefox w/Firebug installed)
83
- * `Bullet.growl`: pop up Growl warnings if your system has Growl installed. Requires a little bit of configuration
84
- * `Bullet.xmpp`: send XMPP/Jabber notifications to the receiver indicated. Note that the code will currently not handle the adding of contacts, so you will need to make both accounts indicated know each other manually before you will receive any notifications. If you restart the development server frequently, the 'coming online' sound for the Bullet account may start to annoy - in this case set :show_online_status to false; you will still get notifications, but the Bullet account won't announce it's online status anymore.
85
- * `Bullet.raise`: raise errors, useful for making your specs fail unless they have optimized queries
86
85
  * `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.
87
86
  * `Bullet.stacktrace_includes`: include paths with any of these substrings in the stack trace, even if they are not in your main app
88
87
  * `Bullet.stacktrace_excludes`: ignore paths with any of these substrings in the stack trace, even if they are not in your main app.
89
88
  * `Bullet.slack`: add notifications to slack
89
+ * `Bullet.raise`: raise errors, useful for making your specs fail unless they have optimized queries
90
+
90
91
 
91
92
  Bullet also allows you to disable any of its detectors.
92
93
 
@@ -21,8 +21,8 @@ module Bullet
21
21
  ::ActiveRecord::Base.class_eval do
22
22
  class <<self
23
23
  alias_method :origin_find_by_sql, :find_by_sql
24
- def find_by_sql(sql, binds = [], preparable: nil)
25
- result = origin_find_by_sql(sql, binds, preparable: nil)
24
+ def find_by_sql(sql, binds = [], preparable: nil, &block)
25
+ result = origin_find_by_sql(sql, binds, preparable: nil, &block)
26
26
  if Bullet.start?
27
27
  if result.is_a? Array
28
28
  if result.size > 1
@@ -204,7 +204,7 @@ module Bullet
204
204
  # call has_one and belongs_to associations
205
205
  alias_method :origin_reader, :reader
206
206
  def reader(force_reload = false)
207
- result = origin_reader(force_reload)
207
+ result = force_reload ? force_reload_reader : origin_reader
208
208
  if Bullet.start?
209
209
  if owner.class.name !~ /^HABTM_/ && !@inversed
210
210
  Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
@@ -11,6 +11,7 @@ module Bullet
11
11
  # if it is, keeps this unpreload associations and caller.
12
12
  def call_association(object, associations)
13
13
  return unless Bullet.start?
14
+ return unless Bullet.n_plus_one_query_enable?
14
15
  return unless object.primary_key_value
15
16
  return if inversed_objects.include?(object.bullet_key, associations)
16
17
  add_call_object_associations(object, associations)
@@ -68,7 +69,7 @@ module Bullet
68
69
  value = object_associations[object.bullet_key]
69
70
  if value
70
71
  value.each do |v|
71
- # associations == v comparision order is important here because
72
+ # associations == v comparison order is important here because
72
73
  # v variable might be a squeel node where :== method is redefined,
73
74
  # so it does not compare values at all and return unexpected results
74
75
  result = v.is_a?(Hash) ? v.key?(associations) : associations == v
@@ -55,7 +55,7 @@ module Bullet
55
55
  end
56
56
 
57
57
  def file?(headers)
58
- headers["Content-Transfer-Encoding"] == "binary"
58
+ headers["Content-Transfer-Encoding"] == "binary" || headers["Content-Disposition"]
59
59
  end
60
60
 
61
61
  def sse?(headers)
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Bullet
3
- VERSION = "5.5.0"
3
+ VERSION = "5.5.1"
4
4
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- if !mongoid? && active_record4?
3
+ if active_record?
4
4
  describe Bullet::Detector::Association, 'has_many' do
5
5
  context "post => comments" do
6
6
  it "should detect non preload post => comments" do
@@ -33,15 +33,7 @@ module Support
33
33
  end
34
34
 
35
35
  def setup_db
36
- if Mongoid::VERSION =~ /\A2\.[4-8]/
37
- Mongoid.configure do |config|
38
- config.master = Mongo::Connection.new.db("bullet")
39
- end
40
- elsif Mongoid::VERSION =~ /\A3/
41
- Mongoid.configure do |config|
42
- config.connect_to("bullet")
43
- end
44
- elsif Mongoid::VERSION =~ /\A4/
36
+ if Mongoid::VERSION =~ /\A4/
45
37
  Mongoid.configure do |config|
46
38
  config.load_configuration(
47
39
  sessions: {
@@ -52,7 +44,7 @@ module Support
52
44
  }
53
45
  )
54
46
  end
55
- elsif Mongoid::VERSION =~ /\A5/
47
+ else
56
48
  Mongoid.configure do |config|
57
49
  config.load_configuration(
58
50
  clients: {
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bullet
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.5.0
4
+ version: 5.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-30 00:00:00.000000000 Z
11
+ date: 2017-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -58,6 +58,7 @@ files:
58
58
  - Gemfile.rails-4.1
59
59
  - Gemfile.rails-4.2
60
60
  - Gemfile.rails-5.0
61
+ - Gemfile.rails-5.1
61
62
  - Guardfile
62
63
  - Hacking.md
63
64
  - MIT-LICENSE
@@ -113,8 +114,7 @@ files:
113
114
  - spec/bullet/registry/base_spec.rb
114
115
  - spec/bullet/registry/object_spec.rb
115
116
  - spec/bullet_spec.rb
116
- - spec/integration/active_record4/association_spec.rb
117
- - spec/integration/active_record5/association_spec.rb
117
+ - spec/integration/active_record/association_spec.rb
118
118
  - spec/integration/counter_cache_spec.rb
119
119
  - spec/integration/mongoid/association_spec.rb
120
120
  - spec/models/address.rb
@@ -177,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
177
  version: 1.3.6
178
178
  requirements: []
179
179
  rubyforge_project:
180
- rubygems_version: 2.5.1
180
+ rubygems_version: 2.5.2
181
181
  signing_key:
182
182
  specification_version: 4
183
183
  summary: help to kill N+1 queries and unused eager loading.
@@ -199,8 +199,7 @@ test_files:
199
199
  - spec/bullet/registry/base_spec.rb
200
200
  - spec/bullet/registry/object_spec.rb
201
201
  - spec/bullet_spec.rb
202
- - spec/integration/active_record4/association_spec.rb
203
- - spec/integration/active_record5/association_spec.rb
202
+ - spec/integration/active_record/association_spec.rb
204
203
  - spec/integration/counter_cache_spec.rb
205
204
  - spec/integration/mongoid/association_spec.rb
206
205
  - spec/models/address.rb
@@ -1,768 +0,0 @@
1
- require 'spec_helper'
2
-
3
- if !mongoid? && active_record5?
4
- describe Bullet::Detector::Association, 'has_many' do
5
- context "post => comments" do
6
- it "should detect non preload post => comments" do
7
- Post.all.each do |post|
8
- post.comments.map(&:name)
9
- end
10
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
11
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
12
-
13
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
14
- end
15
-
16
- it "should detect non preload post => comments for find_by_sql" do
17
- Post.find_by_sql("SELECT * FROM posts").each do |post|
18
- post.comments.map(&:name)
19
- end
20
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
21
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
22
-
23
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
24
- end
25
-
26
- it "should detect preload with post => comments" do
27
- Post.includes(:comments).each do |post|
28
- post.comments.map(&:name)
29
- end
30
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
31
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
32
-
33
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
34
- end
35
-
36
- it "should detect unused preload post => comments" do
37
- Post.includes(:comments).map(&:name)
38
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
39
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
40
-
41
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
42
- end
43
-
44
- it "should not detect unused preload post => comments" do
45
- Post.all.map(&:name)
46
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
47
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
48
-
49
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
50
- end
51
-
52
- it "should detect non preload comment => post with inverse_of" do
53
- Post.includes(:comments).each do |post|
54
- post.comments.each do |comment|
55
- comment.name
56
- comment.post.name
57
- end
58
- end
59
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
60
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
61
-
62
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
63
- end
64
-
65
- it "should detect non preload post => comments with empty?" do
66
- Post.all.each do |post|
67
- post.comments.empty?
68
- end
69
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
70
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
71
-
72
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
73
- end
74
-
75
- it "should detect non preload post => comments with include?" do
76
- comment = Comment.last
77
- Post.all.each do |post|
78
- post.comments.include?(comment)
79
- end
80
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
81
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
82
-
83
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
84
- end
85
-
86
- it "should not detect unused preload person => pets with empty?" do
87
- Person.all.each do |person|
88
- person.pets.empty?
89
- end
90
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
91
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
92
-
93
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
94
- end
95
- end
96
-
97
- context "category => posts => comments" do
98
- it "should detect non preload category => posts => comments" do
99
- Category.all.each do |category|
100
- category.posts.each do |post|
101
- post.comments.map(&:name)
102
- end
103
- end
104
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
105
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
106
-
107
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :posts)
108
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
109
- end
110
-
111
- it "should detect preload category => posts, but no post => comments" do
112
- Category.includes(:posts).each do |category|
113
- category.posts.each do |post|
114
- post.comments.map(&:name)
115
- end
116
- end
117
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
118
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
119
-
120
- expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Category, :posts)
121
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
122
- end
123
-
124
- it "should detect preload with category => posts => comments" do
125
- Category.includes({:posts => :comments}).each do |category|
126
- category.posts.each do |post|
127
- post.comments.map(&:name)
128
- end
129
- end
130
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
131
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
132
-
133
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
134
- end
135
-
136
- it "should detect preload with category => posts => comments with posts.id > 0" do
137
- Category.includes({:posts => :comments}).where('posts.id > 0').references(:posts).each do |category|
138
- category.posts.each do |post|
139
- post.comments.map(&:name)
140
- end
141
- end
142
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
143
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
144
-
145
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
146
- end
147
-
148
- it "should detect unused preload with category => posts => comments" do
149
- Category.includes({:posts => :comments}).map(&:name)
150
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
151
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
152
-
153
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
154
- end
155
-
156
- it "should detect unused preload with post => commnets, no category => posts" do
157
- Category.includes({:posts => :comments}).each do |category|
158
- category.posts.map(&:name)
159
- end
160
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
161
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
162
-
163
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
164
- end
165
- end
166
-
167
- context "category => posts, category => entries" do
168
- it "should detect non preload with category => [posts, entries]" do
169
- Category.all.each do |category|
170
- category.posts.map(&:name)
171
- category.entries.map(&:name)
172
- end
173
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
174
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
175
-
176
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :posts)
177
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :entries)
178
- end
179
-
180
- it "should detect preload with category => posts, but not with category => entries" do
181
- Category.includes(:posts).each do |category|
182
- category.posts.map(&:name)
183
- category.entries.map(&:name)
184
- end
185
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
186
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
187
-
188
- expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Category, :posts)
189
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :entries)
190
- end
191
-
192
- it "should detect preload with category => [posts, entries]" do
193
- Category.includes([:posts, :entries]).each do |category|
194
- category.posts.map(&:name)
195
- category.entries.map(&:name)
196
- end
197
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
198
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
199
-
200
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
201
- end
202
-
203
- it "should detect unused preload with category => [posts, entries]" do
204
- Category.includes([:posts, :entries]).map(&:name)
205
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
206
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Category, :posts)
207
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Category, :entries)
208
-
209
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
210
- end
211
-
212
- it "should detect unused preload with category => entries, but not with category => posts" do
213
- Category.includes([:posts, :entries]).each do |category|
214
- category.posts.map(&:name)
215
- end
216
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
217
- expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Category, :posts)
218
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Category, :entries)
219
-
220
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
221
- end
222
- end
223
-
224
- context "post => comment" do
225
- it "should detect unused preload with post => comments" do
226
- Post.includes(:comments).each do |post|
227
- post.comments.first.name if post.comments.first
228
- end
229
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
230
- expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Post, :comments)
231
-
232
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
233
- end
234
-
235
- it "should detect preload with post => commnets" do
236
- Post.first.comments.map(&:name)
237
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
238
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
239
-
240
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
241
- end
242
-
243
- it "should not detect unused preload with category => posts" do
244
- category = Category.first
245
- category.draft_post.destroy!
246
- post = category.draft_post
247
- post.update_attributes!(link: true)
248
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
249
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
250
-
251
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
252
-
253
- Support::SqliteSeed.setup_db
254
- Support::SqliteSeed.seed_db
255
- end
256
- end
257
-
258
- context "category => posts => writer" do
259
- it "should not detect unused preload associations" do
260
- category = Category.includes({:posts => :writer}).order("id DESC").find_by_name('first')
261
- category.posts.map do |post|
262
- post.name
263
- post.writer.name
264
- end
265
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
266
- expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Category, :posts)
267
- expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Post, :writer)
268
- end
269
- end
270
-
271
- context "scope for_category_name" do
272
- it "should detect preload with post => category" do
273
- Post.in_category_name('first').references(:categories).each do |post|
274
- post.category.name
275
- end
276
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
277
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
278
-
279
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
280
- end
281
-
282
- it "should not be unused preload post => category" do
283
- Post.in_category_name('first').references(:categories).map(&:name)
284
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
285
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
286
-
287
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
288
- end
289
- end
290
-
291
- context "scope preload_comments" do
292
- it "should detect preload post => comments with scope" do
293
- Post.preload_comments.each do |post|
294
- post.comments.map(&:name)
295
- end
296
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
297
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
298
-
299
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
300
- end
301
-
302
- it "should detect unused preload with scope" do
303
- Post.preload_comments.map(&:name)
304
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
305
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
306
-
307
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
308
- end
309
- end
310
- end
311
-
312
- describe Bullet::Detector::Association, 'belongs_to' do
313
- context "comment => post" do
314
- it "should detect non preload with comment => post" do
315
- Comment.all.each do |comment|
316
- comment.post.name
317
- end
318
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
319
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
320
-
321
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Comment, :post)
322
- end
323
-
324
- it "should detect preload with one comment => post" do
325
- Comment.first.post.name
326
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
327
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
328
-
329
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
330
- end
331
-
332
- it "should dtect preload with comment => post" do
333
- Comment.includes(:post).each do |comment|
334
- comment.post.name
335
- end
336
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
337
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
338
-
339
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
340
- end
341
-
342
- it "should not detect preload with comment => post" do
343
- Comment.all.map(&:name)
344
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
345
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
346
-
347
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
348
- end
349
-
350
- it "should detect unused preload with comment => post" do
351
- Comment.includes(:post).map(&:name)
352
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
353
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Comment, :post)
354
-
355
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
356
- end
357
- end
358
-
359
- context "comment => post => category" do
360
- it "should detect non preload association with comment => post" do
361
- Comment.all.each do |comment|
362
- comment.post.category.name
363
- end
364
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
365
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
366
-
367
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Comment, :post)
368
- end
369
-
370
- it "should not detect non preload association with only one comment" do
371
- Comment.first.post.category.name
372
-
373
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
374
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
375
-
376
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
377
- end
378
-
379
- it "should detect non preload association with post => category" do
380
- Comment.includes(:post).each do |comment|
381
- comment.post.category.name
382
- end
383
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
384
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
385
-
386
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :category)
387
- end
388
-
389
- it "should not detect unpreload association" do
390
- Comment.includes(:post => :category).each do |comment|
391
- comment.post.category.name
392
- end
393
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
394
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
395
-
396
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
397
- end
398
- end
399
-
400
- context "comment => author, post => writer" do
401
- it "should detect non preloaded writer" do
402
- Comment.includes([:author, :post]).where(["base_users.id = ?", BaseUser.first]).references(:base_users).each do |comment|
403
- comment.post.writer.name
404
- end
405
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
406
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
407
-
408
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :writer)
409
- end
410
-
411
- it "should detect unused preload with comment => author" do
412
- Comment.includes([:author, {:post => :writer}]).where(["base_users.id = ?", BaseUser.first]).references(:base_users).each do |comment|
413
- comment.post.writer.name
414
- end
415
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
416
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
417
-
418
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
419
- end
420
-
421
- it "should detect non preloading with writer => newspaper" do
422
- Comment.includes(:post => :writer).where("posts.name like '%first%'").references(:posts).each do |comment|
423
- comment.post.writer.newspaper.name
424
- end
425
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
426
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
427
-
428
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Writer, :newspaper)
429
- end
430
-
431
- it "should not raise a stack error from posts to category" do
432
- expect {
433
- Comment.includes({:post => :category}).each do |com|
434
- com.post.category
435
- end
436
- }.not_to raise_error()
437
- end
438
- end
439
- end
440
-
441
- describe Bullet::Detector::Association, 'has_and_belongs_to_many' do
442
- context "students <=> teachers" do
443
- it "should detect non preload associations" do
444
- Student.all.each do |student|
445
- student.teachers.map(&:name)
446
- end
447
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
448
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
449
-
450
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Student, :teachers)
451
- end
452
-
453
- it "should detect preload associations" do
454
- Student.includes(:teachers).each do |student|
455
- student.teachers.map(&:name)
456
- end
457
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
458
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
459
-
460
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
461
- end
462
-
463
- it "should detect unused preload associations" do
464
- Student.includes(:teachers).map(&:name)
465
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
466
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Student, :teachers)
467
-
468
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
469
- end
470
-
471
- it "should detect no unused preload associations" do
472
- Student.all.map(&:name)
473
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
474
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
475
-
476
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
477
- end
478
-
479
- it "should detect non preload student => teachers with empty?" do
480
- Student.all.each do |student|
481
- student.teachers.empty?
482
- end
483
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
484
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
485
-
486
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Student, :teachers)
487
- end
488
- end
489
- end
490
-
491
- describe Bullet::Detector::Association, 'has_many :through' do
492
- context "firm => clients" do
493
- it "should detect non preload associations" do
494
- Firm.all.each do |firm|
495
- firm.clients.map(&:name)
496
- end
497
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
498
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
499
-
500
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Firm, :clients)
501
- end
502
-
503
- it "should detect preload associations" do
504
- Firm.includes(:clients).each do |firm|
505
- firm.clients.map(&:name)
506
- end
507
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
508
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
509
-
510
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
511
- end
512
-
513
- it "should not detect preload associations" do
514
- Firm.all.map(&:name)
515
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
516
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
517
-
518
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
519
- end
520
-
521
- it "should detect unused preload associations" do
522
- Firm.includes(:clients).map(&:name)
523
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
524
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Firm, :clients)
525
-
526
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
527
- end
528
- end
529
- end
530
-
531
- describe Bullet::Detector::Association, "has_one" do
532
- context "company => address" do
533
- it "should detect non preload association" do
534
- Company.all.each do |company|
535
- company.address.name
536
- end
537
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
538
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
539
-
540
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Company, :address)
541
- end
542
-
543
- it "should detect preload association" do
544
- Company.includes(:address).each do |company|
545
- company.address.name
546
- end
547
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
548
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
549
-
550
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
551
- end
552
-
553
- it "should not detect preload association" do
554
- Company.all.map(&:name)
555
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
556
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
557
-
558
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
559
- end
560
-
561
- it "should detect unused preload association" do
562
- Company.includes(:address).map(&:name)
563
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
564
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Company, :address)
565
-
566
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
567
- end
568
- end
569
- end
570
-
571
- describe Bullet::Detector::Association, "has_one => has_many" do
572
- it "should not detect preload association" do
573
- user = User.first
574
- user.submission.replies.map(&:name)
575
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
576
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
577
-
578
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
579
- end
580
- end
581
-
582
- describe Bullet::Detector::Association, "call one association that in possible objects" do
583
- it "should not detect preload association" do
584
- Post.all
585
- Post.first.comments.map(&:name)
586
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
587
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
588
-
589
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
590
- end
591
- end
592
-
593
- describe Bullet::Detector::Association, "query immediately after creation" do
594
- context "with save" do
595
- context "document => children" do
596
- it 'should not detect non preload associations' do
597
- document1 = Document.new
598
- document1.children.build
599
- document1.save
600
-
601
- document2 = Document.new(parent: document1)
602
- document2.save
603
- document2.parent
604
-
605
- document1.children.each.first
606
-
607
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
608
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
609
-
610
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
611
- end
612
- end
613
- end
614
-
615
- context "with save!" do
616
- context "document => children" do
617
- it 'should not detect non preload associations' do
618
- document1 = Document.new
619
- document1.children.build
620
- document1.save!
621
-
622
- document2 = Document.new(parent: document1)
623
- document2.save!
624
- document2.parent
625
-
626
- document1.children.each.first
627
-
628
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
629
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
630
-
631
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
632
- end
633
- end
634
- end
635
- end
636
-
637
- describe Bullet::Detector::Association, "STI" do
638
- context "page => author" do
639
- it "should detect non preload associations" do
640
- Page.all.each do |page|
641
- page.author.name
642
- end
643
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
644
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
645
-
646
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Page, :author)
647
- end
648
-
649
- it "should detect preload associations" do
650
- Page.includes(:author).each do |page|
651
- page.author.name
652
- end
653
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
654
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
655
-
656
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
657
- end
658
-
659
- it "should detect unused preload associations" do
660
- Page.includes(:author).map(&:name)
661
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
662
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Page, :author)
663
-
664
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
665
- end
666
-
667
- it "should not detect preload associations" do
668
- Page.all.map(&:name)
669
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
670
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
671
-
672
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
673
- end
674
- end
675
-
676
- context "disable n plus one query" do
677
- before { Bullet.n_plus_one_query_enable = false }
678
- after { Bullet.n_plus_one_query_enable = true }
679
-
680
- it "should not detect n plus one query" do
681
- Post.all.each do |post|
682
- post.comments.map(&:name)
683
- end
684
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
685
-
686
- expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Post, :comments)
687
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
688
- end
689
-
690
- it "should still detect unused eager loading" do
691
- Post.includes(:comments).map(&:name)
692
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
693
-
694
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
695
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
696
- end
697
- end
698
-
699
- context "disable unused eager loading" do
700
- before { Bullet.unused_eager_loading_enable = false }
701
- after { Bullet.unused_eager_loading_enable = true }
702
-
703
- it "should not detect unused eager loading" do
704
- Post.includes(:comments).map(&:name)
705
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
706
-
707
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
708
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
709
- end
710
-
711
- it "should still detect n plus one query" do
712
- Post.all.each do |post|
713
- post.comments.map(&:name)
714
- end
715
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
716
-
717
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
718
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
719
- end
720
- end
721
-
722
- context "whitelist n plus one query" do
723
- before { Bullet.add_whitelist :type => :n_plus_one_query, :class_name => "Post", :association => :comments }
724
- after { Bullet.clear_whitelist }
725
-
726
- it "should not detect n plus one query" do
727
- Post.all.each do |post|
728
- post.comments.map(&:name)
729
- end
730
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
731
-
732
- expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Post, :comments)
733
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
734
- end
735
-
736
- it "should still detect unused eager loading" do
737
- Post.includes(:comments).map(&:name)
738
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
739
-
740
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
741
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
742
- end
743
- end
744
-
745
- context "whitelist unused eager loading" do
746
- before { Bullet.add_whitelist :type => :unused_eager_loading, :class_name => "Post", :association => :comments }
747
- after { Bullet.clear_whitelist }
748
-
749
- it "should not detect unused eager loading" do
750
- Post.includes(:comments).map(&:name)
751
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
752
-
753
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
754
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
755
- end
756
-
757
- it "should still detect n plus one query" do
758
- Post.all.each do |post|
759
- post.comments.map(&:name)
760
- end
761
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
762
-
763
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
764
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
765
- end
766
- end
767
- end
768
- end