bullet 6.0.2 → 7.0.3
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/.github/workflows/main.yml +82 -0
- data/CHANGELOG.md +52 -0
- data/Gemfile.rails-6.0 +1 -1
- data/Gemfile.rails-6.1 +15 -0
- data/Gemfile.rails-7.0 +10 -0
- data/README.md +39 -25
- data/lib/bullet/active_job.rb +1 -3
- data/lib/bullet/active_record4.rb +9 -23
- data/lib/bullet/active_record41.rb +8 -19
- data/lib/bullet/active_record42.rb +9 -16
- data/lib/bullet/active_record5.rb +188 -170
- data/lib/bullet/active_record52.rb +182 -162
- data/lib/bullet/active_record60.rb +191 -178
- data/lib/bullet/active_record61.rb +272 -0
- data/lib/bullet/active_record70.rb +275 -0
- data/lib/bullet/bullet_xhr.js +18 -23
- data/lib/bullet/dependency.rb +52 -34
- data/lib/bullet/detector/association.rb +24 -18
- data/lib/bullet/detector/counter_cache.rb +12 -8
- data/lib/bullet/detector/n_plus_one_query.rb +20 -10
- data/lib/bullet/detector/unused_eager_loading.rb +7 -4
- data/lib/bullet/mongoid4x.rb +3 -7
- data/lib/bullet/mongoid5x.rb +3 -7
- data/lib/bullet/mongoid6x.rb +3 -7
- data/lib/bullet/mongoid7x.rb +26 -13
- data/lib/bullet/notification/base.rb +14 -18
- data/lib/bullet/notification/n_plus_one_query.rb +2 -4
- data/lib/bullet/notification/unused_eager_loading.rb +2 -4
- data/lib/bullet/notification.rb +2 -1
- data/lib/bullet/rack.rb +28 -17
- data/lib/bullet/stack_trace_filter.rb +10 -17
- data/lib/bullet/version.rb +1 -1
- data/lib/bullet.rb +52 -42
- data/lib/generators/bullet/install_generator.rb +22 -23
- data/perf/benchmark.rb +11 -14
- data/spec/bullet/detector/counter_cache_spec.rb +6 -6
- data/spec/bullet/detector/n_plus_one_query_spec.rb +8 -4
- data/spec/bullet/detector/unused_eager_loading_spec.rb +25 -8
- data/spec/bullet/ext/object_spec.rb +1 -1
- data/spec/bullet/notification/base_spec.rb +5 -7
- data/spec/bullet/notification/n_plus_one_query_spec.rb +16 -3
- data/spec/bullet/notification/unused_eager_loading_spec.rb +5 -1
- data/spec/bullet/rack_spec.rb +154 -13
- data/spec/bullet/registry/association_spec.rb +2 -2
- data/spec/bullet/registry/base_spec.rb +1 -1
- data/spec/bullet_spec.rb +25 -44
- data/spec/integration/active_record/association_spec.rb +104 -130
- data/spec/integration/counter_cache_spec.rb +14 -34
- data/spec/integration/mongoid/association_spec.rb +19 -33
- data/spec/models/attachment.rb +5 -0
- data/spec/models/deal.rb +5 -0
- data/spec/models/post.rb +2 -0
- data/spec/models/role.rb +7 -0
- data/spec/models/submission.rb +1 -0
- data/spec/models/user.rb +2 -0
- data/spec/spec_helper.rb +4 -10
- data/spec/support/bullet_ext.rb +8 -9
- data/spec/support/mongo_seed.rb +3 -16
- data/spec/support/sqlite_seed.rb +38 -0
- data/test.sh +2 -0
- metadata +17 -7
- data/.travis.yml +0 -31
data/lib/bullet/dependency.rb
CHANGED
@@ -3,49 +3,55 @@
|
|
3
3
|
module Bullet
|
4
4
|
module Dependency
|
5
5
|
def mongoid?
|
6
|
-
@mongoid ||= defined?
|
6
|
+
@mongoid ||= defined?(::Mongoid)
|
7
7
|
end
|
8
8
|
|
9
9
|
def active_record?
|
10
|
-
@active_record ||= defined?
|
10
|
+
@active_record ||= defined?(::ActiveRecord)
|
11
11
|
end
|
12
12
|
|
13
13
|
def active_record_version
|
14
|
-
@active_record_version ||=
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
14
|
+
@active_record_version ||=
|
15
|
+
begin
|
16
|
+
if active_record40?
|
17
|
+
'active_record4'
|
18
|
+
elsif active_record41?
|
19
|
+
'active_record41'
|
20
|
+
elsif active_record42?
|
21
|
+
'active_record42'
|
22
|
+
elsif active_record50?
|
23
|
+
'active_record5'
|
24
|
+
elsif active_record51?
|
25
|
+
'active_record5'
|
26
|
+
elsif active_record52?
|
27
|
+
'active_record52'
|
28
|
+
elsif active_record60?
|
29
|
+
'active_record60'
|
30
|
+
elsif active_record61?
|
31
|
+
'active_record61'
|
32
|
+
elsif active_record70?
|
33
|
+
'active_record70'
|
34
|
+
else
|
35
|
+
raise "Bullet does not support active_record #{::ActiveRecord::VERSION::STRING} yet"
|
36
|
+
end
|
37
|
+
end
|
33
38
|
end
|
34
39
|
|
35
40
|
def mongoid_version
|
36
|
-
@mongoid_version ||=
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
41
|
+
@mongoid_version ||=
|
42
|
+
begin
|
43
|
+
if mongoid4x?
|
44
|
+
'mongoid4x'
|
45
|
+
elsif mongoid5x?
|
46
|
+
'mongoid5x'
|
47
|
+
elsif mongoid6x?
|
48
|
+
'mongoid6x'
|
49
|
+
elsif mongoid7x?
|
50
|
+
'mongoid7x'
|
51
|
+
else
|
52
|
+
raise "Bullet does not support mongoid #{::Mongoid::VERSION} yet"
|
53
|
+
end
|
54
|
+
end
|
49
55
|
end
|
50
56
|
|
51
57
|
def active_record4?
|
@@ -60,6 +66,10 @@ module Bullet
|
|
60
66
|
active_record? && ::ActiveRecord::VERSION::MAJOR == 6
|
61
67
|
end
|
62
68
|
|
69
|
+
def active_record7?
|
70
|
+
active_record? && ::ActiveRecord::VERSION::MAJOR == 7
|
71
|
+
end
|
72
|
+
|
63
73
|
def active_record40?
|
64
74
|
active_record4? && ::ActiveRecord::VERSION::MINOR == 0
|
65
75
|
end
|
@@ -88,6 +98,14 @@ module Bullet
|
|
88
98
|
active_record6? && ::ActiveRecord::VERSION::MINOR == 0
|
89
99
|
end
|
90
100
|
|
101
|
+
def active_record61?
|
102
|
+
active_record6? && ::ActiveRecord::VERSION::MINOR == 1
|
103
|
+
end
|
104
|
+
|
105
|
+
def active_record70?
|
106
|
+
active_record7? && ::ActiveRecord::VERSION::MINOR == 0
|
107
|
+
end
|
108
|
+
|
91
109
|
def mongoid4x?
|
92
110
|
mongoid? && ::Mongoid::VERSION =~ /\A4/
|
93
111
|
end
|
@@ -3,13 +3,16 @@
|
|
3
3
|
module Bullet
|
4
4
|
module Detector
|
5
5
|
class Association < Base
|
6
|
-
class <<self
|
6
|
+
class << self
|
7
7
|
def add_object_associations(object, associations)
|
8
8
|
return unless Bullet.start?
|
9
9
|
return if !Bullet.n_plus_one_query_enable? && !Bullet.unused_eager_loading_enable?
|
10
10
|
return unless object.bullet_primary_key_value
|
11
11
|
|
12
|
-
Bullet.debug(
|
12
|
+
Bullet.debug(
|
13
|
+
'Detector::Association#add_object_associations',
|
14
|
+
"object: #{object.bullet_key}, associations: #{associations}"
|
15
|
+
)
|
13
16
|
object_associations.add(object.bullet_key, associations)
|
14
17
|
end
|
15
18
|
|
@@ -18,7 +21,10 @@ module Bullet
|
|
18
21
|
return if !Bullet.n_plus_one_query_enable? && !Bullet.unused_eager_loading_enable?
|
19
22
|
return unless object.bullet_primary_key_value
|
20
23
|
|
21
|
-
Bullet.debug(
|
24
|
+
Bullet.debug(
|
25
|
+
'Detector::Association#add_call_object_associations',
|
26
|
+
"object: #{object.bullet_key}, associations: #{associations}"
|
27
|
+
)
|
22
28
|
call_object_associations.add(object.bullet_key, associations)
|
23
29
|
end
|
24
30
|
|
@@ -40,33 +46,33 @@ module Bullet
|
|
40
46
|
|
41
47
|
private
|
42
48
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
49
|
+
# object_associations keep the object relationships
|
50
|
+
# that the object has many associations.
|
51
|
+
# e.g. { "Post:1" => [:comments] }
|
52
|
+
# the object_associations keep all associations that may be or may no be
|
53
|
+
# unpreload associations or unused preload associations.
|
48
54
|
def object_associations
|
49
55
|
Thread.current[:bullet_object_associations]
|
50
56
|
end
|
51
57
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
58
|
+
# call_object_associations keep the object relationships
|
59
|
+
# that object.associations is called.
|
60
|
+
# e.g. { "Post:1" => [:comments] }
|
61
|
+
# they are used to detect unused preload associations.
|
56
62
|
def call_object_associations
|
57
63
|
Thread.current[:bullet_call_object_associations]
|
58
64
|
end
|
59
65
|
|
60
|
-
|
61
|
-
|
62
|
-
|
66
|
+
# inversed_objects keeps object relationships
|
67
|
+
# that association is inversed.
|
68
|
+
# e.g. { "Comment:1" => ["post"] }
|
63
69
|
def inversed_objects
|
64
70
|
Thread.current[:bullet_inversed_objects]
|
65
71
|
end
|
66
72
|
|
67
|
-
|
68
|
-
|
69
|
-
|
73
|
+
# eager_loadings keep the object relationships
|
74
|
+
# that the associations are preloaded by find :include.
|
75
|
+
# e.g. { ["Post:1", "Post:2"] => [:comments, :user] }
|
70
76
|
def eager_loadings
|
71
77
|
Thread.current[:bullet_eager_loadings]
|
72
78
|
end
|
@@ -3,26 +3,30 @@
|
|
3
3
|
module Bullet
|
4
4
|
module Detector
|
5
5
|
class CounterCache < Base
|
6
|
-
class <<self
|
6
|
+
class << self
|
7
7
|
def add_counter_cache(object, associations)
|
8
8
|
return unless Bullet.start?
|
9
9
|
return unless Bullet.counter_cache_enable?
|
10
10
|
return unless object.bullet_primary_key_value
|
11
11
|
|
12
|
-
Bullet.debug(
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
Bullet.debug(
|
13
|
+
'Detector::CounterCache#add_counter_cache',
|
14
|
+
"object: #{object.bullet_key}, associations: #{associations}"
|
15
|
+
)
|
16
|
+
create_notification object.class.to_s, associations if conditions_met?(object, associations)
|
16
17
|
end
|
17
18
|
|
18
19
|
def add_possible_objects(object_or_objects)
|
19
20
|
return unless Bullet.start?
|
20
21
|
return unless Bullet.counter_cache_enable?
|
21
22
|
|
22
|
-
objects = Array(object_or_objects)
|
23
|
+
objects = Array.wrap(object_or_objects)
|
23
24
|
return if objects.map(&:bullet_primary_key_value).compact.empty?
|
24
25
|
|
25
|
-
Bullet.debug(
|
26
|
+
Bullet.debug(
|
27
|
+
'Detector::CounterCache#add_possible_objects',
|
28
|
+
"objects: #{objects.map(&:bullet_key).join(', ')}"
|
29
|
+
)
|
26
30
|
objects.each { |object| possible_objects.add object.bullet_key }
|
27
31
|
end
|
28
32
|
|
@@ -50,7 +54,7 @@ module Bullet
|
|
50
54
|
private
|
51
55
|
|
52
56
|
def create_notification(klazz, associations)
|
53
|
-
notify_associations = Array(associations) - Bullet.
|
57
|
+
notify_associations = Array.wrap(associations) - Bullet.get_safelist_associations(:counter_cache, klazz)
|
54
58
|
|
55
59
|
if notify_associations.present?
|
56
60
|
notice = Bullet::Notification::CounterCache.new klazz, notify_associations
|
@@ -6,8 +6,8 @@ module Bullet
|
|
6
6
|
extend Dependency
|
7
7
|
extend StackTraceFilter
|
8
8
|
|
9
|
-
class <<self
|
10
|
-
# executed when object.
|
9
|
+
class << self
|
10
|
+
# executed when object.associations is called.
|
11
11
|
# first, it keeps this method call for object.association.
|
12
12
|
# then, it checks if this associations call is unpreload.
|
13
13
|
# if it is, keeps this unpreload associations and caller.
|
@@ -19,7 +19,10 @@ module Bullet
|
|
19
19
|
|
20
20
|
add_call_object_associations(object, associations)
|
21
21
|
|
22
|
-
Bullet.debug(
|
22
|
+
Bullet.debug(
|
23
|
+
'Detector::NPlusOneQuery#call_association',
|
24
|
+
"object: #{object.bullet_key}, associations: #{associations}"
|
25
|
+
)
|
23
26
|
if !excluded_stacktrace_path? && conditions_met?(object, associations)
|
24
27
|
Bullet.debug('detect n + 1 query', "object: #{object.bullet_key}, associations: #{associations}")
|
25
28
|
create_notification caller_in_project, object.class.to_s, associations
|
@@ -30,10 +33,14 @@ module Bullet
|
|
30
33
|
return unless Bullet.start?
|
31
34
|
return unless Bullet.n_plus_one_query_enable?
|
32
35
|
|
33
|
-
objects = Array(object_or_objects)
|
36
|
+
objects = Array.wrap(object_or_objects)
|
34
37
|
return if objects.map(&:bullet_primary_key_value).compact.empty?
|
38
|
+
return if objects.all? { |obj| obj.class.name =~ /^HABTM_/ }
|
35
39
|
|
36
|
-
Bullet.debug(
|
40
|
+
Bullet.debug(
|
41
|
+
'Detector::NPlusOneQuery#add_possible_objects',
|
42
|
+
"objects: #{objects.map(&:bullet_key).join(', ')}"
|
43
|
+
)
|
37
44
|
objects.each { |object| possible_objects.add object.bullet_key }
|
38
45
|
end
|
39
46
|
|
@@ -51,7 +58,10 @@ module Bullet
|
|
51
58
|
return unless Bullet.n_plus_one_query_enable?
|
52
59
|
return unless object.bullet_primary_key_value
|
53
60
|
|
54
|
-
Bullet.debug(
|
61
|
+
Bullet.debug(
|
62
|
+
'Detector::NPlusOneQuery#add_inversed_object',
|
63
|
+
"object: #{object.bullet_key}, association: #{association}"
|
64
|
+
)
|
55
65
|
inversed_objects.add object.bullet_key, association
|
56
66
|
end
|
57
67
|
|
@@ -72,9 +82,9 @@ module Bullet
|
|
72
82
|
def association?(object, associations)
|
73
83
|
value = object_associations[object.bullet_key]
|
74
84
|
value&.each do |v|
|
75
|
-
|
76
|
-
|
77
|
-
|
85
|
+
# associations == v comparison order is important here because
|
86
|
+
# v variable might be a squeel node where :== method is redefined,
|
87
|
+
# so it does not compare values at all and return unexpected results
|
78
88
|
result = v.is_a?(Hash) ? v.key?(associations) : associations == v
|
79
89
|
return true if result
|
80
90
|
end
|
@@ -85,7 +95,7 @@ module Bullet
|
|
85
95
|
private
|
86
96
|
|
87
97
|
def create_notification(callers, klazz, associations)
|
88
|
-
notify_associations = Array(associations) - Bullet.
|
98
|
+
notify_associations = Array.wrap(associations) - Bullet.get_safelist_associations(:n_plus_one_query, klazz)
|
89
99
|
|
90
100
|
if notify_associations.present?
|
91
101
|
notice = Bullet::Notification::NPlusOneQuery.new(callers, klazz, notify_associations)
|
@@ -6,11 +6,11 @@ module Bullet
|
|
6
6
|
extend Dependency
|
7
7
|
extend StackTraceFilter
|
8
8
|
|
9
|
-
class <<self
|
9
|
+
class << self
|
10
10
|
# check if there are unused preload associations.
|
11
11
|
# get related_objects from eager_loadings associated with object and associations
|
12
12
|
# get call_object_association from associations of call_object_associations whose object is in related_objects
|
13
|
-
# if association not in call_object_association, then the object => association - call_object_association is ununsed preload
|
13
|
+
# if association not in call_object_association, then the object => association - call_object_association is ununsed preload associations
|
14
14
|
def check_unused_preload_associations
|
15
15
|
return unless Bullet.start?
|
16
16
|
return unless Bullet.unused_eager_loading_enable?
|
@@ -29,7 +29,10 @@ module Bullet
|
|
29
29
|
return unless Bullet.unused_eager_loading_enable?
|
30
30
|
return if objects.map(&:bullet_primary_key_value).compact.empty?
|
31
31
|
|
32
|
-
Bullet.debug(
|
32
|
+
Bullet.debug(
|
33
|
+
'Detector::UnusedEagerLoading#add_eager_loadings',
|
34
|
+
"objects: #{objects.map(&:bullet_key).join(', ')}, associations: #{associations}"
|
35
|
+
)
|
33
36
|
bullet_keys = objects.map(&:bullet_key)
|
34
37
|
|
35
38
|
to_add = []
|
@@ -62,7 +65,7 @@ module Bullet
|
|
62
65
|
private
|
63
66
|
|
64
67
|
def create_notification(callers, klazz, associations)
|
65
|
-
notify_associations = Array(associations) - Bullet.
|
68
|
+
notify_associations = Array.wrap(associations) - Bullet.get_safelist_associations(:unused_eager_loading, klazz)
|
66
69
|
|
67
70
|
if notify_associations.present?
|
68
71
|
notice = Bullet::Notification::UnusedEagerLoading.new(callers, klazz, notify_associations)
|
data/lib/bullet/mongoid4x.rb
CHANGED
@@ -23,7 +23,7 @@ module Bullet
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def each(&block)
|
26
|
-
return to_enum unless
|
26
|
+
return to_enum unless block
|
27
27
|
|
28
28
|
records = []
|
29
29
|
origin_each { |record| records << record }
|
@@ -37,9 +37,7 @@ module Bullet
|
|
37
37
|
|
38
38
|
def eager_load(docs)
|
39
39
|
associations = criteria.inclusions.map(&:name)
|
40
|
-
docs.each
|
41
|
-
Bullet::Detector::NPlusOneQuery.add_object_associations(doc, associations)
|
42
|
-
end
|
40
|
+
docs.each { |doc| Bullet::Detector::NPlusOneQuery.add_object_associations(doc, associations) }
|
43
41
|
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(docs, associations)
|
44
42
|
origin_eager_load(docs)
|
45
43
|
end
|
@@ -50,9 +48,7 @@ module Bullet
|
|
50
48
|
|
51
49
|
def get_relation(name, metadata, object, reload = false)
|
52
50
|
result = origin_get_relation(name, metadata, object, reload)
|
53
|
-
if metadata.macro !~ /embed/
|
54
|
-
Bullet::Detector::NPlusOneQuery.call_association(self, name)
|
55
|
-
end
|
51
|
+
Bullet::Detector::NPlusOneQuery.call_association(self, name) if metadata.macro !~ /embed/
|
56
52
|
result
|
57
53
|
end
|
58
54
|
end
|
data/lib/bullet/mongoid5x.rb
CHANGED
@@ -23,7 +23,7 @@ module Bullet
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def each(&block)
|
26
|
-
return to_enum unless
|
26
|
+
return to_enum unless block
|
27
27
|
|
28
28
|
records = []
|
29
29
|
origin_each { |record| records << record }
|
@@ -37,9 +37,7 @@ module Bullet
|
|
37
37
|
|
38
38
|
def eager_load(docs)
|
39
39
|
associations = criteria.inclusions.map(&:name)
|
40
|
-
docs.each
|
41
|
-
Bullet::Detector::NPlusOneQuery.add_object_associations(doc, associations)
|
42
|
-
end
|
40
|
+
docs.each { |doc| Bullet::Detector::NPlusOneQuery.add_object_associations(doc, associations) }
|
43
41
|
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(docs, associations)
|
44
42
|
origin_eager_load(docs)
|
45
43
|
end
|
@@ -50,9 +48,7 @@ module Bullet
|
|
50
48
|
|
51
49
|
def get_relation(name, metadata, object, reload = false)
|
52
50
|
result = origin_get_relation(name, metadata, object, reload)
|
53
|
-
if metadata.macro !~ /embed/
|
54
|
-
Bullet::Detector::NPlusOneQuery.call_association(self, name)
|
55
|
-
end
|
51
|
+
Bullet::Detector::NPlusOneQuery.call_association(self, name) if metadata.macro !~ /embed/
|
56
52
|
result
|
57
53
|
end
|
58
54
|
end
|
data/lib/bullet/mongoid6x.rb
CHANGED
@@ -23,7 +23,7 @@ module Bullet
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def each(&block)
|
26
|
-
return to_enum unless
|
26
|
+
return to_enum unless block
|
27
27
|
|
28
28
|
records = []
|
29
29
|
origin_each { |record| records << record }
|
@@ -37,9 +37,7 @@ module Bullet
|
|
37
37
|
|
38
38
|
def eager_load(docs)
|
39
39
|
associations = criteria.inclusions.map(&:name)
|
40
|
-
docs.each
|
41
|
-
Bullet::Detector::NPlusOneQuery.add_object_associations(doc, associations)
|
42
|
-
end
|
40
|
+
docs.each { |doc| Bullet::Detector::NPlusOneQuery.add_object_associations(doc, associations) }
|
43
41
|
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(docs, associations)
|
44
42
|
origin_eager_load(docs)
|
45
43
|
end
|
@@ -50,9 +48,7 @@ module Bullet
|
|
50
48
|
|
51
49
|
def get_relation(name, metadata, object, reload = false)
|
52
50
|
result = origin_get_relation(name, metadata, object, reload)
|
53
|
-
if metadata.macro !~ /embed/
|
54
|
-
Bullet::Detector::NPlusOneQuery.call_association(self, name)
|
55
|
-
end
|
51
|
+
Bullet::Detector::NPlusOneQuery.call_association(self, name) if metadata.macro !~ /embed/
|
56
52
|
result
|
57
53
|
end
|
58
54
|
end
|
data/lib/bullet/mongoid7x.rb
CHANGED
@@ -25,21 +25,36 @@ module Bullet
|
|
25
25
|
def each(&block)
|
26
26
|
return to_enum unless block_given?
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
28
|
+
first_document = nil
|
29
|
+
document_count = 0
|
30
|
+
|
31
|
+
origin_each do |document|
|
32
|
+
document_count += 1
|
33
|
+
|
34
|
+
if document_count == 1
|
35
|
+
first_document = document
|
36
|
+
elsif document_count == 2
|
37
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects([first_document, document])
|
38
|
+
yield(first_document)
|
39
|
+
first_document = nil
|
40
|
+
yield(document)
|
41
|
+
else
|
42
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(document)
|
43
|
+
yield(document)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
if document_count == 1
|
48
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(first_document)
|
49
|
+
yield(first_document)
|
34
50
|
end
|
35
|
-
|
51
|
+
|
52
|
+
self
|
36
53
|
end
|
37
54
|
|
38
55
|
def eager_load(docs)
|
39
56
|
associations = criteria.inclusions.map(&:name)
|
40
|
-
docs.each
|
41
|
-
Bullet::Detector::NPlusOneQuery.add_object_associations(doc, associations)
|
42
|
-
end
|
57
|
+
docs.each { |doc| Bullet::Detector::NPlusOneQuery.add_object_associations(doc, associations) }
|
43
58
|
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(docs, associations)
|
44
59
|
origin_eager_load(docs)
|
45
60
|
end
|
@@ -50,9 +65,7 @@ module Bullet
|
|
50
65
|
|
51
66
|
def get_relation(name, association, object, reload = false)
|
52
67
|
result = origin_get_relation(name, association, object, reload)
|
53
|
-
unless association.embedded?
|
54
|
-
Bullet::Detector::NPlusOneQuery.call_association(self, name)
|
55
|
-
end
|
68
|
+
Bullet::Detector::NPlusOneQuery.call_association(self, name) unless association.embedded?
|
56
69
|
result
|
57
70
|
end
|
58
71
|
end
|
@@ -8,7 +8,8 @@ module Bullet
|
|
8
8
|
|
9
9
|
def initialize(base_class, association_or_associations, path = nil)
|
10
10
|
@base_class = base_class
|
11
|
-
@associations =
|
11
|
+
@associations =
|
12
|
+
association_or_associations.is_a?(Array) ? association_or_associations : [association_or_associations]
|
12
13
|
@path = path
|
13
14
|
end
|
14
15
|
|
@@ -25,16 +26,16 @@ module Bullet
|
|
25
26
|
end
|
26
27
|
|
27
28
|
def whoami
|
28
|
-
@user ||=
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
29
|
+
@user ||=
|
30
|
+
ENV['USER'].presence ||
|
31
|
+
(
|
32
|
+
begin
|
33
|
+
`whoami`.chomp
|
34
|
+
rescue StandardError
|
35
|
+
''
|
36
|
+
end
|
37
|
+
)
|
38
|
+
@user.present? ? "user: #{@user}" : ''
|
38
39
|
end
|
39
40
|
|
40
41
|
def body_with_caller
|
@@ -54,12 +55,7 @@ module Bullet
|
|
54
55
|
end
|
55
56
|
|
56
57
|
def notification_data
|
57
|
-
{
|
58
|
-
user: whoami,
|
59
|
-
url: url,
|
60
|
-
title: title,
|
61
|
-
body: body_with_caller
|
62
|
-
}
|
58
|
+
{ user: whoami, url: url, title: title, body: body_with_caller }
|
63
59
|
end
|
64
60
|
|
65
61
|
def eql?(other)
|
@@ -77,7 +73,7 @@ module Bullet
|
|
77
73
|
end
|
78
74
|
|
79
75
|
def associations_str
|
80
|
-
"
|
76
|
+
".includes(#{@associations.map { |a| a.to_s.to_sym }.inspect})"
|
81
77
|
end
|
82
78
|
end
|
83
79
|
end
|
@@ -10,7 +10,7 @@ module Bullet
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def body
|
13
|
-
"#{klazz_associations_str}\n Add to your
|
13
|
+
"#{klazz_associations_str}\n Add to your query: #{associations_str}"
|
14
14
|
end
|
15
15
|
|
16
16
|
def title
|
@@ -18,9 +18,7 @@ module Bullet
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def notification_data
|
21
|
-
super.merge(
|
22
|
-
backtrace: []
|
23
|
-
)
|
21
|
+
super.merge(backtrace: [])
|
24
22
|
end
|
25
23
|
|
26
24
|
protected
|
@@ -10,7 +10,7 @@ module Bullet
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def body
|
13
|
-
"#{klazz_associations_str}\n Remove from your
|
13
|
+
"#{klazz_associations_str}\n Remove from your query: #{associations_str}"
|
14
14
|
end
|
15
15
|
|
16
16
|
def title
|
@@ -18,9 +18,7 @@ module Bullet
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def notification_data
|
21
|
-
super.merge(
|
22
|
-
backtrace: []
|
23
|
-
)
|
21
|
+
super.merge(backtrace: [])
|
24
22
|
end
|
25
23
|
|
26
24
|
protected
|
data/lib/bullet/notification.rb
CHANGED