lib/cat_tree/observer.rb in cat_tree-0.0.1 vs lib/cat_tree/observer.rb in cat_tree-0.0.2

- old
+ new

@@ -14,18 +14,11 @@ def notice(object) return if object.new_record? key = "#{object.class.name}(id:#{object.id})" @ar_base[key] ||= {:count => 0, :callers => []} @ar_base[key][:count] += 1 - if defined?(Rails) - root_path = Rails.root.to_s - root_path += "/" unless root_path.last == "/" - cal = caller.select{|c| c =~ %r!#{root_path}(app|lib)/!} - @ar_base[key][:callers] << cal unless cal.empty? - else - @ar_base[key][:callers] << caller - end + record_backtrace(@ar_base[key][:callers]) if Config.backtrace end def ar_base_count @ar_base.values.inject(0){|t,v| t + v[:count]} end @@ -42,22 +35,36 @@ output_message end private + def record_backtrace(callers) + if defined?(Rails) + root_path = Rails.root.to_s + root_path += "/" unless root_path.last == "/" + cal = caller.select{|c| c =~ %r!#{root_path}(app|lib)/!} + callers << cal unless cal.empty? + else + callers << caller + end + end + def output_message return if @ar_base.empty? msg = ["", "[CatTree]"] msg << " ActiveRecord::Base:\t#{ar_base_count}" unless (same_objects = same_ar_base_objects).empty? msg << " Same objects:" same_objects.keys.sort_by{|k| same_objects[k][:count]}.reverse.each do |key| msg << " #{key}:\t#{same_objects[key][:count]}" - same_objects[key][:callers].each do |cal| - cal.each{|c| msg << " #{c}"} - msg << "" + + if Config.backtrace + same_objects[key][:callers].each do |cal| + cal.each{|c| msg << " #{c}"} + msg << "" + end end end end msg << ""