Class: ActionView::TemplateHandlers::Erector

Inherits:
TemplateHandler
  • Object
show all
Includes:
Compilable
Defined in:
lib/erector/rails/template_handlers/action_view_template_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.line_offsetObject



5
6
7
# File 'lib/erector/rails/template_handlers/action_view_template_handler.rb', line 5

def self.line_offset
  2
end

Instance Method Details

#compile(template) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/erector/rails/template_handlers/action_view_template_handler.rb', line 13

def compile(template)
  relative_path_parts = template.path.split('/')

  is_partial = relative_path_parts.last =~ /^_/
  require_dependency File.expand_path(template.filename)

  widget_class_parts = relative_path_parts.inject(['Views']) do |class_parts, node|
    class_parts << node.gsub(/^_/, "").gsub(/(\.html)?\.rb$/, '').camelize
    class_parts
  end
  widget_class_name = widget_class_parts.join("::")
  render_method = is_partial ? 'render_partial' : 'content'

  erb_template = <<-ERB
  <%
    assigns = instance_variables.inject({}) do |hash, name|
      hash[name.sub('@', "")] = instance_variable_get(name)
      hash
    end

    widget = #{widget_class_name}.new(assigns)
    widget.to_s(:output => output_buffer, :helpers => self, :content_method_name => :#{render_method})
  %>
  ERB
  ::ERB.new(
    erb_template,
    nil,
    ::ActionView::TemplateHandlers::ERB.erb_trim_mode,
    "@output_buffer"
  ).src
end