bullet 4.14.4 → 5.0.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 (53) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +56 -1
  3. data/CHANGELOG.md +38 -2
  4. data/Gemfile.mongoid +0 -2
  5. data/Gemfile.mongoid-2.4 +0 -2
  6. data/Gemfile.mongoid-2.5 +0 -2
  7. data/Gemfile.mongoid-2.6 +0 -2
  8. data/Gemfile.mongoid-2.7 +0 -2
  9. data/Gemfile.mongoid-2.8 +0 -2
  10. data/Gemfile.mongoid-3.0 +0 -2
  11. data/Gemfile.mongoid-3.1 +0 -2
  12. data/Gemfile.mongoid-4.0 +0 -2
  13. data/Gemfile.mongoid-5.0 +17 -0
  14. data/Gemfile.rails-3.1 +0 -2
  15. data/Gemfile.rails-3.2 +0 -2
  16. data/Gemfile.rails-4.0 +0 -2
  17. data/Gemfile.rails-4.1 +0 -2
  18. data/Gemfile.rails-4.2 +0 -2
  19. data/Gemfile.rails-5.0 +17 -0
  20. data/README.md +29 -1
  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 +61 -31
  25. data/lib/bullet/active_record41.rb +63 -32
  26. data/lib/bullet/active_record42.rb +94 -49
  27. data/lib/bullet/active_record5.rb +217 -0
  28. data/lib/bullet/dependency.rb +16 -0
  29. data/lib/bullet/detector/association.rb +16 -16
  30. data/lib/bullet/detector/counter_cache.rb +13 -13
  31. data/lib/bullet/detector/n_plus_one_query.rb +33 -24
  32. data/lib/bullet/detector/unused_eager_loading.rb +1 -1
  33. data/lib/bullet/ext/object.rb +3 -1
  34. data/lib/bullet/mongoid5x.rb +56 -0
  35. data/lib/bullet/notification/n_plus_one_query.rb +6 -0
  36. data/lib/bullet/rack.rb +4 -6
  37. data/lib/bullet/version.rb +1 -1
  38. data/lib/bullet.rb +28 -11
  39. data/spec/bullet/detector/counter_cache_spec.rb +8 -8
  40. data/spec/bullet/detector/n_plus_one_query_spec.rb +42 -27
  41. data/spec/bullet/ext/object_spec.rb +6 -0
  42. data/spec/bullet/notification/base_spec.rb +2 -2
  43. data/spec/bullet/rack_spec.rb +2 -2
  44. data/spec/bullet_spec.rb +47 -0
  45. data/spec/integration/active_record3/association_spec.rb +11 -2
  46. data/spec/integration/active_record4/association_spec.rb +32 -2
  47. data/spec/integration/active_record5/association_spec.rb +715 -0
  48. data/spec/integration/counter_cache_spec.rb +17 -1
  49. data/spec/spec_helper.rb +1 -0
  50. data/spec/support/mongo_seed.rb +13 -0
  51. data/test.sh +1 -0
  52. data/update.sh +1 -0
  53. metadata +13 -7
@@ -8,12 +8,14 @@ module Bullet
8
8
  # if select only one object, then the only one object has impossible to cause N+1 query.
9
9
  def to_a
10
10
  records = origin_to_a
11
- if records.size > 1
12
- Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
13
- Bullet::Detector::CounterCache.add_possible_objects(records)
14
- elsif records.size == 1
15
- Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
16
- Bullet::Detector::CounterCache.add_impossible_object(records.first)
11
+ if Bullet.start?
12
+ if records.size > 1
13
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
14
+ Bullet::Detector::CounterCache.add_possible_objects(records)
15
+ elsif records.size == 1
16
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
17
+ Bullet::Detector::CounterCache.add_impossible_object(records.first)
18
+ end
17
19
  end
18
20
  records
19
21
  end
@@ -25,12 +27,14 @@ module Bullet
25
27
  alias_method :origin_initialize, :initialize
26
28
  def initialize(records, associations, preload_scope = nil)
27
29
  origin_initialize(records, associations, preload_scope)
28
- records = [records].flatten.compact.uniq
29
- return if records.empty?
30
- records.each do |record|
31
- Bullet::Detector::Association.add_object_associations(record, associations)
30
+ if Bullet.start?
31
+ records = [records].flatten.compact.uniq
32
+ return if records.empty?
33
+ records.each do |record|
34
+ Bullet::Detector::Association.add_object_associations(record, associations)
35
+ end
36
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
32
37
  end
33
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
34
38
  end
35
39
  end
36
40
 
@@ -39,11 +43,13 @@ module Bullet
39
43
  alias_method :origin_find_with_associations, :find_with_associations
40
44
  def find_with_associations
41
45
  records = origin_find_with_associations
42
- associations = (eager_load_values + includes_values).uniq
43
- records.each do |record|
44
- Bullet::Detector::Association.add_object_associations(record, associations)
46
+ if Bullet.start?
47
+ associations = (eager_load_values + includes_values).uniq
48
+ records.each do |record|
49
+ Bullet::Detector::Association.add_object_associations(record, associations)
50
+ end
51
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
45
52
  end
46
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
47
53
  records
48
54
  end
49
55
  end
@@ -56,9 +62,11 @@ module Bullet
56
62
  @bullet_eager_loadings = {}
57
63
  records = origin_instantiate(rows)
58
64
 
59
- @bullet_eager_loadings.each do |klazz, eager_loadings_hash|
60
- objects = eager_loadings_hash.keys
61
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
65
+ if Bullet.start?
66
+ @bullet_eager_loadings.each do |klazz, eager_loadings_hash|
67
+ objects = eager_loadings_hash.keys
68
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
69
+ end
62
70
  end
63
71
  records
64
72
  end
@@ -67,12 +75,14 @@ module Bullet
67
75
  def construct_association(record, join, row)
68
76
  result = origin_construct_association(record, join, row)
69
77
 
70
- associations = join.reflection.name
71
- Bullet::Detector::Association.add_object_associations(record, associations)
72
- Bullet::Detector::NPlusOneQuery.call_association(record, associations)
73
- @bullet_eager_loadings[record.class] ||= {}
74
- @bullet_eager_loadings[record.class][record] ||= Set.new
75
- @bullet_eager_loadings[record.class][record] << associations
78
+ if Bullet.start?
79
+ associations = join.reflection.name
80
+ Bullet::Detector::Association.add_object_associations(record, associations)
81
+ Bullet::Detector::NPlusOneQuery.call_association(record, associations)
82
+ @bullet_eager_loadings[record.class] ||= {}
83
+ @bullet_eager_loadings[record.class][record] ||= Set.new
84
+ @bullet_eager_loadings[record.class][record] << associations
85
+ end
76
86
 
77
87
  result
78
88
  end
@@ -82,19 +92,25 @@ module Bullet
82
92
  # call one to many associations
83
93
  alias_method :origin_load_target, :load_target
84
94
  def load_target
85
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
95
+ if Bullet.start?
96
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
97
+ end
86
98
  origin_load_target
87
99
  end
88
100
 
89
101
  alias_method :origin_empty?, :empty?
90
102
  def empty?
91
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
103
+ if Bullet.start?
104
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
105
+ end
92
106
  origin_empty?
93
107
  end
94
108
 
95
109
  alias_method :origin_include?, :include?
96
110
  def include?(object)
97
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
111
+ if Bullet.start?
112
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
113
+ end
98
114
  origin_include?(object)
99
115
  end
100
116
  end
@@ -104,8 +120,10 @@ module Bullet
104
120
  alias_method :origin_reader, :reader
105
121
  def reader(force_reload = false)
106
122
  result = origin_reader(force_reload)
107
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
108
- Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
123
+ if Bullet.start?
124
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
125
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
126
+ end
109
127
  result
110
128
  end
111
129
  end
@@ -113,8 +131,10 @@ module Bullet
113
131
  ::ActiveRecord::Associations::Association.class_eval do
114
132
  alias_method :origin_set_inverse_instance, :set_inverse_instance
115
133
  def set_inverse_instance(record)
116
- if record && invertible_for?(record)
117
- Bullet::Detector::NPlusOneQuery.add_inversed_object(record, inverse_reflection_for(record).name)
134
+ if Bullet.start?
135
+ if record && invertible_for?(record)
136
+ Bullet::Detector::NPlusOneQuery.add_inversed_object(record, inverse_reflection_for(record).name)
137
+ end
118
138
  end
119
139
  origin_set_inverse_instance(record)
120
140
  end
@@ -125,7 +145,9 @@ module Bullet
125
145
 
126
146
  def has_cached_counter?(reflection = reflection())
127
147
  result = origin_has_cached_counter?(reflection)
128
- Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name) unless result
148
+ if Bullet.start? && !result
149
+ Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
150
+ end
129
151
  result
130
152
  end
131
153
  end
@@ -8,29 +8,43 @@ module Bullet
8
8
  # if select only one object, then the only one object has impossible to cause N+1 query.
9
9
  def to_a
10
10
  records = origin_to_a
11
- if records.size > 1
12
- Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
13
- Bullet::Detector::CounterCache.add_possible_objects(records)
14
- elsif records.size == 1
15
- Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
16
- Bullet::Detector::CounterCache.add_impossible_object(records.first)
11
+ if Bullet.start?
12
+ if records.size > 1
13
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
14
+ Bullet::Detector::CounterCache.add_possible_objects(records)
15
+ elsif records.size == 1
16
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
17
+ Bullet::Detector::CounterCache.add_impossible_object(records.first)
18
+ end
17
19
  end
18
20
  records
19
21
  end
20
22
  end
21
23
 
24
+ ::ActiveRecord::Persistence.class_eval do
25
+ def save_with_bullet(*args, &proc)
26
+ was_new_record = new_record?
27
+ save_without_bullet(*args, &proc).tap do |result|
28
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(self) if result && was_new_record
29
+ end
30
+ end
31
+ alias_method_chain :save, :bullet
32
+ end
33
+
22
34
  ::ActiveRecord::Associations::Preloader.class_eval do
23
35
  # include query for one to many associations.
24
36
  # keep this eager loadings.
25
37
  alias_method :origin_initialize, :initialize
26
38
  def initialize(records, associations, preload_scope = nil)
27
39
  origin_initialize(records, associations, preload_scope)
28
- records = [records].flatten.compact.uniq
29
- return if records.empty?
30
- records.each do |record|
31
- Bullet::Detector::Association.add_object_associations(record, associations)
40
+ if Bullet.start?
41
+ records = [records].flatten.compact.uniq
42
+ return if records.empty?
43
+ records.each do |record|
44
+ Bullet::Detector::Association.add_object_associations(record, associations)
45
+ end
46
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
32
47
  end
33
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
34
48
  end
35
49
  end
36
50
 
@@ -39,11 +53,13 @@ module Bullet
39
53
  alias_method :origin_find_with_associations, :find_with_associations
40
54
  def find_with_associations
41
55
  records = origin_find_with_associations
42
- associations = (eager_load_values + includes_values).uniq
43
- records.each do |record|
44
- Bullet::Detector::Association.add_object_associations(record, associations)
56
+ if Bullet.start?
57
+ associations = (eager_load_values + includes_values).uniq
58
+ records.each do |record|
59
+ Bullet::Detector::Association.add_object_associations(record, associations)
60
+ end
61
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
45
62
  end
46
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
47
63
  records
48
64
  end
49
65
  end
@@ -56,9 +72,11 @@ module Bullet
56
72
  @bullet_eager_loadings = {}
57
73
  records = origin_instantiate(rows)
58
74
 
59
- @bullet_eager_loadings.each do |klazz, eager_loadings_hash|
60
- objects = eager_loadings_hash.keys
61
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
75
+ if Bullet.start?
76
+ @bullet_eager_loadings.each do |klazz, eager_loadings_hash|
77
+ objects = eager_loadings_hash.keys
78
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
79
+ end
62
80
  end
63
81
  records
64
82
  end
@@ -67,12 +85,14 @@ module Bullet
67
85
  def construct_association(record, join, row)
68
86
  result = origin_construct_association(record, join, row)
69
87
 
70
- associations = join.reflection.name
71
- Bullet::Detector::Association.add_object_associations(record, associations)
72
- Bullet::Detector::NPlusOneQuery.call_association(record, associations)
73
- @bullet_eager_loadings[record.class] ||= {}
74
- @bullet_eager_loadings[record.class][record] ||= Set.new
75
- @bullet_eager_loadings[record.class][record] << associations
88
+ if Bullet.start?
89
+ associations = join.reflection.name
90
+ Bullet::Detector::Association.add_object_associations(record, associations)
91
+ Bullet::Detector::NPlusOneQuery.call_association(record, associations)
92
+ @bullet_eager_loadings[record.class] ||= {}
93
+ @bullet_eager_loadings[record.class][record] ||= Set.new
94
+ @bullet_eager_loadings[record.class][record] << associations
95
+ end
76
96
 
77
97
  result
78
98
  end
@@ -82,19 +102,25 @@ module Bullet
82
102
  # call one to many associations
83
103
  alias_method :origin_load_target, :load_target
84
104
  def load_target
85
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
105
+ if Bullet.start?
106
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
107
+ end
86
108
  origin_load_target
87
109
  end
88
110
 
89
111
  alias_method :origin_empty?, :empty?
90
112
  def empty?
91
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
113
+ if Bullet.start?
114
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
115
+ end
92
116
  origin_empty?
93
117
  end
94
118
 
95
119
  alias_method :origin_include?, :include?
96
120
  def include?(object)
97
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
121
+ if Bullet.start?
122
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
123
+ end
98
124
  origin_include?(object)
99
125
  end
100
126
  end
@@ -104,9 +130,11 @@ module Bullet
104
130
  alias_method :origin_reader, :reader
105
131
  def reader(force_reload = false)
106
132
  result = origin_reader(force_reload)
107
- unless @inversed
108
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
109
- Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
133
+ if Bullet.start?
134
+ unless @inversed
135
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
136
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
137
+ end
110
138
  end
111
139
  result
112
140
  end
@@ -117,7 +145,9 @@ module Bullet
117
145
 
118
146
  def has_cached_counter?(reflection = reflection())
119
147
  result = origin_has_cached_counter?(reflection)
120
- Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name) unless result
148
+ if Bullet.start? && !result
149
+ Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
150
+ end
121
151
  result
122
152
  end
123
153
  end
@@ -8,29 +8,43 @@ module Bullet
8
8
  # if select only one object, then the only one object has impossible to cause N+1 query.
9
9
  def to_a
10
10
  records = origin_to_a
11
- if records.first.class.name !~ /^HABTM_/
12
- if records.size > 1
13
- Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
14
- Bullet::Detector::CounterCache.add_possible_objects(records)
15
- elsif records.size == 1
16
- Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
17
- Bullet::Detector::CounterCache.add_impossible_object(records.first)
11
+ if Bullet.start?
12
+ if records.first.class.name !~ /^HABTM_/
13
+ if records.size > 1
14
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
15
+ Bullet::Detector::CounterCache.add_possible_objects(records)
16
+ elsif records.size == 1
17
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
18
+ Bullet::Detector::CounterCache.add_impossible_object(records.first)
19
+ end
18
20
  end
19
21
  end
20
22
  records
21
23
  end
22
24
  end
23
25
 
26
+ ::ActiveRecord::Persistence.class_eval do
27
+ def save_with_bullet(*args, &proc)
28
+ was_new_record = new_record?
29
+ save_without_bullet(*args, &proc).tap do |result|
30
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(self) if result && was_new_record
31
+ end
32
+ end
33
+ alias_method_chain :save, :bullet
34
+ end
35
+
24
36
  ::ActiveRecord::Associations::Preloader.class_eval do
25
37
  alias_method :origin_preloaders_on, :preloaders_on
26
38
 
27
39
  def preloaders_on(association, records, scope)
28
- records.compact!
29
- if records.first.class.name !~ /^HABTM_/
30
- records.each do |record|
31
- Bullet::Detector::Association.add_object_associations(record, association)
40
+ if Bullet.start?
41
+ records.compact!
42
+ if records.first.class.name !~ /^HABTM_/
43
+ records.each do |record|
44
+ Bullet::Detector::Association.add_object_associations(record, association)
45
+ end
46
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, association)
32
47
  end
33
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, association)
34
48
  end
35
49
  origin_preloaders_on(association, records, scope)
36
50
  end
@@ -40,12 +54,15 @@ module Bullet
40
54
  # add includes in scope
41
55
  alias_method :origin_find_with_associations, :find_with_associations
42
56
  def find_with_associations
57
+ return origin_find_with_associations { |r| yield r } if block_given?
43
58
  records = origin_find_with_associations
44
- associations = (eager_load_values + includes_values).uniq
45
- records.each do |record|
46
- Bullet::Detector::Association.add_object_associations(record, associations)
59
+ if Bullet.start?
60
+ associations = (eager_load_values + includes_values).uniq
61
+ records.each do |record|
62
+ Bullet::Detector::Association.add_object_associations(record, associations)
63
+ end
64
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
47
65
  end
48
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
49
66
  records
50
67
  end
51
68
  end
@@ -58,9 +75,11 @@ module Bullet
58
75
  @bullet_eager_loadings = {}
59
76
  records = origin_instantiate(result_set, aliases)
60
77
 
61
- @bullet_eager_loadings.each do |klazz, eager_loadings_hash|
62
- objects = eager_loadings_hash.keys
63
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
78
+ if Bullet.start?
79
+ @bullet_eager_loadings.each do |klazz, eager_loadings_hash|
80
+ objects = eager_loadings_hash.keys
81
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
82
+ end
64
83
  end
65
84
  records
66
85
  end
@@ -69,12 +88,14 @@ module Bullet
69
88
  def construct_model(record, node, row, model_cache, id, aliases)
70
89
  result = origin_construct_model(record, node, row, model_cache, id, aliases)
71
90
 
72
- associations = node.reflection.name
73
- Bullet::Detector::Association.add_object_associations(record, associations)
74
- Bullet::Detector::NPlusOneQuery.call_association(record, associations)
75
- @bullet_eager_loadings[record.class] ||= {}
76
- @bullet_eager_loadings[record.class][record] ||= Set.new
77
- @bullet_eager_loadings[record.class][record] << associations
91
+ if Bullet.start?
92
+ associations = node.reflection.name
93
+ Bullet::Detector::Association.add_object_associations(record, associations)
94
+ Bullet::Detector::NPlusOneQuery.call_association(record, associations)
95
+ @bullet_eager_loadings[record.class] ||= {}
96
+ @bullet_eager_loadings[record.class][record] ||= Set.new
97
+ @bullet_eager_loadings[record.class][record] << associations
98
+ end
78
99
 
79
100
  result
80
101
  end
@@ -84,19 +105,25 @@ module Bullet
84
105
  # call one to many associations
85
106
  alias_method :origin_load_target, :load_target
86
107
  def load_target
87
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) unless @inversed
108
+ if Bullet.start?
109
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) unless @inversed
110
+ end
88
111
  origin_load_target
89
112
  end
90
113
 
91
114
  alias_method :origin_empty?, :empty?
92
115
  def empty?
93
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
116
+ if Bullet.start?
117
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
118
+ end
94
119
  origin_empty?
95
120
  end
96
121
 
97
122
  alias_method :origin_include?, :include?
98
123
  def include?(object)
99
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
124
+ if Bullet.start?
125
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
126
+ end
100
127
  origin_include?(object)
101
128
  end
102
129
  end
@@ -106,9 +133,11 @@ module Bullet
106
133
  alias_method :origin_reader, :reader
107
134
  def reader(force_reload = false)
108
135
  result = origin_reader(force_reload)
109
- if @owner.class.name !~ /^HABTM_/ && !@inversed
110
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
111
- Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
136
+ if Bullet.start?
137
+ if @owner.class.name !~ /^HABTM_/ && !@inversed
138
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
139
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
140
+ end
112
141
  end
113
142
  result
114
143
  end
@@ -119,7 +148,9 @@ module Bullet
119
148
 
120
149
  def has_cached_counter?(reflection = reflection())
121
150
  result = origin_has_cached_counter?(reflection)
122
- Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name) unless result
151
+ if Bullet.start? && !result
152
+ Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
153
+ end
123
154
  result
124
155
  end
125
156
  end