Peter Donald wrote:> Hi,
>
> I just released a plugin to help debug views. The plugin makes it easy
> to add a button that will popup a new window that displays the
> following debug data;
>
> * Request Parameters
> * Session Variables
> * Flash Variables
> * Assigned Template Variables
>
> It was derived from code that traces back to Marten Veldthuis in
> Epilog. You can check out webpage and plugin via;
>
>
http://www.realityforge.org/articles/2006/03/20/rails-plugin-to-help-debug-views
Nice! I had modified the original to also do inline debugging, so
rather than doing <%= debug_popup %>, you just do <%= debug_inline
%>
I merged my inline stuff with your code (attached), if you want it.
The code is pretty ugly, but it works.
John
-------------- next part --------------
# Add something like the following to get debug output in separate popup
# <botton onclick="show_debug_popup(); return false;">Show
debug popup</button>
# <%= debug_popup %>
module ViewDebugHelper
def debug_popup
popup_create do |script|
script << add("<html><head><title>Rails Debug
Console_#{@controller.class.name}</title></head><body>")
script << add("<style type=''text/css''>
body {background-color:#FFFFFF;} table {width:100%;border: 0;} th
{background-color:#CCCCCC;font-weight: bold;} td {background-color:#EEEEEE;
vertical-align: top;} td.key {color: blue;} td {color:
green;}</style><table><colgroup
id=''key-column''/>" )
popup_header(script,''Rails Debug Console'')
if ! @controller.params.nil?
popup_header(script,''Request Parameters:'')
@controller.params.each do |key, value|
popup_data(script,h(key),h(value.inspect).gsub(/,/,
'',<br/>'')) unless IGNORE.include?(key)
end
end
dump_vars(script,''Session
Variables:'',@controller.instance_variable_get("@data"))
dump_vars(script,''Flash
Variables:'',@controller.instance_variable_get("@flash"))
dump_vars(script,''Assigned Template
Variables:'',@controller.assigns)
script <<
add(''</table></body></html>'')
end
end
def debug_inline
html = "<table border=0 width=60%>"
html << "<tr bgcolor=#cccccc><th colspan=2>Rails
Debug - #{@controller.class.name}</th></tr>"
html << "<tr bgcolor=#cccccc><td
colspan=2><b>assigned template
variables:</b></td></tr>"
@controller.assigns.each do |key, value|
html << "<tr bgcolor=#eeeeee><td
valign=top><tt><font color=blue>#{h
key}</font></tt></td><td><tt><font
color=green>#{dump_obj_inline(value)}</font></tt></td></tr>"
unless IGNORE.include?(key)
end unless @controller.assigns.nil?
html << "<tr bgcolor=#cccccc><td
colspan=2><b>request parameters:</b></td></tr>"
@controller.params.each do |key, value|
html << "<tr bgcolor=#eeeeee><td
valign=top><tt><font color=blue>#{h
key}</font></tt></td><td><tt><font
color=green>#{dump_obj_inline(value)}</font></tt></td></tr>"
unless IGNORE.include?(key)
end unless @controller.params.nil?
html << "<tr bgcolor=#cccccc><td
colspan=2><b>session variables:</b></td></tr>"
@controller.session.instance_variable_get("@data").each do |key,
value|
html << "<tr bgcolor=#eeeeee><td
valign=top><tt><font color=blue>#{h
key}</font></tt></td><td><tt><font
color=green>#{dump_obj_inline(value)}</font></tt></td></tr>"
unless IGNORE.include?(key)
end unless @controller.session.instance_variable_get("@data").nil?
html << "<tr bgcolor=#cccccc><td
colspan=2><b>flash variables:</b></td></tr>"
@controller.instance_variable_get("@flash").each do |key, value|
html << "<tr bgcolor=#eeeeee><td
valign=top><tt><font color=blue>#{h
key}</font></tt></td><td><tt><font
color=green>#{dump_obj_inline(value)}</font></tt></td></tr>"
unless IGNORE.include?(key)
end unless @controller.instance_variable_get("@flash").nil?
return html
end
private
IGNORE = [''template_root'',
''template_class'', ''response'',
''template'', ''session'',
''url'', ''params'',
''variables_added'',
''ignore_missing_templates'', ''cookies'',
''request'', ''logger'',
''flash'', ''headers'' ] unless
const_defined?(:IGNORE)
def dump_vars(script,header,vars)
return if vars.nil?
popup_header(script,header)
vars.each {|k,v| popup_data(script,h(k),dump_obj(v)) unless
IGNORE.include?(k)}
end
def popup_header(script,heading); script << add( "<tr><th
colspan=''2''>#{heading}</th></tr>" ); end
def popup_data(script,key,value); script << add( "<tr><td
class=''key''>#{key}</td><td>#{value}</td></tr>"
); end
def popup_create
script = "<script
language=''javascript''>\n<!--\n"
script << "function show_debug_popup() {\n"
script << "_rails_console =
window.open(\"\",\"#{@controller.class.name}\",\"width=680,height=600,resizable,scrollbars=yes\");\n"
yield script
script << "_rails_console.document.close();\n"
script << "}\n"
script << "-->\n</script>"
end
def add(msg)
"_rails_console.document.write(\"#{msg}\")\n"
end
def dump_obj(object)
begin
Marshal::dump(object)
"<pre>#{h(object.to_yaml).gsub(" ", "
").gsub("\n", "<br/>\"+\n\""
)}</pre>"
rescue Object => e
# Object couldn''t be dumped, perhaps because of singleton methods
-- this is the fallback
"<pre>#{h(object.inspect)}</pre>"
end
end
def dump_obj_inline(object)
begin
Marshal::dump(object)
"<pre>#{h(object.to_yaml).gsub(" ", "
").gsub("\n", "<br/>")}</pre>"
rescue Object => e
# Object couldn''t be dumped, perhaps because of singleton methods
-- this is the fallback
# FIXME - pagination stuff is killing useful output so comment out for now
#"<pre>#{h(object.inspect).gsub("#",
"<br/>")}</pre>"
end
end
end
ActionController::Base.class_eval do
helper :view_debug
end