Module: Vanity::Render

Included in:
Commands
Defined in:
lib/vanity/commands/report.rb

Overview

Render method available to templates (when used by Vanity command line, outside Rails).

Defined Under Namespace

Classes: ProxyEmpty

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

prevent certain url helper methods from failing so we can run erb templates outside of rails for reports.



36
37
38
# File 'lib/vanity/commands/report.rb', line 36

def method_missing(method, *args, &block) # rubocop:todo Style/MissingRespondToMissing
  %w(url_for flash).include?(method.to_s) ? ProxyEmpty.new : super
end

Instance Method Details

#render(path_or_options, locals = {}) ⇒ Object

Render the named template. Used for reporting and the dashboard.



9
10
11
12
13
14
15
16
17
18
# File 'lib/vanity/commands/report.rb', line 9

def render(path_or_options, locals = {})
  if path_or_options.respond_to?(:keys)
    render_erb(
      path_or_options[:template] || path_or_options[:partial],
      path_or_options[:locals]
    )
  else
    render_erb(path_or_options, locals)
  end
end

#vanity_h(html) ⇒ Object

Escape HTML.



21
22
23
# File 'lib/vanity/commands/report.rb', line 21

def vanity_h(html)
  CGI.escapeHTML(html.to_s)
end

#vanity_html_safe(text) ⇒ Object



25
26
27
# File 'lib/vanity/commands/report.rb', line 25

def vanity_html_safe(text)
  text
end

#vanity_simple_format(text, options = {}) ⇒ Object

Dumbed down from Rails’ simple_format.



41
42
43
44
45
46
47
# 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 # rubocop:todo Lint/UselessAssignment
                    .gsub(/\n\n+/, "</p>\n\n#{open}") # 2+ newline  -> paragraph
                    .gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') + # 1 newline   -> br
         "</p>"
end