Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Never return stored content from content_for when a block is given #2794

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions actionpack/lib/action_view/helpers/capture_helper.rb
Expand Up @@ -134,9 +134,13 @@ 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?
@_content_for[name] << content if content
@_content_for[name] unless content
if content || block_given?
content = capture(&block) if block_given?
@_content_for[name] << content if content
nil
else
@_content_for[name]
end
end

# content_for? simply checks whether any content has been captured yet using content_for
Expand Down
25 changes: 25 additions & 0 deletions actionpack/test/template/capture_helper_test.rb
Expand Up @@ -47,6 +47,31 @@ def test_content_for
assert ! content_for?(:something_else)
end

def test_content_for_with_multiple_calls
assert ! content_for?(:title)
content_for :title, 'foo'
content_for :title, 'bar'
assert_equal 'foobar', content_for(:title)
end

def test_content_for_with_block
assert ! content_for?(:title)
content_for :title do
output_buffer << 'foo'
output_buffer << 'bar'
nil
end
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_with_output_buffer_swaps_the_output_buffer_given_no_argument
assert_nil @av.output_buffer
buffer = @av.with_output_buffer do
Expand Down