forked from indirect/rails-footnotes
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merged in partials footnote from coreymartealla
- Loading branch information
Showing
2 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module Footnotes | ||
module Notes | ||
class PartialsNote < LogNote | ||
def initialize(controller) | ||
super | ||
@controller = controller | ||
end | ||
def row | ||
:edit | ||
end | ||
def title | ||
"Partials (#{partials.size})" | ||
end | ||
def content | ||
links = partials.map do |file| | ||
href = Footnotes::Filter.prefix(file,1,1) | ||
"<tr><td><a href=\"#{href}\">#{file.gsub(File.join(Rails.root,"app/views/"),"")}</td><td>#{@partial_times[file].sum}ms</a></td><td>#{@partial_counts[file]}</td></tr>" | ||
end | ||
"<table><thead><tr><th>Partial</th><th>Time</th><th>Count</th></tr></thead><tbody>#{links.join}</tbody></table>" | ||
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_tail | ||
log_lines.split("\n").each do |line| | ||
if line =~ /Rendered (\S*) \(([\d\.]+)ms\)/ | ||
partial = $1 | ||
files = Dir.glob("#{Rails.root}/app/views/#{partial}*") | ||
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 | ||
partials.reverse | ||
end | ||
end | ||
end | ||
end | ||
end |