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
@@ -1,5 +1,7 @@
1
1
  module Bullet
2
2
  module ActiveRecord
3
+ LOAD_TARGET = 'load_target'.freeze
4
+
3
5
  def self.enable
4
6
  require 'active_record'
5
7
  ::ActiveRecord::Relation.class_eval do
@@ -8,12 +10,14 @@ module Bullet
8
10
  # if select only one object, then the only one object has impossible to cause N+1 query.
9
11
  def to_a
10
12
  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)
13
+ if Bullet.start?
14
+ if records.size > 1
15
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
16
+ Bullet::Detector::CounterCache.add_possible_objects(records)
17
+ elsif records.size == 1
18
+ Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
19
+ Bullet::Detector::CounterCache.add_impossible_object(records.first)
20
+ end
17
21
  end
18
22
  records
19
23
  end
@@ -24,12 +28,14 @@ module Bullet
24
28
  # include query for one to many associations.
25
29
  # keep this eager loadings.
26
30
  def preload_associations(records, associations, preload_options={})
27
- records = [records].flatten.compact.uniq
28
- return if records.empty?
29
- records.each do |record|
30
- Bullet::Detector::Association.add_object_associations(record, associations)
31
+ if Bullet.start?
32
+ records = [records].flatten.compact.uniq
33
+ return if records.empty?
34
+ records.each do |record|
35
+ Bullet::Detector::Association.add_object_associations(record, associations)
36
+ end
37
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
31
38
  end
32
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
33
39
  origin_preload_associations(records, associations, preload_options={})
34
40
  end
35
41
  end
@@ -39,11 +45,13 @@ module Bullet
39
45
  alias_method :origin_find_with_associations, :find_with_associations
40
46
  def find_with_associations
41
47
  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)
48
+ if Bullet.start?
49
+ associations = (@eager_load_values + @includes_values).uniq
50
+ records.each do |record|
51
+ Bullet::Detector::Association.add_object_associations(record, associations)
52
+ end
53
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
45
54
  end
46
- Bullet::Detector::UnusedEagerLoading.add_eager_loadings(records, associations)
47
55
  records
48
56
  end
49
57
  end
@@ -56,9 +64,11 @@ module Bullet
56
64
  @bullet_eager_loadings = {}
57
65
  records = origin_instantiate(rows)
58
66
 
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)
67
+ if Bullet.start?
68
+ @bullet_eager_loadings.each do |klazz, eager_loadings_hash|
69
+ objects = eager_loadings_hash.keys
70
+ Bullet::Detector::UnusedEagerLoading.add_eager_loadings(objects, eager_loadings_hash[objects.first].to_a)
71
+ end
62
72
  end
63
73
  records
64
74
  end
@@ -67,12 +77,14 @@ module Bullet
67
77
  def construct_association(record, join, row)
68
78
  result = origin_construct_association(record, join, row)
69
79
 
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
80
+ if Bullet.start?
81
+ associations = join.reflection.name
82
+ Bullet::Detector::Association.add_object_associations(record, associations)
83
+ Bullet::Detector::NPlusOneQuery.call_association(record, associations)
84
+ @bullet_eager_loadings[record.class] ||= {}
85
+ @bullet_eager_loadings[record.class][record] ||= Set.new
86
+ @bullet_eager_loadings[record.class][record] << associations
87
+ end
76
88
 
77
89
  result
78
90
  end
@@ -82,31 +94,41 @@ module Bullet
82
94
  # call one to many associations
83
95
  alias_method :origin_load_target, :load_target
84
96
  def load_target
85
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
97
+ if Bullet.start?
98
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
99
+ end
86
100
  origin_load_target
87
101
  end
88
102
 
89
103
  alias_method :origin_first, :first
90
104
  def first(*args)
91
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
105
+ if Bullet.start?
106
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
107
+ end
92
108
  origin_first(*args)
93
109
  end
94
110
 
95
111
  alias_method :origin_last, :last
96
112
  def last(*args)
97
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
113
+ if Bullet.start?
114
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
115
+ end
98
116
  origin_last(*args)
99
117
  end
100
118
 
101
119
  alias_method :origin_empty?, :empty?
102
120
  def empty?
103
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
121
+ if Bullet.start?
122
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
123
+ end
104
124
  origin_empty?
105
125
  end
106
126
 
107
127
  alias_method :origin_include?, :include?
108
128
  def include?(object)
109
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
129
+ if Bullet.start?
130
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
131
+ end
110
132
  origin_include?(object)
111
133
  end
112
134
  end
@@ -117,15 +139,19 @@ module Bullet
117
139
  def load_target
118
140
  # avoid stack level too deep
119
141
  result = origin_load_target
120
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) unless caller.any? { |c| c.include?("load_target") }
121
- Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
142
+ if Bullet.start?
143
+ Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) unless caller.any? { |c| c.include?(LOAD_TARGET) }
144
+ Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
145
+ end
122
146
  result
123
147
  end
124
148
 
125
149
  alias_method :origin_set_inverse_instance, :set_inverse_instance
126
150
  def set_inverse_instance(record, instance)
127
- if record && we_can_set_the_inverse_on_this?(record)
128
- Bullet::Detector::NPlusOneQuery.add_inversed_object(record, @reflection.inverse_of.name)
151
+ if Bullet.start?
152
+ if record && we_can_set_the_inverse_on_this?(record)
153
+ Bullet::Detector::NPlusOneQuery.add_inversed_object(record, @reflection.inverse_of.name)
154
+ end
129
155
  end
130
156
  origin_set_inverse_instance(record, instance)
131
157
  end
@@ -136,7 +162,9 @@ module Bullet
136
162
 
137
163
  def has_cached_counter?
138
164
  result = origin_has_cached_counter?
139
- Bullet::Detector::CounterCache.add_counter_cache(@owner, @reflection.name) unless result
165
+ if Bullet.start? && !result
166
+ Bullet::Detector::CounterCache.add_counter_cache(@owner, @reflection.name)
167
+ end
140
168
  result
141
169
  end
142
170
  end
@@ -145,7 +173,9 @@ module Bullet
145
173
  alias_method :origin_has_cached_counter?, :has_cached_counter?
146
174
  def has_cached_counter?
147
175
  result = origin_has_cached_counter?
148
- Bullet::Detector::CounterCache.add_counter_cache(@owner, @reflection.name) unless result
176
+ if Bullet.start? && !result
177
+ Bullet::Detector::CounterCache.add_counter_cache(@owner, @reflection.name)
178
+ end
149
179
  result
150
180
  end
151
181
  end
@@ -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,8 +130,12 @@ module Bullet
104
130
  alias_method :origin_reader, :reader
105
131
  def reader(force_reload = false)
106
132
  result = origin_reader(force_reload)
107
- Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) unless @inversed
108
- 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
138
+ end
109
139
  result
110
140
  end
111
141
  end
@@ -115,7 +145,9 @@ module Bullet
115
145
 
116
146
  def has_cached_counter?(reflection = reflection())
117
147
  result = origin_has_cached_counter?(reflection)
118
- 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
119
151
  result
120
152
  end
121
153
  end