diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-07-28 12:03:16 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-07-28 12:03:16 +0100 |
commit | de211914d84737c4314e862b459b9a23a0baa28f (patch) | |
tree | 3f04c940a4c821014332ddc4f119a81a37db3a33 /actionpack/lib/action_view/renderable.rb | |
parent | 6246a8e06035973c88a38826ce1ee6b07b03b8dc (diff) | |
parent | 10d9fe4bf3110c1d5de0c6b509fe0cbb9d5eda1d (diff) | |
download | rails-de211914d84737c4314e862b459b9a23a0baa28f.tar.gz rails-de211914d84737c4314e862b459b9a23a0baa28f.tar.bz2 rails-de211914d84737c4314e862b459b9a23a0baa28f.zip |
Merge commit 'mainstream/master'
Conflicts:
actionpack/lib/action_controller/cookies.rb
actionpack/lib/action_controller/streaming.rb
actionpack/lib/action_controller/test_case.rb
actionpack/lib/action_view/base.rb
actionpack/lib/action_view/helpers/date_helper.rb
actionpack/lib/action_view/helpers/debug_helper.rb
actionpack/lib/action_view/helpers/form_options_helper.rb
actionpack/lib/action_view/helpers/javascript_helper.rb
activerecord/lib/active_record/associations.rb
activerecord/lib/active_record/validations.rb
activeresource/README
activesupport/lib/active_support/core_ext/hash/reverse_merge.rb
activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/data_timezone_info.rb
activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/timezone.rb
railties/doc/guides/actionview/helpers.markdown
railties/doc/guides/actionview/partials.markdown
railties/doc/guides/activerecord/basics.markdown
Diffstat (limited to 'actionpack/lib/action_view/renderable.rb')
-rw-r--r-- | actionpack/lib/action_view/renderable.rb | 85 |
1 files changed, 48 insertions, 37 deletions
diff --git a/actionpack/lib/action_view/renderable.rb b/actionpack/lib/action_view/renderable.rb index 2c4302146f..5fe1ca86f3 100644 --- a/actionpack/lib/action_view/renderable.rb +++ b/actionpack/lib/action_view/renderable.rb @@ -3,26 +3,35 @@ module ActionView # NOTE: The template that this mixin is beening include into is frozen # So you can not set or modify any instance variables + extend ActiveSupport::Memoizable + def self.included(base) @@mutex = Mutex.new end - # NOTE: Exception to earlier notice. Ensure this is called before freeze + def filename + 'compiled-template' + end + def handler - @handler ||= Template.handler_class_for_extension(extension) + Template.handler_class_for_extension(extension) end + memoize :handler - # NOTE: Exception to earlier notice. Ensure this is called before freeze def compiled_source - @compiled_source ||= handler.new(nil).compile(self) if handler.compilable? + handler.call(self) end + memoize :compiled_source def render(view, local_assigns = {}) - view.first_render ||= self + compile(local_assigns) + + view._first_render ||= self + view._last_render = self + view.send(:evaluate_assigns) - view.current_render_extension = extension - compile(local_assigns) if handler.compilable? - handler.new(view).render(self, local_assigns) + view.send(:set_controller_content_type, mime_type) if respond_to?(:mime_type) + view.send(:execute, method(local_assigns), local_assigns) end def method(local_assigns) @@ -33,47 +42,49 @@ module ActionView end private - # Compile and evaluate the template's code + # Compile and evaluate the template's code (if necessary) 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) + if recompile?(render_symbol) + compile!(render_symbol, local_assigns) end end end + 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 + logger = ActionController::Base.logger + logger.debug "Compiling template #{render_symbol}" if logger + + 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")}" + end + + raise ActionView::TemplateError.new(self, {}, e) + 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 + !(ActionView::PathSet::Path.eager_load_templates? && Base::CompiledTemplates.method_defined?(symbol)) end end end |