bullet 4.13.1 → 4.14.10

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 (60) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +29 -1
  3. data/CHANGELOG.md +53 -2
  4. data/Gemfile.mongoid +0 -2
  5. data/Gemfile.mongoid-2.4 +2 -4
  6. data/Gemfile.mongoid-2.5 +2 -4
  7. data/Gemfile.mongoid-2.6 +1 -3
  8. data/Gemfile.mongoid-2.7 +2 -4
  9. data/Gemfile.mongoid-2.8 +2 -4
  10. data/Gemfile.mongoid-3.0 +2 -4
  11. data/Gemfile.mongoid-3.1 +2 -4
  12. data/Gemfile.mongoid-4.0 +1 -3
  13. data/Gemfile.mongoid-5.0 +17 -0
  14. data/Gemfile.rails-3.0 +1 -3
  15. data/Gemfile.rails-3.1 +1 -3
  16. data/Gemfile.rails-3.2 +1 -3
  17. data/Gemfile.rails-4.0 +1 -3
  18. data/Gemfile.rails-4.1 +1 -3
  19. data/Gemfile.rails-4.2 +17 -0
  20. data/README.md +29 -2
  21. data/bullet.gemspec +1 -1
  22. data/lib/bullet/active_record3.rb +65 -35
  23. data/lib/bullet/active_record3x.rb +54 -32
  24. data/lib/bullet/active_record4.rb +62 -30
  25. data/lib/bullet/active_record41.rb +63 -32
  26. data/lib/bullet/active_record42.rb +214 -0
  27. data/lib/bullet/dependency.rb +12 -0
  28. data/lib/bullet/detector/association.rb +16 -16
  29. data/lib/bullet/detector/counter_cache.rb +13 -13
  30. data/lib/bullet/detector/n_plus_one_query.rb +30 -27
  31. data/lib/bullet/detector/unused_eager_loading.rb +2 -2
  32. data/lib/bullet/ext/object.rb +4 -2
  33. data/lib/bullet/mongoid5x.rb +56 -0
  34. data/lib/bullet/notification/base.rb +7 -11
  35. data/lib/bullet/notification/n_plus_one_query.rb +6 -4
  36. data/lib/bullet/rack.rb +20 -13
  37. data/lib/bullet/version.rb +1 -1
  38. data/lib/bullet.rb +24 -11
  39. data/spec/bullet/detector/counter_cache_spec.rb +8 -8
  40. data/spec/bullet/detector/n_plus_one_query_spec.rb +27 -27
  41. data/spec/bullet/ext/object_spec.rb +6 -0
  42. data/spec/bullet/notification/base_spec.rb +4 -29
  43. data/spec/bullet/notification/n_plus_one_query_spec.rb +3 -3
  44. data/spec/bullet/notification/unused_eager_loading_spec.rb +1 -1
  45. data/spec/bullet/rack_spec.rb +3 -3
  46. data/spec/bullet_spec.rb +47 -0
  47. data/spec/integration/active_record3/association_spec.rb +11 -2
  48. data/spec/integration/active_record4/association_spec.rb +58 -3
  49. data/spec/integration/counter_cache_spec.rb +8 -1
  50. data/spec/models/category.rb +4 -1
  51. data/spec/models/comment.rb +2 -0
  52. data/spec/models/post.rb +7 -2
  53. data/spec/models/reply.rb +3 -0
  54. data/spec/models/submission.rb +1 -1
  55. data/spec/spec_helper.rb +3 -2
  56. data/spec/support/mongo_seed.rb +13 -0
  57. data/spec/support/sqlite_seed.rb +14 -6
  58. data/test.sh +2 -0
  59. data/update.sh +15 -0
  60. metadata +14 -7
@@ -219,6 +219,20 @@ if !mongoid? && active_record4?
219
219
 
220
220
  expect(Bullet::Detector::Association).to be_completely_preloading_associations
221
221
  end
222
+
223
+ it "should not detect unused preload with category => posts" do
224
+ category = Category.first
225
+ category.draft_post.destroy!
226
+ post = category.draft_post
227
+ post.update_attributes!(link: true)
228
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
229
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
230
+
231
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
232
+
233
+ Support::SqliteSeed.setup_db
234
+ Support::SqliteSeed.seed_db
235
+ end
222
236
  end
223
237
 
224
238
  context "category => posts => writer" do
@@ -313,7 +327,7 @@ if !mongoid? && active_record4?
313
327
  expect(Bullet::Detector::Association).to be_completely_preloading_associations
314
328
  end
315
329
 
316
- it "should detect unused preload with comments => post" do
330
+ it "should detect unused preload with comment => post" do
317
331
  Comment.includes(:post).map(&:name)
318
332
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
319
333
  expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Comment, :post)
@@ -333,6 +347,15 @@ if !mongoid? && active_record4?
333
347
  expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Comment, :post)
334
348
  end
335
349
 
350
+ it "should not detect non preload association with only one comment" do
351
+ Comment.first.post.category.name
352
+
353
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
354
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
355
+
356
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
357
+ end
358
+
336
359
  it "should detect non preload association with post => category" do
337
360
  Comment.includes(:post).each do |comment|
338
361
  comment.post.category.name
@@ -515,6 +538,17 @@ if !mongoid? && active_record4?
515
538
  end
516
539
  end
517
540
 
541
+ describe Bullet::Detector::Association, "has_one => has_many" do
542
+ it "should not detect preload association" do
543
+ user = User.first
544
+ user.submission.replies.map(&:name)
545
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
546
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
547
+
548
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
549
+ end
550
+ end
551
+
518
552
  describe Bullet::Detector::Association, "call one association that in possible objects" do
519
553
  it "should not detect preload association" do
520
554
  Post.all
@@ -526,6 +560,27 @@ if !mongoid? && active_record4?
526
560
  end
527
561
  end
528
562
 
563
+ describe Bullet::Detector::Association, "query immediately after creation" do
564
+ context "document => children" do
565
+ it 'should not detect non preload associations' do
566
+ document1 = Document.new
567
+ document1.children.build
568
+ document1.save
569
+
570
+ document2 = Document.new(parent: document1)
571
+ document2.save
572
+ document2.parent
573
+
574
+ document1.children.each.first
575
+
576
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
577
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
578
+
579
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
580
+ end
581
+ end
582
+ end
583
+
529
584
  describe Bullet::Detector::Association, "STI" do
530
585
  context "page => author" do
531
586
  it "should detect non preload associations" do
@@ -613,7 +668,7 @@ if !mongoid? && active_record4?
613
668
 
614
669
  context "whitelist n plus one query" do
615
670
  before { Bullet.add_whitelist :type => :n_plus_one_query, :class_name => "Post", :association => :comments }
616
- after { Bullet.reset_whitelist }
671
+ after { Bullet.clear_whitelist }
617
672
 
618
673
  it "should not detect n plus one query" do
619
674
  Post.all.each do |post|
@@ -636,7 +691,7 @@ if !mongoid? && active_record4?
636
691
 
637
692
  context "whitelist unused eager loading" do
638
693
  before { Bullet.add_whitelist :type => :unused_eager_loading, :class_name => "Post", :association => :comments }
639
- after { Bullet.reset_whitelist }
694
+ after { Bullet.clear_whitelist }
640
695
 
641
696
  it "should not detect unused eager loading" do
642
697
  Post.includes(:comments).map(&:name)
@@ -29,6 +29,13 @@ if !mongoid? && active_record?
29
29
  expect(Bullet.collected_counter_cache_notifications).to be_empty
30
30
  end
31
31
 
32
+ it "should need counter cache for has_many through" do
33
+ Client.all.each do |client|
34
+ client.firms.size
35
+ end
36
+ expect(Bullet.collected_counter_cache_notifications).not_to be_empty
37
+ end
38
+
32
39
  it "should not need counter cache with part of cities" do
33
40
  Country.all.each do |country|
34
41
  country.cities.where(:name => 'first').size
@@ -50,7 +57,7 @@ if !mongoid? && active_record?
50
57
 
51
58
  context "whitelist" do
52
59
  before { Bullet.add_whitelist :type => :counter_cache, :class_name => "Country", :association => :cities }
53
- after { Bullet.reset_whitelist }
60
+ after { Bullet.clear_whitelist }
54
61
 
55
62
  it "should not detect counter cache" do
56
63
  Country.all.each do |country|
@@ -2,6 +2,9 @@ class Category < ActiveRecord::Base
2
2
  has_many :posts, inverse_of: :category
3
3
  has_many :entries
4
4
 
5
- has_many :submissions
6
5
  has_many :users
6
+
7
+ def draft_post
8
+ posts.draft.first_or_create
9
+ end
7
10
  end
@@ -1,4 +1,6 @@
1
1
  class Comment < ActiveRecord::Base
2
2
  belongs_to :post, inverse_of: :comments
3
3
  belongs_to :author, class_name: "BaseUser"
4
+
5
+ validates :post, presence: true
4
6
  end
data/spec/models/post.rb CHANGED
@@ -1,10 +1,15 @@
1
1
  class Post < ActiveRecord::Base
2
- extend Bullet::Dependency
3
-
4
2
  belongs_to :category, inverse_of: :posts
5
3
  belongs_to :writer
6
4
  has_many :comments, inverse_of: :post
7
5
 
6
+ validates :category, presence: true
7
+
8
8
  scope :preload_comments, -> { includes(:comments) }
9
9
  scope :in_category_name, ->(name) { where(['categories.name = ?', name]).includes(:category) }
10
+ scope :draft, -> { where(active: false) }
11
+
12
+ def link=(*)
13
+ comments.new
14
+ end
10
15
  end
@@ -0,0 +1,3 @@
1
+ class Reply < ActiveRecord::Base
2
+ belongs_to :submission
3
+ end
@@ -1,4 +1,4 @@
1
1
  class Submission < ActiveRecord::Base
2
- belongs_to :category
3
2
  belongs_to :user
3
+ has_many :replies
4
4
  end
data/spec/spec_helper.rb CHANGED
@@ -56,11 +56,12 @@ if active_record?
56
56
  Support::SqliteSeed.seed_db
57
57
  end
58
58
 
59
- config.before(:each) do
59
+ config.before(:example) do
60
60
  Bullet.start_request
61
+ Bullet.enable = true
61
62
  end
62
63
 
63
- config.after(:each) do
64
+ config.after(:example) do
64
65
  Bullet.end_request
65
66
  end
66
67
  end
@@ -52,6 +52,19 @@ module Support
52
52
  }
53
53
  )
54
54
  end
55
+ elsif Mongoid::VERSION =~ /\A5/
56
+ Mongoid.configure do |config|
57
+ config.load_configuration(
58
+ clients: {
59
+ default: {
60
+ database: "bullet",
61
+ hosts: [ "localhost:27017" ]
62
+ }
63
+ }
64
+ )
65
+ end
66
+ # Increase the level from DEBUG in order to avoid excessive logging to the screen
67
+ Mongo::Logger.logger.level = Logger::WARN
55
68
  end
56
69
  end
57
70
 
@@ -14,7 +14,7 @@ module Support
14
14
  category2 = Category.create(:name => 'second')
15
15
 
16
16
  post1 = category1.posts.create(:name => 'first', :writer => writer1)
17
- post1a = category1.posts.create(:name => 'like first', :writer => writer2)
17
+ post1a = category1.posts.create(:name => 'like first', :writer => writer2, active: false)
18
18
  post2 = category2.posts.create(:name => 'second', :writer => writer2)
19
19
 
20
20
  comment1 = post1.comments.create(:name => 'first', :author => writer1)
@@ -82,10 +82,13 @@ module Support
82
82
  user1 = User.create(:name => 'user1', :category => category1)
83
83
  user2 = User.create(:name => 'user2', :category => category1)
84
84
 
85
- submission1 = category1.submissions.create(:name => "submission1", :user => user1)
86
- submission2 = category1.submissions.create(:name => "submission2", :user => user2)
87
- submission3 = category2.submissions.create(:name => "submission3", :user => user1)
88
- submission4 = category2.submissions.create(:name => "submission4", :user => user2)
85
+ submission1 = user1.create_submission(:name => "submission1")
86
+ submission2 = user2.create_submission(:name => "submission2")
87
+
88
+ submission1.replies.create(:name => 'reply1')
89
+ submission1.replies.create(:name => 'reply2')
90
+ submission2.replies.create(:name => 'reply3')
91
+ submission2.replies.create(:name => 'reply4')
89
92
  end
90
93
 
91
94
  def setup_db
@@ -191,6 +194,7 @@ module Support
191
194
  t.column :name, :string
192
195
  t.column :category_id, :integer
193
196
  t.column :writer_id, :integer
197
+ t.column :active, :boolean, :default => true
194
198
  end
195
199
 
196
200
  create_table :relationships do |t|
@@ -211,9 +215,13 @@ module Support
211
215
  t.column :name, :string
212
216
  end
213
217
 
218
+ create_table :replies do |t|
219
+ t.column :name, :string
220
+ t.column :submission_id, :integer
221
+ end
222
+
214
223
  create_table :submissions do |t|
215
224
  t.column :name, :string
216
- t.column :category_id, :integer
217
225
  t.column :user_id, :integer
218
226
  end
219
227
 
data/test.sh CHANGED
@@ -1,10 +1,12 @@
1
1
  #bundle update rails && bundle exec rspec spec
2
2
  #BUNDLE_GEMFILE=Gemfile.mongoid bundle update mongoid && BUNDLE_GEMFILE=Gemfile.mongoid bundle exec rspec spec
3
+ BUNDLE_GEMFILE=Gemfile.rails-4.2 bundle && BUNDLE_GEMFILE=Gemfile.rails-4.2 bundle exec rspec spec
3
4
  BUNDLE_GEMFILE=Gemfile.rails-4.1 bundle && BUNDLE_GEMFILE=Gemfile.rails-4.1 bundle exec rspec spec
4
5
  BUNDLE_GEMFILE=Gemfile.rails-4.0 bundle && BUNDLE_GEMFILE=Gemfile.rails-4.0 bundle exec rspec spec
5
6
  BUNDLE_GEMFILE=Gemfile.rails-3.2 bundle && BUNDLE_GEMFILE=Gemfile.rails-3.2 bundle exec rspec spec
6
7
  BUNDLE_GEMFILE=Gemfile.rails-3.1 bundle && BUNDLE_GEMFILE=Gemfile.rails-3.1 bundle exec rspec spec
7
8
  BUNDLE_GEMFILE=Gemfile.rails-3.0 bundle && BUNDLE_GEMFILE=Gemfile.rails-3.0 bundle exec rspec spec
9
+ BUNDLE_GEMFILE=Gemfile.mongoid-5.0 bundle && BUNDLE_GEMFILE=Gemfile.mongoid-5.0 bundle exec rspec spec
8
10
  BUNDLE_GEMFILE=Gemfile.mongoid-4.0 bundle && BUNDLE_GEMFILE=Gemfile.mongoid-4.0 bundle exec rspec spec
9
11
  BUNDLE_GEMFILE=Gemfile.mongoid-3.1 bundle && BUNDLE_GEMFILE=Gemfile.mongoid-3.1 bundle exec rspec spec
10
12
  BUNDLE_GEMFILE=Gemfile.mongoid-3.0 bundle && BUNDLE_GEMFILE=Gemfile.mongoid-3.0 bundle exec rspec spec
data/update.sh ADDED
@@ -0,0 +1,15 @@
1
+ BUNDLE_GEMFILE=Gemfile.rails-4.2 bundle update
2
+ BUNDLE_GEMFILE=Gemfile.rails-4.1 bundle update
3
+ BUNDLE_GEMFILE=Gemfile.rails-4.0 bundle update
4
+ BUNDLE_GEMFILE=Gemfile.rails-3.2 bundle update
5
+ BUNDLE_GEMFILE=Gemfile.rails-3.1 bundle update
6
+ BUNDLE_GEMFILE=Gemfile.rails-3.0 bundle update
7
+ BUNDLE_GEMFILE=Gemfile.mongoid-5.0 bundle update
8
+ BUNDLE_GEMFILE=Gemfile.mongoid-4.0 bundle update
9
+ BUNDLE_GEMFILE=Gemfile.mongoid-3.1 bundle update
10
+ BUNDLE_GEMFILE=Gemfile.mongoid-3.0 bundle update
11
+ BUNDLE_GEMFILE=Gemfile.mongoid-2.8 bundle update
12
+ BUNDLE_GEMFILE=Gemfile.mongoid-2.7 bundle update
13
+ BUNDLE_GEMFILE=Gemfile.mongoid-2.6 bundle update
14
+ BUNDLE_GEMFILE=Gemfile.mongoid-2.5 bundle update
15
+ BUNDLE_GEMFILE=Gemfile.mongoid-2.4 bundle update
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: 4.13.1
4
+ version: 4.14.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-03 00:00:00.000000000 Z
11
+ date: 2015-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: uniform_notifier
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 1.6.0
33
+ version: 1.9.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 1.6.0
40
+ version: 1.9.0
41
41
  description: help to kill N+1 queries and unused eager loading.
42
42
  email:
43
43
  - flyerhzm@gmail.com
@@ -59,11 +59,13 @@ files:
59
59
  - Gemfile.mongoid-3.0
60
60
  - Gemfile.mongoid-3.1
61
61
  - Gemfile.mongoid-4.0
62
+ - Gemfile.mongoid-5.0
62
63
  - Gemfile.rails-3.0
63
64
  - Gemfile.rails-3.1
64
65
  - Gemfile.rails-3.2
65
66
  - Gemfile.rails-4.0
66
67
  - Gemfile.rails-4.1
68
+ - Gemfile.rails-4.2
67
69
  - Guardfile
68
70
  - Hacking.md
69
71
  - MIT-LICENSE
@@ -75,6 +77,7 @@ files:
75
77
  - lib/bullet/active_record3x.rb
76
78
  - lib/bullet/active_record4.rb
77
79
  - lib/bullet/active_record41.rb
80
+ - lib/bullet/active_record42.rb
78
81
  - lib/bullet/dependency.rb
79
82
  - lib/bullet/detector.rb
80
83
  - lib/bullet/detector/association.rb
@@ -87,6 +90,7 @@ files:
87
90
  - lib/bullet/mongoid2x.rb
88
91
  - lib/bullet/mongoid3x.rb
89
92
  - lib/bullet/mongoid4x.rb
93
+ - lib/bullet/mongoid5x.rb
90
94
  - lib/bullet/notification.rb
91
95
  - lib/bullet/notification/base.rb
92
96
  - lib/bullet/notification/counter_cache.rb
@@ -148,6 +152,7 @@ files:
148
152
  - spec/models/pet.rb
149
153
  - spec/models/post.rb
150
154
  - spec/models/relationship.rb
155
+ - spec/models/reply.rb
151
156
  - spec/models/student.rb
152
157
  - spec/models/submission.rb
153
158
  - spec/models/teacher.rb
@@ -160,6 +165,7 @@ files:
160
165
  - spec/support/sqlite_seed.rb
161
166
  - tasks/bullet_tasks.rake
162
167
  - test.sh
168
+ - update.sh
163
169
  homepage: http://github.com/flyerhzm/bullet
164
170
  licenses:
165
171
  - MIT
@@ -180,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
180
186
  version: 1.3.6
181
187
  requirements: []
182
188
  rubyforge_project:
183
- rubygems_version: 2.2.2
189
+ rubygems_version: 2.4.6
184
190
  signing_key:
185
191
  specification_version: 4
186
192
  summary: help to kill N+1 queries and unused eager loading.
@@ -232,6 +238,7 @@ test_files:
232
238
  - spec/models/pet.rb
233
239
  - spec/models/post.rb
234
240
  - spec/models/relationship.rb
241
+ - spec/models/reply.rb
235
242
  - spec/models/student.rb
236
243
  - spec/models/submission.rb
237
244
  - spec/models/teacher.rb