Skip to content

Commit

Permalink
Update capture to handle empty blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jkowens committed Dec 9, 2018
1 parent bf2cb85 commit 9a31067
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sinatra-contrib/lib/sinatra/capture.rb
Expand Up @@ -110,7 +110,7 @@ def capture(*args, &block)
buffer.clear unless buffer.nil?
result = render(current_engine, dummy, options, &block)
end
result.strip.empty? && @capture ? @capture : result
result && result.strip.empty? && @capture ? @capture : result
ensure
buffer.replace(old_buffer) unless buffer.nil?
end
Expand Down
6 changes: 6 additions & 0 deletions sinatra-contrib/spec/capture_spec.rb
Expand Up @@ -50,6 +50,12 @@ def render(engine, template)
expect(render(:erb, "iso_8859_1")).to eq("ISO-8859-1 -")
end
end

describe 'without templates' do
it 'captures empty blocks' do
expect(capture {}).to be_nil
end
end
end

__END__
Expand Down
2 changes: 1 addition & 1 deletion sinatra-contrib/spec/content_for_spec.rb
Expand Up @@ -33,7 +33,7 @@ def render(engine, template)
end

it 'renders default content if no block matches the key and a default block is specified' do
content_for(:bar) { "bar" }
expect(yield_content(:foo) {}).to be_nil
expect(yield_content(:foo) { "foo" }).to eq("foo")
end

Expand Down

0 comments on commit 9a31067

Please sign in to comment.