-
Notifications
You must be signed in to change notification settings - Fork 431
/
n_plus_one_query.rb
35 lines (32 loc) · 1.28 KB
/
n_plus_one_query.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Bullet
module Detector
class NPlusOneQuery < Association
def self.add_unpreload_associations(klazz, associations)
notice = Bullet::Notification::NPlusOneQuery.new( callers, klazz, associations )
Bullet.add_notification( notice )
end
# executed when object.assocations is called.
# first, it keeps this method call for object.association.
# then, it checks if this associations call is unpreload.
# if it is, keeps this unpreload associations and caller.
def self.call_association(object, associations)
@@checked = true
add_call_object_associations(object, associations)
if unpreload_associations?(object, associations)
add_unpreload_associations(object.class, associations)
caller_in_project
end
end
private
# decide whether the object.associations is unpreloaded or not.
def self.unpreload_associations?(object, associations)
possible?(object) and !impossible?(object) and !association?(object, associations)
end
def self.caller_in_project
vender_root ||= File.join(Rails.root, 'vendor')
callers << caller.select {|c| c =~ /#{Rails.root}/}.reject {|c| c =~ /#{vender_root}/}
callers.uniq!
end
end
end
end