bullet 5.1.0 → 5.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f726582290756f07599b35204f8e903fd20350b3
4
- data.tar.gz: 2e4a9e95087d246cb69e887eef07868c2d9e0a63
3
+ metadata.gz: dbcef4cb1e9b0db722674cc600a456767eb5d2dd
4
+ data.tar.gz: 27107041da317defb0433329dc55375b305cc7b8
5
5
  SHA512:
6
- metadata.gz: 66c4b18f8c522a2cfa310bd8b818b177a12507c99255860881d225bac39722f35bfea65d7ea14287496f7fcf4d52d618e77ba712ebb28b2dad7a7a08909f56a8
7
- data.tar.gz: f68a5397786c67ae3aefe57ce3030133f4c0f54a253add872dc54ba83ab00c33439325b6f0ee3500a0534bddca951e95b5929a8dfc0c74d8bf5c818b204992ad
6
+ metadata.gz: ea8208a3a4f1a1ef3f4f247667f9485b951a00ba5036cc87b8049005813e8f7452a5d340c1fdb09ea525fd5996b0050587a758afc8ff2a421f93acb21424f605
7
+ data.tar.gz: 456466364914301c30c4622bba947888bd16d92dad0603759c8b1108c4bc11ed417a8fca5a8ee2c983f119fa16e587cc140498393d2ec31f1a79c410082f1874
data/.travis.yml CHANGED
@@ -5,7 +5,7 @@ rvm:
5
5
  - 2.0
6
6
  - 2.1
7
7
  - 2.2
8
- - 2.2.3
8
+ - 2.3.0
9
9
  gemfile:
10
10
  - Gemfile.rails-5.0
11
11
  - Gemfile.rails-4.2
@@ -63,14 +63,14 @@ matrix:
63
63
  gemfile: Gemfile.rails-3.0
64
64
  - rvm: 2.1
65
65
  gemfile: Gemfile.rails-5.0
66
- - rvm: 2.2
67
- gemfile: Gemfile.rails-5.0
68
66
  - rvm: 2.2
69
67
  gemfile: Gemfile.rails-3.0
70
68
  - rvm: 2.2
71
69
  gemfile: Gemfile.rails-3.1
72
70
  - rvm: 2.2
73
71
  gemfile: Gemfile.rails-3.2
72
+ - rvm: 2.2
73
+ gemfile: Gemfile.rails-5.0
74
74
  - rvm: 2.2
75
75
  gemfile: Gemfile.mongoid-3.1
76
76
  - rvm: 2.2
@@ -85,23 +85,23 @@ matrix:
85
85
  gemfile: Gemfile.mongoid-2.5
86
86
  - rvm: 2.2
87
87
  gemfile: Gemfile.mongoid-2.4
88
- - rvm: 2.2.3
88
+ - rvm: 2.3.0
89
89
  gemfile: Gemfile.rails-3.0
90
- - rvm: 2.2.3
90
+ - rvm: 2.3.0
91
91
  gemfile: Gemfile.rails-3.1
92
- - rvm: 2.2.3
92
+ - rvm: 2.3.0
93
93
  gemfile: Gemfile.rails-3.2
94
- - rvm: 2.2.3
94
+ - rvm: 2.3.0
95
95
  gemfile: Gemfile.mongoid-3.1
96
- - rvm: 2.2.3
96
+ - rvm: 2.3.0
97
97
  gemfile: Gemfile.mongoid-3.0
98
- - rvm: 2.2.3
98
+ - rvm: 2.3.0
99
99
  gemfile: Gemfile.mongoid-2.8
100
- - rvm: 2.2.3
100
+ - rvm: 2.3.0
101
101
  gemfile: Gemfile.mongoid-2.7
102
- - rvm: 2.2.3
102
+ - rvm: 2.3.0
103
103
  gemfile: Gemfile.mongoid-2.6
104
- - rvm: 2.2.3
104
+ - rvm: 2.3.0
105
105
  gemfile: Gemfile.mongoid-2.5
106
- - rvm: 2.2.3
106
+ - rvm: 2.3.0
107
107
  gemfile: Gemfile.mongoid-2.4
data/CHANGELOG.md CHANGED
@@ -1,6 +1,12 @@
1
1
  # Next Release
2
2
 
3
- ## 5.1.0 (05/21/2015)
3
+ ## 5.2.0 (07/26/2016)
4
+
5
+ * Fix `has_cached_counter?` is not defined in HABTM #297
6
+ * Fix false alert if preloaded association has no records #260
7
+ * Support Rails 5.0.0
8
+
9
+ ## 5.1.0 (05/21/2016)
4
10
 
5
11
  * Fix false alert when `empty?` used with `counter_cache`
6
12
  * Fix `alias_method_chain` deprecation for rails 5
@@ -8,7 +14,7 @@
8
14
  * Fix false alert when querying immediately after creation
9
15
  * Fix UnusedEagerLoading bug when multiple eager loading query include same objects
10
16
 
11
- ## 5.0.0 (01/06/2015)
17
+ ## 5.0.0 (01/06/2016)
12
18
 
13
19
  * Support Rails 5.0.0.beta1
14
20
  * Fix `has_many :through` infinite loop issue
data/Gemfile.mongoid-2.4 CHANGED
@@ -9,8 +9,6 @@ gem 'mongoid', '~> 2.4.0'
9
9
 
10
10
  gem "rspec"
11
11
 
12
- gem 'coveralls', require: false
13
-
14
12
  platforms :rbx do
15
13
  gem 'rubysl', '~> 2.0'
16
14
  gem 'rubinius-developer_tools'
data/Gemfile.mongoid-2.5 CHANGED
@@ -9,8 +9,6 @@ gem 'mongoid', '~> 2.5.0'
9
9
 
10
10
  gem "rspec"
11
11
 
12
- gem 'coveralls', require: false
13
-
14
12
  platforms :rbx do
15
13
  gem 'rubysl', '~> 2.0'
16
14
  gem 'rubinius-developer_tools'
data/Gemfile.mongoid-2.6 CHANGED
@@ -9,8 +9,6 @@ gem 'mongoid', '~> 2.6.0'
9
9
 
10
10
  gem "rspec"
11
11
 
12
- gem 'coveralls', require: false
13
-
14
12
  platforms :rbx do
15
13
  gem 'rubysl', '~> 2.0'
16
14
  gem 'rubinius-developer_tools'
data/Gemfile.mongoid-2.7 CHANGED
@@ -9,8 +9,6 @@ gem 'mongoid', '~> 2.7.0'
9
9
 
10
10
  gem "rspec"
11
11
 
12
- gem 'coveralls', require: false
13
-
14
12
  platforms :rbx do
15
13
  gem 'rubysl', '~> 2.0'
16
14
  gem 'rubinius-developer_tools'
data/Gemfile.mongoid-2.8 CHANGED
@@ -9,8 +9,6 @@ gem 'mongoid', '~> 2.8.0'
9
9
 
10
10
  gem "rspec"
11
11
 
12
- gem 'coveralls', require: false
13
-
14
12
  platforms :rbx do
15
13
  gem 'rubysl', '~> 2.0'
16
14
  gem 'rubinius-developer_tools'
data/Gemfile.mongoid-3.0 CHANGED
@@ -9,8 +9,6 @@ gem 'mongoid', '~> 3.0.0'
9
9
 
10
10
  gem "rspec"
11
11
 
12
- gem 'coveralls', require: false
13
-
14
12
  platforms :rbx do
15
13
  gem 'rubysl', '~> 2.0'
16
14
  gem 'rubinius-developer_tools'
data/Gemfile.mongoid-3.1 CHANGED
@@ -9,8 +9,6 @@ gem 'mongoid', '~> 3.1.0'
9
9
 
10
10
  gem "rspec"
11
11
 
12
- gem 'coveralls', require: false
13
-
14
12
  platforms :rbx do
15
13
  gem 'rubysl', '~> 2.0'
16
14
  gem 'rubinius-developer_tools'
data/Gemfile.mongoid-4.0 CHANGED
@@ -9,8 +9,6 @@ gem 'mongoid', '~> 4.0.0'
9
9
 
10
10
  gem "rspec"
11
11
 
12
- gem 'coveralls', require: false
13
-
14
12
  platforms :rbx do
15
13
  gem 'rubysl', '~> 2.0'
16
14
  gem 'rubinius-developer_tools'
data/Gemfile.mongoid-5.0 CHANGED
@@ -9,8 +9,6 @@ gem 'mongoid', '~> 5.0.0'
9
9
 
10
10
  gem "rspec"
11
11
 
12
- gem 'coveralls', require: false
13
-
14
12
  platforms :rbx do
15
13
  gem 'rubysl', '~> 2.0'
16
14
  gem 'rubinius-developer_tools'
data/Gemfile.rails-3.0 CHANGED
@@ -10,8 +10,6 @@ gem 'tins', '~> 1.6.0', platforms: [:ruby_19]
10
10
 
11
11
  gem "rspec"
12
12
 
13
- gem 'coveralls', require: false
14
-
15
13
  platforms :rbx do
16
14
  gem 'rubysl', '~> 2.0'
17
15
  gem 'rubinius-developer_tools'
data/Gemfile.rails-3.1 CHANGED
@@ -10,8 +10,6 @@ gem 'tins', '~> 1.6.0', platforms: [:ruby_19]
10
10
 
11
11
  gem "rspec"
12
12
 
13
- gem 'coveralls', require: false
14
-
15
13
  platforms :rbx do
16
14
  gem 'rubysl', '~> 2.0'
17
15
  gem 'rubinius-developer_tools'
data/Gemfile.rails-3.2 CHANGED
@@ -10,8 +10,6 @@ gem 'tins', '~> 1.6.0', platforms: [:ruby_19]
10
10
 
11
11
  gem "rspec"
12
12
 
13
- gem 'coveralls', require: false
14
-
15
13
  platforms :rbx do
16
14
  gem 'rubysl', '~> 2.0'
17
15
  gem 'rubinius-developer_tools'
data/Gemfile.rails-4.0 CHANGED
@@ -10,8 +10,6 @@ gem 'tins', '~> 1.6.0', platforms: [:ruby_19]
10
10
 
11
11
  gem "rspec"
12
12
 
13
- gem 'coveralls', require: false
14
-
15
13
  platforms :rbx do
16
14
  gem 'rubysl', '~> 2.0'
17
15
  gem 'rubinius-developer_tools'
data/Gemfile.rails-4.1 CHANGED
@@ -10,8 +10,6 @@ gem 'tins', '~> 1.6.0', platforms: [:ruby_19]
10
10
 
11
11
  gem "rspec"
12
12
 
13
- gem 'coveralls', require: false
14
-
15
13
  platforms :rbx do
16
14
  gem 'rubysl', '~> 2.0'
17
15
  gem 'rubinius-developer_tools'
data/Gemfile.rails-4.2 CHANGED
@@ -10,8 +10,6 @@ gem 'tins', '~> 1.6.0', platforms: [:ruby_19]
10
10
 
11
11
  gem "rspec"
12
12
 
13
- gem 'coveralls', require: false
14
-
15
13
  platforms :rbx do
16
14
  gem 'rubysl', '~> 2.0'
17
15
  gem 'rubinius-developer_tools'
data/Gemfile.rails-5.0 CHANGED
@@ -2,15 +2,13 @@ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'rails', '5.0.0.beta1'
5
+ gem 'rails', '5.0.0'
6
6
  gem 'sqlite3'
7
7
  gem 'activerecord-jdbcsqlite3-adapter', platforms: [:jruby]
8
8
  gem 'activerecord-import'
9
9
 
10
10
  gem "rspec"
11
11
 
12
- gem 'coveralls', require: false
13
-
14
12
  platforms :rbx do
15
13
  gem 'rubysl', '~> 2.0'
16
14
  gem 'rubinius-developer_tools'
data/README.md CHANGED
@@ -2,7 +2,6 @@
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
- [![Coverage Status](https://coveralls.io/repos/flyerhzm/bullet/badge.png?branch=master)](https://coveralls.io/r/flyerhzm/bullet)
6
5
  <a href="https://codeclimate.com/github/flyerhzm/bullet"><img src="https://codeclimate.com/github/flyerhzm/bullet.png" /></a>
7
6
  [![Coderwall Endorse](http://api.coderwall.com/flyerhzm/endorsecount.png)](http://coderwall.com/flyerhzm)
8
7
 
@@ -63,7 +62,7 @@ config.after_initialize do
63
62
  Bullet.add_footer = true
64
63
  Bullet.stacktrace_includes = [ 'your_gem', 'your_middleware' ]
65
64
  Bullet.stacktrace_excludes = [ 'their_gem', 'their_middleware' ]
66
- Bullet.slack = { webhook_url: 'http://some.slack.url', foo: 'bar' }
65
+ Bullet.slack = { webhook_url: 'http://some.slack.url', channel: '#default', username: 'notifier' }
67
66
  end
68
67
  ```
69
68
 
@@ -238,7 +237,9 @@ Bullet outputs some details info, to enable debug mode, set
238
237
 
239
238
  ## Demo
240
239
 
241
- Bullet is designed to function as you browse through your application in development. To see it in action, follow these steps to create, detect, and fix example query problems.
240
+ Bullet is designed to function as you browse through your application in development. To see it in action,
241
+ you can visit [https://github.com/flyerhzm/bullet_test](https://github.com/flyerhzm/bullet_test) or
242
+ follow these steps to create, detect, and fix example query problems.
242
243
 
243
244
  1\. Create an example application
244
245
 
@@ -458,4 +459,4 @@ Meanwhile, there's a line appended to `log/bullet.log`
458
459
  Post => [:comments]
459
460
  ```
460
461
 
461
- Copyright (c) 2009 - 2015 Richard Huang (flyerhzm@gmail.com), released under the MIT license
462
+ Copyright (c) 2009 - 2016 Richard Huang (flyerhzm@gmail.com), released under the MIT license
@@ -134,6 +134,16 @@ module Bullet
134
134
  origin_last(*args)
135
135
  end
136
136
 
137
+ alias_method :origin_include?, :include?
138
+ def include?(object)
139
+ if Bullet.start?
140
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
141
+ end
142
+ origin_include?(object)
143
+ end
144
+ end
145
+
146
+ ::ActiveRecord::Associations::HasManyAssociation.class_eval do
137
147
  alias_method :origin_empty?, :empty?
138
148
  def empty?
139
149
  if Bullet.start? && !has_cached_counter?
@@ -141,13 +151,15 @@ module Bullet
141
151
  end
142
152
  origin_empty?
143
153
  end
154
+ end
144
155
 
145
- alias_method :origin_include?, :include?
146
- def include?(object)
156
+ ::ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
157
+ alias_method :origin_empty?, :empty?
158
+ def empty?
147
159
  if Bullet.start?
148
160
  Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
149
161
  end
150
- origin_include?(object)
162
+ origin_empty?
151
163
  end
152
164
  end
153
165
 
@@ -116,6 +116,16 @@ module Bullet
116
116
  origin_load_target
117
117
  end
118
118
 
119
+ alias_method :origin_include?, :include?
120
+ def include?(object)
121
+ if Bullet.start?
122
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
123
+ end
124
+ origin_include?(object)
125
+ end
126
+ end
127
+
128
+ ::ActiveRecord::Associations::HasManyAssociation.class_eval do
119
129
  alias_method :origin_empty?, :empty?
120
130
  def empty?
121
131
  if Bullet.start? && !has_cached_counter?(@reflection)
@@ -123,13 +133,15 @@ module Bullet
123
133
  end
124
134
  origin_empty?
125
135
  end
136
+ end
126
137
 
127
- alias_method :origin_include?, :include?
128
- def include?(object)
138
+ ::ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
139
+ alias_method :origin_empty?, :empty?
140
+ def empty?
129
141
  if Bullet.start?
130
142
  Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
131
143
  end
132
- origin_include?(object)
144
+ origin_empty?
133
145
  end
134
146
  end
135
147
 
@@ -116,6 +116,16 @@ module Bullet
116
116
  origin_load_target
117
117
  end
118
118
 
119
+ alias_method :origin_include?, :include?
120
+ def include?(object)
121
+ if Bullet.start?
122
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
123
+ end
124
+ origin_include?(object)
125
+ end
126
+ end
127
+
128
+ ::ActiveRecord::Associations::HasManyAssociation.class_eval do
119
129
  alias_method :origin_empty?, :empty?
120
130
  def empty?
121
131
  if Bullet.start? && !has_cached_counter?(@reflection)
@@ -123,13 +133,15 @@ module Bullet
123
133
  end
124
134
  origin_empty?
125
135
  end
136
+ end
126
137
 
127
- alias_method :origin_include?, :include?
128
- def include?(object)
138
+ ::ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
139
+ alias_method :origin_empty?, :empty?
140
+ def empty?
129
141
  if Bullet.start?
130
142
  Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
131
143
  end
132
- origin_include?(object)
144
+ origin_empty?
133
145
  end
134
146
  end
135
147
 
@@ -135,7 +135,9 @@ module Bullet
135
135
  records = origin_load_target
136
136
 
137
137
  if Bullet.start?
138
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) unless @inversed
138
+ if records.size > 1
139
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) unless @inversed
140
+ end
139
141
  if records.first.class.name !~ /^HABTM_/
140
142
  if records.size > 1
141
143
  Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
@@ -40,38 +40,30 @@ module Bullet
40
40
  ::ActiveRecord::Base.prepend(SaveWithBulletSupport)
41
41
 
42
42
  ::ActiveRecord::Relation.class_eval do
43
- alias_method :origin_to_a, :to_a
43
+ alias_method :origin_records, :records
44
44
  # if select a collection of objects, then these objects have possible to cause N+1 query.
45
45
  # if select only one object, then the only one object has impossible to cause N+1 query.
46
- def to_a
47
- records = origin_to_a
46
+ def records
47
+ result = origin_records
48
48
  if Bullet.start?
49
- if records.first.class.name !~ /^HABTM_/
50
- if records.size > 1
51
- Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
52
- Bullet::Detector::CounterCache.add_possible_objects(records)
53
- elsif records.size == 1
54
- Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
55
- Bullet::Detector::CounterCache.add_impossible_object(records.first)
49
+ if result.first.class.name !~ /^HABTM_/
50
+ if result.size > 1
51
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
52
+ Bullet::Detector::CounterCache.add_possible_objects(result)
53
+ elsif result.size == 1
54
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)
55
+ Bullet::Detector::CounterCache.add_impossible_object(result.first)
56
56
  end
57
57
  end
58
58
  end
59
- records
60
- end
61
- end
62
-
63
- ::ActiveRecord::Associations::Association.class_eval do
64
- alias_method :origin_initialize, :initialize
65
- def initialize(owner, reflection)
66
- origin_initialize(owner, reflection)
67
- reflection.set_owner owner
59
+ result
68
60
  end
69
61
  end
70
62
 
71
63
  ::ActiveRecord::Associations::Preloader.class_eval do
72
- alias_method :origin_preloaders_on, :preloaders_on
64
+ alias_method :origin_preloaders_for_one, :preloaders_for_one
73
65
 
74
- def preloaders_on(association, records, scope)
66
+ def preloaders_for_one(association, records, scope)
75
67
  if Bullet.start?
76
68
  records.compact!
77
69
  if records.first.class.name !~ /^HABTM_/
@@ -81,7 +73,7 @@ module Bullet
81
73
  Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, association)
82
74
  end
83
75
  end
84
- origin_preloaders_on(association, records, scope)
76
+ origin_preloaders_for_one(association, records, scope)
85
77
  end
86
78
  end
87
79
 
@@ -143,7 +135,16 @@ module Bullet
143
135
  records = origin_load_target
144
136
 
145
137
  if Bullet.start?
146
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) unless @inversed
138
+ if records.size > 1
139
+ if self.is_a? ::ActiveRecord::Associations::ThroughAssociation
140
+ Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
141
+ association = self.owner.association self.through_reflection.name
142
+ association.target.each do |through_record|
143
+ Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
144
+ end
145
+ end
146
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name) unless @inversed
147
+ end
147
148
  if records.first.class.name !~ /^HABTM_/
148
149
  if records.size > 1
149
150
  Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
@@ -159,8 +160,8 @@ module Bullet
159
160
 
160
161
  alias_method :origin_empty?, :empty?
161
162
  def empty?
162
- if Bullet.start? && !@reflection.has_cached_counter?
163
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
163
+ if Bullet.start? && !reflection.has_cached_counter?
164
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
164
165
  end
165
166
  origin_empty?
166
167
  end
@@ -168,7 +169,7 @@ module Bullet
168
169
  alias_method :origin_include?, :include?
169
170
  def include?(object)
170
171
  if Bullet.start?
171
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
172
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
172
173
  end
173
174
  origin_include?(object)
174
175
  end
@@ -180,9 +181,9 @@ module Bullet
180
181
  def reader(force_reload = false)
181
182
  result = origin_reader(force_reload)
182
183
  if Bullet.start?
183
- if @owner.class.name !~ /^HABTM_/ && !@inversed
184
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
185
- if Bullet::Detector::NPlusOneQuery.impossible?(@owner)
184
+ if owner.class.name !~ /^HABTM_/ && !@inversed
185
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
186
+ if Bullet::Detector::NPlusOneQuery.impossible?(owner)
186
187
  Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
187
188
  else
188
189
  Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
@@ -196,28 +197,20 @@ module Bullet
196
197
  ::ActiveRecord::Associations::HasManyAssociation.class_eval do
197
198
  alias_method :origin_many_empty?, :empty?
198
199
  def empty?
199
- Thread.current[:bullet_collection_empty] = true
200
200
  result = origin_many_empty?
201
- Thread.current[:bullet_collection_empty] = nil
202
- if Bullet.start? && !@reflection.has_cached_counter?
203
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
201
+ if Bullet.start? && !reflection.has_cached_counter?
202
+ Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
204
203
  end
205
204
  result
206
205
  end
207
- end
208
206
 
209
- ::ActiveRecord::Reflection::AbstractReflection.class_eval do
210
- def set_owner(owner)
211
- @owner = owner
212
- end
213
-
214
- alias_method :origin_has_cached_counter?, :has_cached_counter?
215
- def has_cached_counter?
216
- result = origin_has_cached_counter?
217
- if Bullet.start? && !result && !Thread.current[:bullet_collection_empty]
218
- Bullet::Detector::CounterCache.add_counter_cache(@owner, @name)
207
+ alias_method :origin_count_records, :count_records
208
+ def count_records
209
+ result = reflection.has_cached_counter?
210
+ if Bullet.start? && !result && !self.is_a?(::ActiveRecord::Associations::ThroughAssociation)
211
+ Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
219
212
  end
220
- result
213
+ origin_count_records
221
214
  end
222
215
  end
223
216
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Bullet
3
- VERSION = "5.1.0"
3
+ VERSION = "5.2.0"
4
4
  end
data/lib/bullet.rb CHANGED
@@ -190,17 +190,22 @@ module Bullet
190
190
  end
191
191
 
192
192
  def profile
193
+ return_value = nil
193
194
  if Bullet.enable?
194
195
  begin
195
196
  Bullet.start_request
196
197
 
197
- yield
198
+ return_value = yield
198
199
 
199
200
  Bullet.perform_out_of_channel_notifications if Bullet.notification?
200
201
  ensure
201
202
  Bullet.end_request
202
203
  end
204
+ else
205
+ return_value = yield
203
206
  end
207
+
208
+ return_value
204
209
  end
205
210
 
206
211
  private
@@ -214,7 +214,7 @@ if !mongoid? && active_record3?
214
214
  context "post => comment" do
215
215
  it "should detect unused preload with post => comments" do
216
216
  Post.includes(:comments).each do |post|
217
- post.comments.first.name
217
+ post.comments.first.name if post.comments.first
218
218
  end
219
219
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
220
220
  expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Post, :comments)
@@ -453,6 +453,16 @@ if !mongoid? && active_record3?
453
453
 
454
454
  expect(Bullet::Detector::Association).to be_completely_preloading_associations
455
455
  end
456
+
457
+ it "should detect non preload student => teachers with empty?" do
458
+ Student.all.each do |student|
459
+ student.teachers.empty?
460
+ end
461
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
462
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
463
+
464
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Student, :teachers)
465
+ end
456
466
  end
457
467
  end
458
468
 
@@ -214,7 +214,7 @@ if !mongoid? && active_record4?
214
214
  context "post => comment" do
215
215
  it "should detect unused preload with post => comments" do
216
216
  Post.includes(:comments).each do |post|
217
- post.comments.first.name
217
+ post.comments.first.name if post.comments.first
218
218
  end
219
219
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
220
220
  expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Post, :comments)
@@ -465,6 +465,16 @@ if !mongoid? && active_record4?
465
465
 
466
466
  expect(Bullet::Detector::Association).to be_completely_preloading_associations
467
467
  end
468
+
469
+ it "should detect non preload student => teachers with empty?" do
470
+ Student.all.each do |student|
471
+ student.teachers.empty?
472
+ end
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_detecting_unpreloaded_association_for(Student, :teachers)
477
+ end
468
478
  end
469
479
  end
470
480
 
@@ -214,7 +214,7 @@ if !mongoid? && active_record5?
214
214
  context "post => comment" do
215
215
  it "should detect unused preload with post => comments" do
216
216
  Post.includes(:comments).each do |post|
217
- post.comments.first.name
217
+ post.comments.first.name if post.comments.first
218
218
  end
219
219
  Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
220
220
  expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Post, :comments)
@@ -465,6 +465,16 @@ if !mongoid? && active_record5?
465
465
 
466
466
  expect(Bullet::Detector::Association).to be_completely_preloading_associations
467
467
  end
468
+
469
+ it "should detect non preload student => teachers with empty?" do
470
+ Student.all.each do |student|
471
+ student.teachers.empty?
472
+ end
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_detecting_unpreloaded_association_for(Student, :teachers)
477
+ end
468
478
  end
469
479
  end
470
480
 
data/spec/spec_helper.rb CHANGED
@@ -30,9 +30,6 @@ $LOAD_PATH.unshift(MODELS)
30
30
  SUPPORT = File.join(File.dirname(__FILE__), "support")
31
31
  Dir[ File.join(SUPPORT, "*.rb") ].reject { |filename| filename =~ /_seed.rb$/ }.sort.each { |file| require file }
32
32
 
33
- require 'coveralls'
34
- Coveralls.wear!
35
-
36
33
  RSpec.configure do |config|
37
34
  config.extend Bullet::Dependency
38
35
 
@@ -16,6 +16,7 @@ module Support
16
16
  post1 = category1.posts.create(:name => 'first', :writer => writer1)
17
17
  post1a = category1.posts.create(:name => 'like first', :writer => writer2, active: false)
18
18
  post2 = category2.posts.create(:name => 'second', :writer => writer2)
19
+ post3 = category2.posts.create(:name => 'third', :writer => writer2)
19
20
 
20
21
  comment1 = post1.comments.create(:name => 'first', :author => writer1)
21
22
  comment2 = post1.comments.create(:name => 'first2', :author => writer1)
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.1.0
4
+ version: 5.2.0
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-05-22 00:00:00.000000000 Z
11
+ date: 2016-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport