aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/template/renderable.rb
diff options
context:
space:
mode:
authorYehuda Katz <wycats@gmail.com>2009-01-22 16:18:10 -0600
committerJoshua Peek <josh@joshpeek.com>2009-01-22 16:18:10 -0600
commiteb9af20b7cc0e374277cf330bdd404f9daab28ec (patch)
treeba05a906690684e442ed92db3e63e295f0dd133e /actionpack/lib/action_view/template/renderable.rb
parentcc0b5fa9930dcc60914e21b518b3c54109243cfa (diff)
downloadrails-eb9af20b7cc0e374277cf330bdd404f9daab28ec.tar.gz
rails-eb9af20b7cc0e374277cf330bdd404f9daab28ec.tar.bz2
rails-eb9af20b7cc0e374277cf330bdd404f9daab28ec.zip
Begin unifying the interface between ActionController and ActionView
Diffstat (limited to 'actionpack/lib/action_view/template/renderable.rb')
-rw-r--r--actionpack/lib/action_view/template/renderable.rb74
1 files changed, 74 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/template/renderable.rb b/actionpack/lib/action_view/template/renderable.rb
new file mode 100644
index 0000000000..35c832aaba
--- /dev/null
+++ b/actionpack/lib/action_view/template/renderable.rb
@@ -0,0 +1,74 @@
+module ActionView
+ # NOTE: The template that this mixin is being included into is frozen
+ # so you cannot set or modify any instance variables
+ module Renderable #:nodoc:
+ extend ActiveSupport::Memoizable
+
+ def filename
+ 'compiled-template'
+ end
+
+ def handler
+ Template.handler_class_for_extension(extension)
+ end
+ memoize :handler
+
+ def compiled_source
+ handler.call(self)
+ end
+ memoize :compiled_source
+
+ def method_name_without_locals
+ ['_run', extension, method_segment].compact.join('_')
+ end
+ memoize :method_name_without_locals
+
+ def method_name(local_assigns)
+ if local_assigns && local_assigns.any?
+ method_name = method_name_without_locals.dup
+ method_name << "_locals_#{local_assigns.keys.map { |k| k.to_s }.sort.join('_')}"
+ else
+ method_name = method_name_without_locals
+ end
+ method_name.to_sym
+ end
+
+ # Compile and evaluate the template's code (if necessary)
+ def compile(local_assigns)
+ render_symbol = method_name(local_assigns)
+
+ if !Base::CompiledTemplates.method_defined?(render_symbol) || recompile?
+ compile!(render_symbol, local_assigns)
+ end
+ end
+
+ private
+ def compile!(render_symbol, local_assigns)
+ 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
+ ActionView::Base::CompiledTemplates.module_eval(source, filename, 0)
+ rescue Exception => e # errors from template code
+ if logger = defined?(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
+
+ def recompile?
+ false
+ end
+ end
+end