bullet 4.14.5 → 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.
@@ -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
@@ -42,11 +56,13 @@ module Bullet
42
56
  def find_with_associations
43
57
  return origin_find_with_associations { |r| yield r } if block_given?
44
58
  records = origin_find_with_associations
45
- associations = (eager_load_values + includes_values).uniq
46
- records.each do |record|
47
- 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)
48
65
  end
49
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
50
66
  records
51
67
  end
52
68
  end
@@ -59,9 +75,11 @@ module Bullet
59
75
  @bullet_eager_loadings = {}
60
76
  records = origin_instantiate(result_set, aliases)
61
77
 
62
- @bullet_eager_loadings.each do |klazz, eager_loadings_hash|
63
- objects = eager_loadings_hash.keys
64
- 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
65
83
  end
66
84
  records
67
85
  end
@@ -70,12 +88,14 @@ module Bullet
70
88
  def construct_model(record, node, row, model_cache, id, aliases)
71
89
  result = origin_construct_model(record, node, row, model_cache, id, aliases)
72
90
 
73
- associations = node.reflection.name
74
- Bullet::Detector::Association.add_object_associations(record, associations)
75
- Bullet::Detector::NPlusOneQuery.call_association(record, associations)
76
- @bullet_eager_loadings[record.class] ||= {}
77
- @bullet_eager_loadings[record.class][record] ||= Set.new
78
- @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
79
99
 
80
100
  result
81
101
  end
@@ -85,19 +105,25 @@ module Bullet
85
105
  # call one to many associations
86
106
  alias_method :origin_load_target, :load_target
87
107
  def load_target
88
- 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
89
111
  origin_load_target
90
112
  end
91
113
 
92
114
  alias_method :origin_empty?, :empty?
93
115
  def empty?
94
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
116
+ if Bullet.start?
117
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
118
+ end
95
119
  origin_empty?
96
120
  end
97
121
 
98
122
  alias_method :origin_include?, :include?
99
123
  def include?(object)
100
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
124
+ if Bullet.start?
125
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
126
+ end
101
127
  origin_include?(object)
102
128
  end
103
129
  end
@@ -107,9 +133,11 @@ module Bullet
107
133
  alias_method :origin_reader, :reader
108
134
  def reader(force_reload = false)
109
135
  result = origin_reader(force_reload)
110
- if @owner.class.name !~ /^HABTM_/ && !@inversed
111
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
112
- 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
113
141
  end
114
142
  result
115
143
  end
@@ -120,7 +148,9 @@ module Bullet
120
148
 
121
149
  def has_cached_counter?(reflection = reflection())
122
150
  result = origin_has_cached_counter?(reflection)
123
- 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
124
154
  result
125
155
  end
126
156
  end
@@ -7,31 +7,45 @@ module Bullet
7
7
  alias_method :origin_find, :find
8
8
  def find(*args)
9
9
  result = origin_find(*args)
10
- if result.is_a? Array
11
- Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
12
- Bullet::Detector::CounterCache.add_possible_objects(result)
13
- elsif result.is_a? ::ActiveRecord::Base
14
- Bullet::Detector::NPlusOneQuery.add_impossible_object(result)
15
- Bullet::Detector::CounterCache.add_impossible_object(result)
10
+ if Bullet.start?
11
+ if result.is_a? Array
12
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
13
+ Bullet::Detector::CounterCache.add_possible_objects(result)
14
+ elsif result.is_a? ::ActiveRecord::Base
15
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(result)
16
+ Bullet::Detector::CounterCache.add_impossible_object(result)
17
+ end
16
18
  end
17
19
  result
18
20
  end
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::Relation.class_eval do
23
35
  alias_method :origin_to_a, :to_a
24
36
  # if select a collection of objects, then these objects have possible to cause N+1 query.
25
37
  # if select only one object, then the only one object has impossible to cause N+1 query.
26
38
  def to_a
27
39
  records = origin_to_a
28
- if records.first.class.name !~ /^HABTM_/
29
- if records.size > 1
30
- Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
31
- Bullet::Detector::CounterCache.add_possible_objects(records)
32
- elsif records.size == 1
33
- Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
34
- Bullet::Detector::CounterCache.add_impossible_object(records.first)
40
+ if Bullet.start?
41
+ if records.first.class.name !~ /^HABTM_/
42
+ if records.size > 1
43
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
44
+ Bullet::Detector::CounterCache.add_possible_objects(records)
45
+ elsif records.size == 1
46
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
47
+ Bullet::Detector::CounterCache.add_impossible_object(records.first)
48
+ end
35
49
  end
36
50
  end
37
51
  records
@@ -42,12 +56,14 @@ module Bullet
42
56
  alias_method :origin_preloaders_on, :preloaders_on
43
57
 
44
58
  def preloaders_on(association, records, scope)
45
- records.compact!
46
- if records.first.class.name !~ /^HABTM_/
47
- records.each do |record|
48
- Bullet::Detector::Association.add_object_associations(record, association)
59
+ if Bullet.start?
60
+ records.compact!
61
+ if records.first.class.name !~ /^HABTM_/
62
+ records.each do |record|
63
+ Bullet::Detector::Association.add_object_associations(record, association)
64
+ end
65
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, association)
49
66
  end
50
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, association)
51
67
  end
52
68
  origin_preloaders_on(association, records, scope)
53
69
  end
@@ -59,11 +75,13 @@ module Bullet
59
75
  def find_with_associations
60
76
  return origin_find_with_associations { |r| yield r } if block_given?
61
77
  records = origin_find_with_associations
62
- associations = (eager_load_values + includes_values).uniq
63
- records.each do |record|
64
- Bullet::Detector::Association.add_object_associations(record, associations)
78
+ if Bullet.start?
79
+ associations = (eager_load_values + includes_values).uniq
80
+ records.each do |record|
81
+ Bullet::Detector::Association.add_object_associations(record, associations)
82
+ end
83
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
65
84
  end
66
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
67
85
  records
68
86
  end
69
87
  end
@@ -76,9 +94,11 @@ module Bullet
76
94
  @bullet_eager_loadings = {}
77
95
  records = origin_instantiate(result_set, aliases)
78
96
 
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)
97
+ if Bullet.start?
98
+ @bullet_eager_loadings.each do |klazz, eager_loadings_hash|
99
+ objects = eager_loadings_hash.keys
100
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
101
+ end
82
102
  end
83
103
  records
84
104
  end
@@ -87,12 +107,14 @@ module Bullet
87
107
  def construct_model(record, node, row, model_cache, id, aliases)
88
108
  result = origin_construct_model(record, node, row, model_cache, id, aliases)
89
109
 
90
- associations = node.reflection.name
91
- Bullet::Detector::Association.add_object_associations(record, associations)
92
- Bullet::Detector::NPlusOneQuery.call_association(record, associations)
93
- @bullet_eager_loadings[record.class] ||= {}
94
- @bullet_eager_loadings[record.class][record] ||= Set.new
95
- @bullet_eager_loadings[record.class][record] << associations
110
+ if Bullet.start?
111
+ associations = node.reflection.name
112
+ Bullet::Detector::Association.add_object_associations(record, associations)
113
+ Bullet::Detector::NPlusOneQuery.call_association(record, associations)
114
+ @bullet_eager_loadings[record.class] ||= {}
115
+ @bullet_eager_loadings[record.class][record] ||= Set.new
116
+ @bullet_eager_loadings[record.class][record] << associations
117
+ end
96
118
 
97
119
  result
98
120
  end
@@ -102,16 +124,18 @@ module Bullet
102
124
  # call one to many associations
103
125
  alias_method :origin_load_target, :load_target
104
126
  def load_target
105
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) unless @inversed
106
127
  records = origin_load_target
107
128
 
108
- if records.first.class.name !~ /^HABTM_/
109
- if records.size > 1
110
- Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
111
- Bullet::Detector::CounterCache.add_possible_objects(records)
112
- elsif records.size == 1
113
- Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
114
- Bullet::Detector::CounterCache.add_impossible_object(records.first)
129
+ if Bullet.start?
130
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) unless @inversed
131
+ if records.first.class.name !~ /^HABTM_/
132
+ if records.size > 1
133
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
134
+ Bullet::Detector::CounterCache.add_possible_objects(records)
135
+ elsif records.size == 1
136
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
137
+ Bullet::Detector::CounterCache.add_impossible_object(records.first)
138
+ end
115
139
  end
116
140
  end
117
141
  records
@@ -119,13 +143,17 @@ module Bullet
119
143
 
120
144
  alias_method :origin_empty?, :empty?
121
145
  def empty?
122
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
146
+ if Bullet.start?
147
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
148
+ end
123
149
  origin_empty?
124
150
  end
125
151
 
126
152
  alias_method :origin_include?, :include?
127
153
  def include?(object)
128
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
154
+ if Bullet.start?
155
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
156
+ end
129
157
  origin_include?(object)
130
158
  end
131
159
  end
@@ -135,12 +163,15 @@ module Bullet
135
163
  alias_method :origin_reader, :reader
136
164
  def reader(force_reload = false)
137
165
  result = origin_reader(force_reload)
138
- if @owner.class.name !~ /^HABTM_/ && !@inversed
139
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
140
- Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
141
- end
142
- if ::ActiveRecord::Reflection::HasOneReflection === @reflection && result
143
- Bullet::Detector::NPlusOneQuery.add_impossible_object(result)
166
+ if Bullet.start?
167
+ if @owner.class.name !~ /^HABTM_/ && !@inversed
168
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
169
+ if Bullet::Detector::NPlusOneQuery.impossible?(@owner)
170
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
171
+ else
172
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
173
+ end
174
+ end
144
175
  end
145
176
  result
146
177
  end
@@ -152,14 +183,27 @@ module Bullet
152
183
  Thread.current[:bullet_collection_empty] = true
153
184
  result = origin_many_empty?
154
185
  Thread.current[:bullet_collection_empty] = nil
155
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
186
+ if Bullet.start?
187
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
188
+ end
156
189
  result
157
190
  end
158
191
 
159
192
  alias_method :origin_has_cached_counter?, :has_cached_counter?
160
193
  def has_cached_counter?(reflection = reflection())
161
194
  result = origin_has_cached_counter?(reflection)
162
- if !result && !Thread.current[:bullet_collection_empty]
195
+ if Bullet.start? && !result && !Thread.current[:bullet_collection_empty]
196
+ Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
197
+ end
198
+ result
199
+ end
200
+ end
201
+
202
+ ::ActiveRecord::Associations::HasManyThroughAssociation.class_eval do
203
+ alias_method :origin_hmt_has_cached_counter?, :has_cached_counter?
204
+ def has_cached_counter?(reflection = reflection())
205
+ result = origin_hmt_has_cached_counter?(reflection)
206
+ if Bullet.start? && !result && !Thread.current[:bullet_collection_empty]
163
207
  Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
164
208
  end
165
209
  result