Sha256: 9405f5dacaae07903c4aa6c65c4f7e27143b3fc31c8c87b431db7153c651ccea

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

module ActionView #:nodoc:
  module TemplateHandlers #:nodoc:
    class Erector < TemplateHandler
      include Compilable
      def self.line_offset
        2
      end

      ActionView::Template.instance_eval do
        register_template_handler :rb, ActionView::TemplateHandlers::Erector
      end

      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$/, '').classify
          class_parts
        end
        widget_class_name = widget_class_parts.join("::")
        render_method = is_partial ? 'render_partial' : 'render'

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
erector-0.5.0 lib/erector/rails/template_handlers/2.2.0/action_view_template_handler.rb