1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Introduce ActionView::InlineTemplate class

This commit is contained in:
Pratik Naik 2008-04-19 16:16:32 +01:00
parent 69a5c1df82
commit 534c6b2444
7 changed files with 37 additions and 18 deletions

View file

@ -1,5 +1,7 @@
*SVN* *SVN*
* Introduce ActionView::InlineTemplate class. [Pratik]
* Automatically parse posted JSON content for Mime::JSON requests. [rick] * Automatically parse posted JSON content for Mime::JSON requests. [rick]
POST /posts POST /posts

View file

@ -870,7 +870,7 @@ module ActionController #:nodoc:
elsif inline = options[:inline] elsif inline = options[:inline]
add_variables_to_assigns add_variables_to_assigns
tmpl = ActionView::Template.new(@template, options[:inline], false, options[:locals], true, options[:type]) tmpl = ActionView::InlineTemplate.new(@template, options[:inline], options[:locals], options[:type])
render_for_text(@template.render_template(tmpl), options[:status]) render_for_text(@template.render_template(tmpl), options[:status])
elsif action_name = options[:action] elsif action_name = options[:action]

View file

@ -30,6 +30,7 @@ require 'action_view/template_handlers/rjs'
require 'action_view/template_finder' require 'action_view/template_finder'
require 'action_view/template' require 'action_view/template'
require 'action_view/partial_template' require 'action_view/partial_template'
require 'action_view/inline_template'
require 'action_view/base' require 'action_view/base'
require 'action_view/partials' require 'action_view/partials'

View file

@ -279,7 +279,7 @@ If you are rendering a subtemplate, you must now use controller-like partial syn
elsif options[:partial] elsif options[:partial]
render_partial(options[:partial], ActionView::Base::ObjectWrapper.new(options[:object]), options[:locals]) render_partial(options[:partial], ActionView::Base::ObjectWrapper.new(options[:object]), options[:locals])
elsif options[:inline] elsif options[:inline]
template = Template.new(self, options[:inline], false, options[:locals], true, options[:type]) template = InlineTemplate.new(self, options[:inline], options[:locals], options[:type])
render_template(template) render_template(template)
end end
end end

View file

@ -0,0 +1,20 @@
module ActionView #:nodoc:
class InlineTemplate < Template #:nodoc:
def initialize(view, source, locals = {}, type = nil)
@view = view
@finder = @view.finder
@source = source
@extension = type
@locals = locals || {}
@handler = self.class.handler_class_for_extension(@extension).new(@view)
end
def method_key
@source
end
end
end

View file

@ -2,22 +2,18 @@ module ActionView #:nodoc:
class Template #:nodoc: class Template #:nodoc:
attr_accessor :locals attr_accessor :locals
attr_reader :handler, :path, :source, :extension, :filename, :path_without_extension, :method attr_reader :handler, :path, :extension, :filename, :path_without_extension, :method
def initialize(view, path_or_source, use_full_path, locals = {}, inline = false, inline_type = nil) def initialize(view, path, use_full_path, locals = {})
@view = view @view = view
@finder = @view.finder @finder = @view.finder
unless inline # Clear the forward slash at the beginning if exists
# Clear the forward slash at the beginning if exists @path = use_full_path ? path.sub(/^\//, '') : path
@path = use_full_path ? path_or_source.sub(/^\//, '') : path_or_source @view.first_render ||= @path
@view.first_render ||= @path @source = nil # Don't read the source until we know that it is required
@source = nil # Don't read the source until we know that it is required set_extension_and_file_name(use_full_path)
set_extension_and_file_name(use_full_path)
else
@source = path_or_source
@extension = inline_type
end
@locals = locals || {} @locals = locals || {}
@handler = self.class.handler_class_for_extension(@extension).new(@view) @handler = self.class.handler_class_for_extension(@extension).new(@view)
end end
@ -32,7 +28,7 @@ module ActionView #:nodoc:
end end
def method_key def method_key
@method_key ||= (@filename || @source) @filename
end end
def base_path_for_exception def base_path_for_exception

View file

@ -20,7 +20,7 @@ class CustomHandlerTest < Test::Unit::TestCase
end end
def test_custom_render def test_custom_render
template = ActionView::Template.new(@view, "hello <%= one %>", false, { :one => "two" }, true, "foo") template = ActionView::InlineTemplate.new(@view, "hello <%= one %>", { :one => "two" }, "foo")
result = @view.render_template(template) result = @view.render_template(template)
assert_equal( assert_equal(
@ -29,7 +29,7 @@ class CustomHandlerTest < Test::Unit::TestCase
end end
def test_custom_render2 def test_custom_render2
template = ActionView::Template.new(@view, "hello <%= one %>", false, { :one => "two" }, true, "foo2") template = ActionView::InlineTemplate.new(@view, "hello <%= one %>", { :one => "two" }, "foo2")
result = @view.render_template(template) result = @view.render_template(template)
assert_equal( assert_equal(
[ "hello <%= one %>", { :one => "two" }, @view ], [ "hello <%= one %>", { :one => "two" }, @view ],
@ -38,7 +38,7 @@ class CustomHandlerTest < Test::Unit::TestCase
def test_unhandled_extension def test_unhandled_extension
# uses the ERb handler by default if the extension isn't recognized # uses the ERb handler by default if the extension isn't recognized
template = ActionView::Template.new(@view, "hello <%= one %>", false, { :one => "two" }, true, "bar") template = ActionView::InlineTemplate.new(@view, "hello <%= one %>", { :one => "two" }, "bar")
result = @view.render_template(template) result = @view.render_template(template)
assert_equal "hello two", result assert_equal "hello two", result
end end