Permalink
Browse files

Never return stored content from content_for when a block is given

The capture helper may return nil when evaluation of the block has
produced a buffer which contains only whitespace, but that doesn't
mean content_for should return stored content.
  • Loading branch information...
1 parent b149021 commit f49faeb705f269a2ea0d07078e94bffe7e5d4f1a @tomstuart tomstuart committed Sep 1, 2011
View
6 actionpack/lib/action_view/helpers/capture_helper.rb
@@ -134,9 +134,9 @@ def capture(*args)
# WARNING: content_for is ignored in caches. So you shouldn't use it
# for elements that will be fragment cached.
def content_for(name, content = nil, &block)
- content = capture(&block) if block_given?
- if content
- @view_flow.append(name, content)
+ if content || block_given?
+ content = capture(&block) if block_given?
+ @view_flow.append(name, content) if content
nil
else
@view_flow.get(name)
View
8 actionpack/test/template/capture_helper_test.rb
@@ -74,6 +74,14 @@ def test_content_for_with_whitespace_block
assert_equal 'foobar', content_for(:title)
end
+ def test_content_for_returns_nil_when_writing
+ assert ! content_for?(:title)
+ assert_equal nil, content_for(:title, 'foo')
+ assert_equal nil, content_for(:title) { output_buffer << 'bar'; nil }
+ assert_equal nil, content_for(:title) { output_buffer << " \n "; nil }
+ assert_equal 'foobar', content_for(:title)
+ end
+
def test_content_for_question_mark
assert ! content_for?(:title)
content_for :title, 'title'

0 comments on commit f49faeb

Please sign in to comment.