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.
- checksums.yaml +4 -4
- data/.travis.yml +30 -0
- data/CHANGELOG.md +29 -0
- data/Gemfile.mongoid-5.0 +17 -0
- data/Gemfile.rails-5.0 +17 -0
- data/README.md +23 -0
- data/lib/bullet/active_record3.rb +65 -35
- data/lib/bullet/active_record3x.rb +54 -32
- data/lib/bullet/active_record4.rb +61 -31
- data/lib/bullet/active_record41.rb +62 -32
- data/lib/bullet/active_record42.rb +93 -49
- data/lib/bullet/active_record5.rb +217 -0
- data/lib/bullet/dependency.rb +16 -0
- data/lib/bullet/detector/association.rb +16 -16
- data/lib/bullet/detector/counter_cache.rb +13 -13
- data/lib/bullet/detector/n_plus_one_query.rb +33 -27
- data/lib/bullet/detector/unused_eager_loading.rb +1 -1
- data/lib/bullet/ext/object.rb +3 -1
- data/lib/bullet/mongoid5x.rb +56 -0
- data/lib/bullet/rack.rb +2 -2
- data/lib/bullet/version.rb +1 -1
- data/lib/bullet.rb +19 -5
- data/spec/bullet/detector/counter_cache_spec.rb +8 -8
- data/spec/bullet/detector/n_plus_one_query_spec.rb +42 -27
- data/spec/bullet/ext/object_spec.rb +6 -0
- data/spec/bullet/notification/base_spec.rb +2 -2
- data/spec/bullet_spec.rb +47 -0
- data/spec/integration/active_record3/association_spec.rb +11 -2
- data/spec/integration/active_record4/association_spec.rb +32 -2
- data/spec/integration/active_record5/association_spec.rb +715 -0
- data/spec/integration/counter_cache_spec.rb +17 -1
- data/spec/spec_helper.rb +1 -0
- data/spec/support/mongo_seed.rb +13 -0
- data/test.sh +1 -0
- data/update.sh +1 -0
- metadata +9 -3
|
@@ -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
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
|
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
|
|
12
|
-
if records.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
records.
|
|
31
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
111
|
-
|
|
112
|
-
|
|
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
|
|
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
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
|
29
|
-
if records.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
records.
|
|
48
|
-
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
|
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
|
|
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
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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
|
|
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
|