lib/rails-footnotes/notes/partials_note.rb in rails3-footnotes-4.0.0.pre.2 vs lib/rails-footnotes/notes/partials_note.rb in rails3-footnotes-4.0.0.pre.3
- old
+ new
@@ -1,56 +1,72 @@
require "#{File.dirname(__FILE__)}/log_note"
module Footnotes
module Notes
class PartialsNote < LogNote
+ @@partial_subscriber = nil
+ cattr_accessor :partial_subscriber
+
def initialize(controller)
super
@controller = controller
end
+
+ def self.start!(controller)
+ @@partial_subscriber = Footnotes::Notes::PartialSubscriber.new
+ ActiveSupport::LogSubscriber.attach_to(:action_view, @@partial_subscriber)
+ end
+
def row
:edit
end
+
def title
"Partials (#{partials.size})"
end
+
def content
rows = partials.map do |filename|
href = Footnotes::Filter.prefix(filename,1,1)
shortened_name=filename.gsub(File.join(Rails.root,"app/views/"),"")
- [%{<a href="#{href}">#{shortened_name}</a>},"#{@partial_times[filename].sum}ms",@partial_counts[filename]]
+ [%{<a href="#{href}">#{shortened_name}</a>},"#{@partial_times[filename].sum}ms", @partial_counts[filename]]
end
mount_table(rows.unshift(%w(Partial Time Count)), :summary => "Partials for #{title}")
end
+ def self.load
+ self.loaded = true unless loaded
+ end
+
protected
- #Generate a list of partials that were rendered, also build up render times and counts.
- #This is memoized so we can use its information in the title easily.
- def partials
- @partials ||= begin
- partials = []
- @partial_counts = {}
- @partial_times = {}
- log_lines = log
- log_lines.split("\n").each do |line|
- if line =~ /Rendered (\S*) \(([\d\.]+)\S*?\)/
- partial = $1
- @controller.view_paths.each do |view_path|
- path = File.join(view_path, "#{partial}*")
- files = Dir.glob(path)
- for file in files
- #TODO figure out what format got rendered if theres multiple
- @partial_times[file] ||= []
- @partial_times[file] << $2.to_f
- @partial_counts[file] ||= 0
- @partial_counts[file] += 1
- partials << file unless partials.include?(file)
- end
- end
- end
- end
- partials.reverse
+ #Generate a list of partials that were rendered, also build up render times and counts.
+ #This is memoized so we can use its information in the title easily.
+ def partials
+ @partials ||= begin
+ partials = []
+ @partial_counts = {}
+ @partial_times = {}
+ @@partial_subscriber.events.each do |event|
+ partial = event.payload[:identifier]
+ @partial_times[partial] ||= []
+ @partial_times[partial] << event.duration
+ @partial_counts[partial] ||= 0
+ @partial_counts[partial] += 1
+ partials << partial unless partials.include?(partial)
end
+ partials.reverse
end
- end
+ end
+ end
+ class PartialSubscriber < ActiveSupport::LogSubscriber
+ attr_accessor :events
+ def initialize
+ @events = Array.new
+ super
+ end
+
+ def render_partial(event)
+ @events << event.dup
+ end
+ end
end
end