module Vanity::Render
Render
method available to templates (when used by Vanity
command line, outside Rails
).
Public Instance Methods
method_missing(method, *args, &block)
click to toggle source
prevent certain url helper methods from failing so we can run erb templates outside of rails for reports.
Calls superclass method
# File lib/vanity/commands/report.rb, line 36 def method_missing(method, *args, &block) %w(url_for flash).include?(method.to_s) ? ProxyEmpty.new : super end
render(path_or_options, locals = {})
click to toggle source
Render
the named template. Used for reporting and the dashboard.
# File lib/vanity/commands/report.rb, line 11 def render(path_or_options, locals = {}) if path_or_options.respond_to?(:keys) render_erb( path_or_options[:file] || path_or_options[:partial], path_or_options[:locals] ) else render_erb(path_or_options, locals) end end
vanity_h(html)
click to toggle source
Escape HTML.
# File lib/vanity/commands/report.rb, line 23 def vanity_h(html) CGI.escapeHTML(html.to_s) end
vanity_html_safe(text)
click to toggle source
# File lib/vanity/commands/report.rb, line 27 def vanity_html_safe(text) text end
vanity_simple_format(text, options={})
click to toggle source
Dumbed down from Rails' simple_format.
# File lib/vanity/commands/report.rb, line 41 def vanity_simple_format(text, options={}) open = "<p #{options.map { |k,v| "#{k}=\"#{CGI.escapeHTML v}\"" }.join(" ")}>" text = open + text.gsub(/\r\n?/, "\n"). # \r\n and \r -> \n gsub(/\n\n+/, "</p>\n\n#{open}"). # 2+ newline -> paragraph gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') + # 1 newline -> br "</p>" end
Protected Instance Methods
partialize(template_name)
click to toggle source
# File lib/vanity/commands/report.rb, line 64 def partialize(template_name) if template_name[0] != '_' "_#{template_name}" else template_name end end
render_erb(path, locals = {})
click to toggle source
# File lib/vanity/commands/report.rb, line 51 def render_erb(path, locals = {}) locals[:playground] = self keys = locals.keys struct = Struct.new(*keys) struct.send :include, Render locals = struct.new(*locals.values_at(*keys)) dir, base = File.split(path) path = File.join(dir, partialize(base)) erb = ERB.new(File.read("#{path}.erb"), nil, '<>') erb.filename = path erb.result(locals.instance_eval { binding }) end