renderable.rb 3.0 KB
Newer Older
1
module ActionView
J
Joshua Peek 已提交
2
  module Renderable
3 4
    # NOTE: The template that this mixin is beening include into is frozen
    # So you can not set or modify any instance variables
5

6 7
    extend ActiveSupport::Memoizable

8 9 10 11
    def self.included(base)
      @@mutex = Mutex.new
    end

12 13 14 15
    def filename
      'compiled-template'
    end

16
    def handler
17
      Template.handler_class_for_extension(extension)
18
    end
J
Joshua Peek 已提交
19
    memoize :handler
20

21
    def compiled_source
22
      handler.call(self)
23
    end
J
Joshua Peek 已提交
24
    memoize :compiled_source
25

26
    def render(view, local_assigns = {})
27 28
      compile(local_assigns)

29 30
      view._first_render ||= self
      view._last_render = self
31

32
      view.send(:evaluate_assigns)
33
      view.send(:set_controller_content_type, mime_type) if respond_to?(:mime_type)
34 35 36 37 38 39 40 41

      view.send(method_name(local_assigns), local_assigns) do |*names|
        if proc = view.instance_variable_get("@_proc_for_layout")
          view.capture(*names, &proc)
        else
          view.instance_variable_get("@content_for_#{names.first || 'layout'}")
        end
      end
42 43
    end

44
    def method_name(local_assigns)
45 46 47 48
      if local_assigns && local_assigns.any?
        local_assigns_keys = "locals_#{local_assigns.keys.map { |k| k.to_s }.sort.join('_')}"
      end
      ['_run', extension, method_segment, local_assigns_keys].compact.join('_').to_sym
49 50
    end

51
    private
52
      # Compile and evaluate the template's code (if necessary)
53
      def compile(local_assigns)
54
        render_symbol = method_name(local_assigns)
55

56
        @@mutex.synchronize do
57 58 59 60 61
          if recompile?(render_symbol)
            compile!(render_symbol, local_assigns)
          end
        end
      end
62

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

66 67 68 69 70 71 72
        source = <<-end_src
          def #{render_symbol}(local_assigns)
            old_output_buffer = output_buffer;#{locals_code};#{compiled_source}
          ensure
            self.output_buffer = old_output_buffer
          end
        end_src
73

74
        begin
J
Joshua Peek 已提交
75
          logger = defined?(ActionController) && Base.logger
76
          logger.debug "Compiling template #{render_symbol}" if logger
77

78 79 80 81 82 83
          ActionView::Base::CompiledTemplates.module_eval(source, filename, 0)
        rescue Exception => e # errors from template code
          if logger
            logger.debug "ERROR: compiling #{render_symbol} RAISED #{e}"
            logger.debug "Function body: #{source}"
            logger.debug "Backtrace: #{e.backtrace.join("\n")}"
84
          end
85 86

          raise ActionView::TemplateError.new(self, {}, e)
87 88
        end
      end
89

90 91 92 93
      # Method to check whether template compilation is necessary.
      # The template will be compiled if the file has not been compiled yet, or
      # if local_assigns has a new key, which isn't supported by the compiled code yet.
      def recompile?(symbol)
94
        !(ActionView::PathSet::Path.eager_load_templates? && Base::CompiledTemplates.method_defined?(symbol))
95
      end
96 97
  end
end