Buffer should be an option passed down to template rendering.

This commit is contained in:
José Valim 2011-04-16 02:10:36 +02:00
parent fad214b9e1
commit 2dd43c3f80
5 changed files with 5 additions and 100 deletions

@ -132,11 +132,11 @@ def initialize(source, identifier, handler, details)
# This method is instrumented as "!render_template.action_view". Notice that
# we use a bang in this instrumentation because you don't want to
# consume this in production. This is only slow if it's being listened to.
def render(view, locals, &block)
def render(view, locals, buffer=nil, &block)
old_template, view._template = view._template, self
ActiveSupport::Notifications.instrument("!render_template.action_view", :virtual_path => @virtual_path) do
compile!(view)
view.send(method_name, locals, &block)
view.send(method_name, locals, buffer, &block)
end
rescue Exception => e
handle_render_error(view, e)
@ -167,28 +167,6 @@ def refresh(view)
end
end
# Expires this template by setting his updated_at date to Jan 1st, 1970.
def expire!
@updated_at = Time.utc(1970)
end
# Receives a view context and renders a template exactly like self by using
# the @virtual_path. It raises an error if no @virtual_path was given.
def rerender(view)
raise "A template needs to have a virtual path in order to be rerendered" unless @virtual_path
name = @virtual_path.dup
if name.sub!(/(^|\/)_([^\/]*)$/, '\1\2')
view.render :partial => name
else
view.render :template => @virtual_path
end
end
# Used to store template data by template handlers.
def data
@data ||= {}
end
def inspect
@inspect ||=
if defined?(Rails.root)
@ -274,13 +252,12 @@ def compile(view, mod) #:nodoc:
end
end
arity = @handler.respond_to?(:arity) ? @handler.arity : @handler.method(:call).arity
code = arity.abs == 1 ? @handler.call(self) : @handler.call(self, view)
code = @handler.call(self)
# Make sure that the resulting String to be evalled is in the
# encoding of the code
source = <<-end_src
def #{method_name}(local_assigns)
def #{method_name}(local_assigns, output_buffer)
_old_output_buffer = @output_buffer;#{locals_code};#{code}
ensure
@output_buffer = _old_output_buffer

@ -22,7 +22,7 @@ class Template
module Handlers
class Erubis < ::Erubis::Eruby
def add_preamble(src)
src << "@output_buffer = ActionView::OutputBuffer.new;"
src << "@output_buffer = output_buffer || ActionView::OutputBuffer.new;"
end
def add_text(src, text)

@ -180,16 +180,6 @@ def teardown
assert_not_equal template, old_template
end
test "data can be stored in cached templates" do
template = @lookup_context.find("hello_world", %w(test))
template.data["cached"] = "data"
assert_equal "Hello world!", template.source
template = @lookup_context.find("hello_world", %w(test))
assert_equal "data", template.data["cached"]
assert_equal "Hello world!", template.source
end
end
class LookupContextWithFalseCaching < ActiveSupport::TestCase
@ -235,21 +225,6 @@ def setup
template = @lookup_context.find("foo", %w(test), true)
assert_equal "Foo", template.source
end
test "data can be stored as long as template was not updated" do
template = @lookup_context.find("foo", %w(test), true)
template.data["cached"] = "data"
assert_equal "Foo", template.source
template = @lookup_context.find("foo", %w(test), true)
assert_equal "data", template.data["cached"]
assert_equal "Foo", template.source
@resolver.hash["test/_foo.erb"][1] = Time.now.utc
template = @lookup_context.find("foo", %w(test), true)
assert_nil template.data["cached"]
assert_equal "Foo", template.source
end
end
class TestMissingTemplate < ActiveSupport::TestCase

@ -227,20 +227,11 @@ def test_render_fallbacks_to_erb_for_unknown_types
"@output_buffer << 'source: #{template.source.inspect}'\n"
end
WithViewHandler = lambda do |template, view|
%'"#{template.class} #{view.class}"'
end
def test_render_inline_with_render_from_to_proc
ActionView::Template.register_template_handler :ruby_handler, :source.to_proc
assert_equal '3', @view.render(:inline => "(1 + 2).to_s", :type => :ruby_handler)
end
def test_render_inline_with_template_handler_with_view
ActionView::Template.register_template_handler :with_view, WithViewHandler
assert_equal 'ActionView::Template ActionView::Base', @view.render(:inline => "Hello, World!", :type => :with_view)
end
def test_render_inline_with_compilable_custom_type
ActionView::Template.register_template_handler :foo, CustomHandler
assert_equal 'source: "Hello, World!"', @view.render(:inline => "Hello, World!", :type => :foo)

@ -113,44 +113,6 @@ def test_refresh_raises_an_error_without_virtual_path
end
end
def test_template_expire_sets_the_timestamp_to_1970
@template = new_template("Hello", :updated_at => Time.utc(2010))
assert_equal Time.utc(2010), @template.updated_at
@template.expire!
assert_equal Time.utc(1970), @template.updated_at
end
def test_template_rerender_renders_a_template_like_self
@template = new_template("Hello", :virtual_path => "test/foo_bar")
@context.expects(:render).with(:template => "test/foo_bar").returns("template")
assert_equal "template", @template.rerender(@context)
end
def test_template_rerender_renders_a_root_template_like_self
@template = new_template("Hello", :virtual_path => "foo_bar")
@context.expects(:render).with(:template => "foo_bar").returns("template")
assert_equal "template", @template.rerender(@context)
end
def test_template_rerender_renders_a_partial_like_self
@template = new_template("Hello", :virtual_path => "test/_foo_bar")
@context.expects(:render).with(:partial => "test/foo_bar").returns("partial")
assert_equal "partial", @template.rerender(@context)
end
def test_template_rerender_renders_a_root_partial_like_self
@template = new_template("Hello", :virtual_path => "_foo_bar")
@context.expects(:render).with(:partial => "foo_bar").returns("partial")
assert_equal "partial", @template.rerender(@context)
end
def test_rerender_raises_an_error_without_virtual_path
@template = new_template("Hello", :virtual_path => nil)
assert_raise RuntimeError do
@template.rerender(@context)
end
end
if "ruby".encoding_aware?
def test_resulting_string_is_utf8
@template = new_template