lib/bullet/association.rb in flyerhzm-bullet-1.2.0 vs lib/bullet/association.rb in flyerhzm-bullet-1.3.0
- old
+ new
@@ -22,10 +22,11 @@
@@unused_preload_associations = nil
@@callers = nil
@@possible_objects = nil
@@impossible_objects = nil
@@call_object_associations = nil
+ @@eager_loadings = nil
end
def alert=(alert)
@@alert = alert
end
@@ -62,12 +63,14 @@
@@rails_logger = rails_logger
end
def check_unused_preload_associations
object_associations.each do |object, association|
- call_object_association = call_object_associations[object] || []
- add_unused_preload_associations(object.class, association - call_object_association) unless (association - call_object_association).empty?
+ related_objects = eager_loadings.select {|key, value| key.include?(object) and value == association}.collect(&:first).flatten
+ call_object_association = related_objects.collect { |related_object| call_object_associations[related_object] }.compact.flatten.uniq
+ diff_object_association = (association - call_object_association).reject {|a| a.is_a? Hash}
+ add_unused_preload_associations(object.class, diff_object_association) unless diff_object_association.empty?
end
end
def has_bad_assocations?
check_unused_preload_associations
@@ -86,15 +89,15 @@
str = ''
if @@alert || @@console || @@growl
response = []
if has_unused_preload_associations?
response.push("Unused eager loadings detected:\n")
- response.push(*@@unused_preload_associations.to_a.collect{|klazz, associations| klazz_associations_str(klazz, associations)}.join('\n'))
+ response.push(*@@unused_preload_associations.to_a.collect{|klazz, associations| klazz_associations_str(klazz, associations)}.join("\n"))
end
if has_unpreload_associations?
response.push("#{"\n" unless response.empty?}N+1 queries detected:\n")
- response.push(*@@unpreload_associations.to_a.collect{|klazz, associations| " #{klazz} => [#{associations.map(&:inspect).join(', ')}]"}.join('\n'))
+ response.push(*@@unpreload_associations.to_a.collect{|klazz, associations| " #{klazz} => [#{associations.map(&:inspect).join(', ')}]"}.join("\n"))
end
end
if @@alert
str << wrap_js_association("alert(#{response.join("\n").inspect});")
end
@@ -141,11 +144,11 @@
end
end
def bad_associations_str(bad_associations)
# puts bad_associations.inspect
- bad_associations.to_a.collect{|klazz, associations| klazz_associations_str(klazz, associations)}.join('\\n')
+ bad_associations.to_a.collect{|klazz, associations| klazz_associations_str(klazz, associations)}.join("\n")
end
def klazz_associations_str(klazz, associations)
" #{klazz} => [#{associations.map(&:inspect).join(', ')}]"
end
@@ -227,10 +230,18 @@
# puts "define associations, #{klazz} => #{associations.inspect}"
klazz_associations[klazz] ||= []
klazz_associations[klazz] << associations
unique(klazz_associations[klazz])
end
+
+ def add_eager_loadings(objects, associations)
+ # puts "add eager loadings, #{objects.inspect} => #{associations.inspect}"
+ objects = Array(objects)
+ eager_loadings[objects] ||= []
+ eager_loadings[objects] << associations
+ unique(eager_loadings[objects])
+ end
def unique(array)
array.flatten!
array.uniq!
end
@@ -259,9 +270,13 @@
@@impossible_objects ||= {}
end
def klazz_associations
@@klazz_associations ||= {}
+ end
+
+ def eager_loadings
+ @@eager_loadings ||= {}
end
VENDOR_ROOT = File.join(RAILS_ROOT, 'vendor')
def caller_in_project
callers << caller.select {|c| c =~ /#{RAILS_ROOT}/}.reject {|c| c =~ /#{VENDOR_ROOT}/}