mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Introduce flush_output_buffer to append the buffer to the response body then start a new buffer. Useful for pushing custom parts to the response body without disrupting template rendering.
This commit is contained in:
parent
91d2740595
commit
3d260760f0
2 changed files with 43 additions and 0 deletions
|
@ -131,6 +131,14 @@ module ActionView
|
|||
ensure
|
||||
self.output_buffer = old_buffer
|
||||
end
|
||||
|
||||
# Add the output buffer to the response body and start a new one.
|
||||
def flush_output_buffer #:nodoc:
|
||||
if output_buffer && output_buffer != ''
|
||||
response.body_parts << output_buffer
|
||||
self.output_buffer = ''
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
35
actionpack/test/template/output_buffer_test.rb
Normal file
35
actionpack/test/template/output_buffer_test.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
class OutputBufferTest < ActionController::TestCase
|
||||
class TestController < ActionController::Base
|
||||
def index
|
||||
render :text => 'foo'
|
||||
end
|
||||
end
|
||||
|
||||
tests TestController
|
||||
|
||||
def test_flush_output_buffer
|
||||
# Start with the default body parts
|
||||
get :index
|
||||
assert_equal ['foo'], @response.body_parts
|
||||
assert_nil @response.template.output_buffer
|
||||
|
||||
# Nil output buffer is skipped
|
||||
@response.template.flush_output_buffer
|
||||
assert_nil @response.template.output_buffer
|
||||
assert_equal ['foo'], @response.body_parts
|
||||
|
||||
# Empty output buffer is skipped
|
||||
@response.template.output_buffer = ''
|
||||
@response.template.flush_output_buffer
|
||||
assert_equal '', @response.template.output_buffer
|
||||
assert_equal ['foo'], @response.body_parts
|
||||
|
||||
# Flushing appends the output buffer to the body parts
|
||||
@response.template.output_buffer = 'bar'
|
||||
@response.template.flush_output_buffer
|
||||
assert_equal '', @response.template.output_buffer
|
||||
assert_equal ['foo', 'bar'], @response.body_parts
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue