Ruby 1.9: flushing the output buffer preserves its encoding

This commit is contained in:
Jeremy Kemper
2009-05-28 16:56:09 -05:00
parent d120f9de7e
commit e23554d79e
2 changed files with 21 additions and 3 deletions

View File

@@ -124,7 +124,11 @@ module ActionView
# Use an alternate output buffer for the duration of the block.
# Defaults to a new empty string.
def with_output_buffer(buf = '') #:nodoc:
def with_output_buffer(buf = nil) #:nodoc:
unless buf
buf = ''
buf.force_encoding(output_buffer.encoding) if buf.respond_to?(:force_encoding)
end
self.output_buffer, old_buffer = buf, output_buffer
yield
output_buffer
@@ -134,9 +138,12 @@ module ActionView
# Add the output buffer to the response body and start a new one.
def flush_output_buffer #:nodoc:
if output_buffer && output_buffer != ''
if output_buffer && !output_buffer.empty?
response.body_parts << output_buffer
self.output_buffer = ''
new = ''
new.force_encoding(output_buffer.encoding) if new.respond_to?(:force_encoding)
self.output_buffer = new
nil
end
end
end

View File

@@ -38,6 +38,17 @@ class OutputBufferTest < ActionController::TestCase
assert_equal ['foo', 'bar'], body_parts
end
if '1.9'.respond_to?(:force_encoding)
test 'flushing preserves output buffer encoding' do
original_buffer = ' '.force_encoding(Encoding::EUC_JP)
@controller.template.output_buffer = original_buffer
@controller.template.flush_output_buffer
assert_equal ['foo', original_buffer], body_parts
assert_not_equal original_buffer, output_buffer
assert_equal Encoding::EUC_JP, output_buffer.encoding
end
end
protected
def output_buffer
@controller.template.output_buffer