aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/template_handlers/compilable.rb
blob: 887c7835378a4348b8b1b955bdc6f184b3303d9a (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
module ActionView
  module TemplateHandlers
    module Compilable
      def self.included(base)
        base.extend ClassMethod

        # Map method names to the names passed in local assigns so far
        base.cattr_accessor :template_args
        base.template_args = {}
      end

      module ClassMethod
        # If a handler is mixin this module, set compilable to true
        def compilable?
          true
        end
      end

      def render(template)
        @view.send :execute, template
      end

      # Compile and evaluate the template's code
      def compile_template(template)
        return false unless compile_template?(template)

        render_symbol = assign_method_name(template)
        render_source = create_template_source(template, render_symbol)

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

          raise ActionView::TemplateError.new(template, @view.assigns, e)
        end
      end

      private
        # Method to check whether template compilation is necessary.
        # The template will be compiled if the inline template or file has not been compiled yet,
        # if local_assigns has a new key, which isn't supported by the compiled code yet.
        def compile_template?(template)
          # Unless the template has been complied yet, compile
          return true unless render_symbol = @view.method_names[template.method_key]

          # If template caching is disabled, compile
          return true unless Base.cache_template_loading

          # Always recompile inline templates
          return true if template.is_a?(InlineTemplate)

          # Unless local assigns support, recompile
          return true unless supports_local_assigns?(render_symbol, template.locals)

          # Otherwise, use compiled method
          return false
        end

        def assign_method_name(template)
          @view.method_names[template.method_key] ||= template.method_name
        end

        # Method to create the source code for a given template.
        def create_template_source(template, render_symbol)
          body = compile(template)

          self.template_args[render_symbol] ||= {}
          locals_keys = self.template_args[render_symbol].keys | template.locals.keys
          self.template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }

          locals_code = ""
          locals_keys.each do |key|
            locals_code << "#{key} = local_assigns[:#{key}];"
          end

          source = <<-end_src
            def #{render_symbol}(local_assigns)
              old_output_buffer = output_buffer;#{locals_code};#{body}
            ensure
              self.output_buffer = old_output_buffer
            end
          end_src

          return source
        end

        # Return true if the given template was compiled for a superset of the keys in local_assigns
        def supports_local_assigns?(render_symbol, local_assigns)
          local_assigns.empty? ||
            ((args = self.template_args[render_symbol]) && local_assigns.all? { |k,_| args.has_key?(k) })
        end
    end
  end
end