Module: Rack::Bug::Render

Includes:
ERB::Util, CompiledTemplates
Included in:
Panel, PanelApp, RedirectInterceptor, SpeedTrace::Render, Toolbar
Defined in:
lib/rack/bug/render.rb

Defined Under Namespace

Modules: CompiledTemplates

Instance Method Summary collapse

Instance Method Details

#compile(filename, local_assigns) ⇒ Object



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

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



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rack/bug/render.rb', line 31

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

  CompiledTemplates.module_eval(source, filename, 0)
end

#compiled_source(filename) ⇒ Object



45
46
47
# File 'lib/rack/bug/render.rb', line 45

def compiled_source(filename)
  ::ERB.new(::File.read(::File.dirname(__FILE__) + "/../bug/views/#{filename}.html.erb"), nil, "-").src
end

#method_name(filename, local_assigns) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/rack/bug/render.rb', line 49

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



59
60
61
# File 'lib/rack/bug/render.rb', line 59

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

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



17
18
19
20
21
# File 'lib/rack/bug/render.rb', line 17

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



9
10
11
# File 'lib/rack/bug/render.rb', line 9

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