aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2008-07-03 13:06:00 -0500
committerJoshua Peek <josh@joshpeek.com>2008-07-03 13:06:00 -0500
commit8a442e0d57fabedcaf3ded836cfc12f7067ead68 (patch)
tree3ab64f7ec0bda906882531af8a76a54ef83ae282 /actionpack
parent7d5c8505f528f195433693d5074a00f7635955b2 (diff)
downloadrails-8a442e0d57fabedcaf3ded836cfc12f7067ead68.tar.gz
rails-8a442e0d57fabedcaf3ded836cfc12f7067ead68.tar.bz2
rails-8a442e0d57fabedcaf3ded836cfc12f7067ead68.zip
Extracted Template rendering logic into Renderer module
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_view.rb2
-rw-r--r--actionpack/lib/action_view/inline_template.rb9
-rw-r--r--actionpack/lib/action_view/partial_template.rb2
-rw-r--r--actionpack/lib/action_view/renderer.rb29
-rw-r--r--actionpack/lib/action_view/template.rb33
5 files changed, 42 insertions, 33 deletions
diff --git a/actionpack/lib/action_view.rb b/actionpack/lib/action_view.rb
index 973020a768..49768fe264 100644
--- a/actionpack/lib/action_view.rb
+++ b/actionpack/lib/action_view.rb
@@ -24,6 +24,8 @@
require 'action_view/template_handlers'
require 'action_view/template_file'
require 'action_view/view_load_paths'
+
+require 'action_view/renderer'
require 'action_view/template'
require 'action_view/partial_template'
require 'action_view/inline_template'
diff --git a/actionpack/lib/action_view/inline_template.rb b/actionpack/lib/action_view/inline_template.rb
index 3a97f47e32..19ab92ce1a 100644
--- a/actionpack/lib/action_view/inline_template.rb
+++ b/actionpack/lib/action_view/inline_template.rb
@@ -1,5 +1,7 @@
module ActionView #:nodoc:
- class InlineTemplate < Template #:nodoc:
+ class InlineTemplate #:nodoc:
+ include Renderer
+
def initialize(view, source, locals = {}, type = nil)
@view = view
@@ -7,11 +9,8 @@ module ActionView #:nodoc:
@extension = type
@locals = locals || {}
+ @method_key = @source
@handler = Base.handler_class_for_extension(@extension).new(@view)
end
-
- def method_key
- @source
- end
end
end
diff --git a/actionpack/lib/action_view/partial_template.rb b/actionpack/lib/action_view/partial_template.rb
index 6ebe165a15..72f831e937 100644
--- a/actionpack/lib/action_view/partial_template.rb
+++ b/actionpack/lib/action_view/partial_template.rb
@@ -18,7 +18,7 @@ module ActionView #:nodoc:
def render
ActionController::Base.benchmark("Rendered #{@path.path_without_format_and_extension}", Logger::DEBUG, false) do
- @handler.render(self)
+ super
end
end
diff --git a/actionpack/lib/action_view/renderer.rb b/actionpack/lib/action_view/renderer.rb
new file mode 100644
index 0000000000..e6c64d2749
--- /dev/null
+++ b/actionpack/lib/action_view/renderer.rb
@@ -0,0 +1,29 @@
+module ActionView
+ module Renderer
+ # TODO: Local assigns should not be tied to template instance
+ attr_accessor :locals
+
+ # TODO: These readers should be private
+ attr_reader :filename, :source, :handler, :method_key, :method
+
+ def render
+ prepare!
+ @handler.render(self)
+ end
+
+ private
+ def prepare!
+ unless @prepared
+ @view.send(:evaluate_assigns)
+ @view.current_render_extension = @extension
+
+ if @handler.compilable?
+ @handler.compile_template(self) # compile the given template, if necessary
+ @method = @view.method_names[method_key] # Set the method name for this template and run it
+ end
+
+ @prepared = true
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb
index d4118f0f86..8142232c8f 100644
--- a/actionpack/lib/action_view/template.rb
+++ b/actionpack/lib/action_view/template.rb
@@ -1,12 +1,13 @@
module ActionView #:nodoc:
class Template #:nodoc:
+ include Renderer
+
class << self
# TODO: Deprecate
delegate :register_template_handler, :to => 'ActionView::Base'
end
- attr_accessor :locals
- attr_reader :handler, :path, :extension, :filename, :method
+ attr_reader :path, :extension
def initialize(view, path, use_full_path = nil, locals = {})
unless use_full_path == nil
@@ -19,9 +20,10 @@ module ActionView #:nodoc:
@original_path = path
@path = TemplateFile.from_path(path)
@view.first_render ||= @path.to_s
- @source = nil # Don't read the source until we know that it is required
+
set_extension_and_file_name
+ @method_key = @filename
@locals = locals || {}
@handler = Base.handler_class_for_extension(@extension).new(@view)
end
@@ -38,37 +40,14 @@ module ActionView #:nodoc:
end
end
- def render
- prepare!
- @handler.render(self)
- end
-
- def path_without_extension
- @path.path_without_extension
- end
-
def source
@source ||= File.read(self.filename)
end
- def method_key
- @filename
- end
-
def base_path_for_exception
(@paths.find_load_path_for_path(@path) || @paths.first).to_s
end
- def prepare!
- @view.send :evaluate_assigns
- @view.current_render_extension = @extension
-
- if @handler.compilable?
- @handler.compile_template(self) # compile the given template, if necessary
- @method = @view.method_names[method_key] # Set the method name for this template and run it
- end
- end
-
private
def set_extension_and_file_name
@extension = @path.extension
@@ -94,7 +73,7 @@ module ActionView #:nodoc:
full_template_path = @original_path.include?('.') ? @original_path : "#{@original_path}.#{@view.template_format}.erb"
display_paths = @paths.join(':')
template_type = (@original_path =~ /layouts/i) ? 'layout' : 'template'
- raise(MissingTemplate, "Missing #{template_type} #{full_template_path} in view path #{display_paths}")
+ raise MissingTemplate, "Missing #{template_type} #{full_template_path} in view path #{display_paths}"
end
end
end