aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/renderable.rb
blob: 2c4302146fc716d29095d6ccdba67e2c9965c005 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
module ActionView
  module Renderable
    # NOTE: The template that this mixin is beening include into is frozen
    # So you can not set or modify any instance variables

    def self.included(base)
      @@mutex = Mutex.new
    end

    # NOTE: Exception to earlier notice. Ensure this is called before freeze
    def handler
      @handler ||= Template.handler_class_for_extension(extension)
    end

    # NOTE: Exception to earlier notice. Ensure this is called before freeze
    def compiled_source
      @compiled_source ||= handler.new(nil).compile(self) if handler.compilable?
    end

    def render(view, local_assigns = {})
      view.first_render ||= self
      view.send(:evaluate_assigns)
      view.current_render_extension = extension
      compile(local_assigns) if handler.compilable?
      handler.new(view).render(self, local_assigns)
    end

    def method(local_assigns)
      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
    end

    private
      # Compile and evaluate the template's code
      def compile(local_assigns)
        render_symbol = method(local_assigns)

        @@mutex.synchronize do
          return false unless recompile?(render_symbol)

          locals_code = local_assigns.keys.map { |key| "#{key} = local_assigns[:#{key}];" }.join

          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

          begin
            file_name = respond_to?(:filename) ? filename : 'compiled-template'
            ActionView::Base::CompiledTemplates.module_eval(source, file_name, 0)
          rescue Exception => e # errors from template code
            if logger = ActionController::Base.logger
              logger.debug "ERROR: compiling #{render_symbol} RAISED #{e}"
              logger.debug "Function body: #{source}"
              logger.debug "Backtrace: #{e.backtrace.join("\n")}"
            end

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

      # 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)
        unless Base::CompiledTemplates.instance_methods.include?(symbol) && Base.cache_template_loading
          true
        else
          false
        end
      end
  end
end