Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Tristan

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Rails Debug Popup - Makes easy to debug info.

   1  
   2  module DebugHelper
   3    
   4    def self.append_features(controller) #:nodoc:
   5      controller.ancestors.include?(ActionController::Base) ? controller.add_template_helper(self) : super
   6    end
   7      
   8    IGNORE = ["template_root", "template_class", "response", "template", "session", "url", "params", "subcategories", "ignore_missing_templates", "cookies", "request", "logger", "flash", "headers" ]
   9  
  10    
  11    def debug_popup
  12  
  13      popup_create do |script| 
  14        
  15        script << add( "<HTML><HEAD><TITLE>Smarty Debug Console_#{@controller.class.name}</TITLE></HEAD><BODY bgcolor=#ffffff>" )
  16  
  17        script << add( "<table border=0 width=100%>" )
  18        script << add( "<tr bgcolor=#cccccc><th colspan=2>Rails Debug Console</th></tr>" )
  19        
  20        script << add( "<tr bgcolor=#cccccc><td colspan=2><b>assigned template variables:</b></td></tr>" )      
  21        @controller.assigns.each do |key, value|
  22            script << add ("<tr bgcolor=#eeeeee><td valign=top><tt><font color=blue>#{h key}</font></tt></td><td><tt><font color=green>#{dump_obj(value)}</font></tt></td></tr>")  unless IGNORE.include?(key)
  23        end unless  @controller.assigns.nil?
  24  
  25  
  26        script << add( "<tr bgcolor=#cccccc><td colspan=2><b>request parameters:</b></td></tr>" )
  27        @controller.params.each do |key, value|
  28            script << add ("<tr bgcolor=#eeeeee><td valign=top><tt><font color=blue>#{h key}</font></tt></td><td><tt><font color=green>#{dump_obj(value)}</font></tt></td></tr>")  unless IGNORE.include?(key)          
  29        end unless  @controller.params.nil?
  30  
  31  
  32        script << add( "<tr bgcolor=#cccccc><td colspan=2><b>session variables:</b></td></tr>" )
  33        @controller.session.instance_variable_get("@data").each do |key, value|      
  34            script << add ("<tr bgcolor=#eeeeee><td valign=top><tt><font color=blue>#{h key}</font></tt></td><td><tt><font color=green>#{dump_obj(value)}</font></tt></td></tr>")  unless IGNORE.include?(key)          
  35        end unless  @controller.session.instance_variable_get("@data").nil?
  36  
  37  
  38        script << add( "<tr bgcolor=#cccccc><td colspan=2><b>flash variables:</b></td></tr>" )
  39        @controller.instance_variable_get("@flash").each do |key, value|
  40            script << add ("<tr bgcolor=#eeeeee><td valign=top><tt><font color=blue>#{h key}</font></tt></td><td><tt><font color=green>#{dump_obj(value)}</font></tt></td></tr>")  unless IGNORE.include?(key)
  41        end unless @controller.instance_variable_get("@flash").nil?
  42          
  43      end
  44    
  45    end
  46  
  47    private
  48    
  49    def popup_create
  50      
  51      script = "<SCRIPT language=javascript>\n<!--\n"
  52      script << "_rails_console = window.open(\"\",\"#{@controller.class.name}\",\"width=680,height=600,resizable,scrollbars=yes\");\n"
  53      yield script
  54      script << "_rails_console.document.close();\n"
  55      script << "-->\n</SCRIPT>"
  56    
  57    end
  58    
  59    
  60    def add(msg)
  61      "_rails_console.document.write(\"#{msg}\")\n"
  62    end  
  63    
  64    def dump_obj(object)
  65      begin
  66        Marshal::dump(object)
  67        "<pre>#{h(object.to_yaml).gsub("  ", "&nbsp; ").gsub("\n", "<br/>\"+\n\"" )}</pre>"
  68      rescue Object => e
  69        # Object couldn't be dumped, perhaps because of singleton methods -- this is the fallback
  70        "<pre>#{h(object.inspect)}</pre>"
  71      end
  72    end
  73  
  74    
  75  end


I got this from EpiLog after seeing it mentioned on on the HowToDebugViews wiki. It is a helper class which will raise a Smarty like popup dumping all assigns, sessions, parameter and flash. Install it into your helper directory and include DebugHelper in your application controller. In your view (perhaps the bottom of your generic layout) add <%= debug_popup %>. Make sure to disable your popup blocker.

This is especially useful if you work together with designers who have no idea what kind of information are exported by the controllers in a given view.
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS