Class: RenderSQL::TemplateHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/render_sql/template_handler.rb

Constant Summary collapse

DEFAULT_CSS =
"  <style>\n    table.render-sql-results {\n      border-collapse: collapse;\n      width: 100%;\n    }\n\n    table.render-sql-results th, table.render-sql-results td {\n      border: 1px solid #ccc;\n      padding: 8px;\n    }\n\n    table.render-sql-results th {\n      text-align: left;\n      background-color: #f2f2f2;\n    }\n  </style>\n"
DEFAULT_TEMPLATE =
"  <div class=\"<%= css[:container] %>\">\n    <h1 class=\"<%= css[:heading] %>\"><%= heading %></h1>\n    <table class=\"<%= css[:table] %>\">\n      <thead>\n        <tr>\n          <% result.headers.each do |header| %>\n            <th><%= h(header) %></th>\n          <% end %>\n        </tr>\n      </thead>\n      <tbody>\n        <% result.each do |row| %>\n          <tr>\n            <% row.each do |cell| %>\n              <td><%= h(cell) %></td>\n            <% end %>\n          </tr>\n        <% end %>\n      </tbody>\n    </table>\n  </div>\n"

Class Method Summary collapse

Class Method Details

.call(template, source = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/render_sql/template_handler.rb', line 51

def call(template, source = nil)
  sql = source || template.source

  "    begin\n      config = Rails.application.config.render_sql\n      heading = File.basename(\#{template.short_identifier.inspect}, \".sql\").titleize\n      result = RenderSQL::Query.execute(\#{sql.inspect}, params)\n\n      if config.partial\n        render :partial => config.partial, :locals => { :css => config.css_classes, :result => result }\n      else\n        css = config.css_classes\n        ERB.new(\#{DEFAULT_CSS.inspect + DEFAULT_TEMPLATE.inspect}).result(binding)\n      end\n    rescue ActiveRecord::StatementInvalid => e\n      \"<p style='color: red'>SQL Error: \\\#{h(e.message)}</p>\"\n    end\n  TEMPLATE\nend\n"