Module: Rack::Insight::Render

Includes:
ERB::Util, Logging, CompiledTemplates
Included in:
EnableButton, Panel, PanelApp, RedirectInterceptor, SpeedTracer::TraceRecord, Toolbar
Defined in:
lib/rack/insight/render.rb

Defined Under Namespace

Modules: CompiledTemplates

Instance Method Summary collapse

Methods included from Logging

logger, verbose, verbosity

Instance Method Details

#compile(filename, local_assigns) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/rack/insight/render.rb', line 22

def compile(filename, local_assigns)
  render_symbol = method_name(filename, local_assigns)

  if !CompiledTemplates.instance_methods.include?(render_symbol.to_s)
    compile!(filename, local_assigns)
  end
end

#compile!(filename, local_assigns) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rack/insight/render.rb', line 30

def compile!(filename, local_assigns)
  render_symbol = method_name(filename, local_assigns)
  locals_code = local_assigns.keys.map { |key| "#{key} = local_assigns[:#{key}];" }.join

  source = <<-end_src
  def #{render_symbol}(local_assigns)
    #{locals_code}
    #{compiled_source(filename)}
  end
  end_src

  begin
    CompiledTemplates.module_eval(source, filename, 0)
  rescue StandardError => ex
    logger.error do
      "#{ex.class.name}: #{ex.message} in\n" +
      source +
        ex.backtrace.join("\n")
    end
  end
end

#compiled_source(filename) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rack/insight/render.rb', line 52

def compiled_source(filename)
  templates = []
  templates << ::File.join(::File.dirname(__FILE__), "views/#{filename}.html.erb")
  if self.class.respond_to?(:template_root) && !self.class.template_root.nil?
    # Push onto the front of the array to try because if there is a template root it is the most likely place to find the view.
    templates.unshift(::File.join(::File.join(self.class.template_root, "#{filename}.html.erb")))
  end
  file = nil
  templates.each do |template_path|
    begin
      file = ::File.read(template_path)
      break # If no error is raised then the file was read!
    rescue Errno::ENOENT
    end
  end
  if file
    ::ERB.new(file, nil, "-").src
  else
    logger.fatal("Rack::Insight: Unable to find expected view template #{filename} in any of the following locations: #{templates.inspect}")
  end
end

#method_name(filename, local_assigns) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/rack/insight/render.rb', line 74

def method_name(filename, local_assigns)
  if local_assigns && local_assigns.any?
    method_name = method_name_without_locals(filename).dup
    method_name << "_locals_#{local_assigns.keys.map { |k| k.to_s }.sort.join('_')}"
  else
    method_name = method_name_without_locals(filename)
  end
  method_name.to_sym
end

#method_name_without_locals(filename) ⇒ Object



84
85
86
# File 'lib/rack/insight/render.rb', line 84

def method_name_without_locals(filename)
  filename.split("/").join("_").tr("-", "_")
end

#render_template(filename, local_assigns = {}) ⇒ Object



16
17
18
19
20
# File 'lib/rack/insight/render.rb', line 16

def render_template(filename, local_assigns = {})
  compile(filename, local_assigns)
  render_symbol = method_name(filename, local_assigns)
  send(render_symbol, local_assigns)
end

#signed_params(hash) ⇒ Object



8
9
10
# File 'lib/rack/insight/render.rb', line 8

def signed_params(hash)
  ParamsSignature.sign(request, hash)
end