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

Add flush option for Sinatra::ContentFor flush content #1225

Merged
merged 2 commits into from Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions sinatra-contrib/lib/sinatra/capture.rb
Expand Up @@ -104,7 +104,7 @@ def capture(*args, &block)
buffer = eval '_buf if defined?(_buf)', block.binding
old_buffer = buffer.dup if buffer
dummy = DUMMIES.fetch(current_engine)
options = { :layout => false, :locals => {:args => args, :block => block }}
options = { :layout => false, :locals => {:args => args, :block => block } }

buffer.try :clear
result = render(current_engine, dummy, options, &block)
Expand All @@ -116,7 +116,7 @@ def capture(*args, &block)

def capture_later(&block)
engine = current_engine
proc { |*a| with_engine(engine) { @capture = capture(*a, &block) }}
proc { |*a| with_engine(engine) { @capture = capture(*a, &block) } }
end
end

Expand Down
13 changes: 9 additions & 4 deletions sinatra-contrib/lib/sinatra/content_for.rb
Expand Up @@ -128,9 +128,12 @@ module ContentFor
#
# Your blocks can also receive values, which are passed to them
# by <tt>yield_content</tt>
def content_for(key, value = nil, &block)
def content_for(key, value = nil, options = {}, &block)
key = key.to_sym

block ||= proc { |*| value }
content_blocks[key.to_sym] << capture_later(&block)
clear_content_for(key) if options[:flush]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iguchi1124 this is the only line that needs added in this method. Can you rebase with master and limit changes to just this line and the added tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tried to resolve the conflicts by web GUI.

content_blocks[key] << capture_later(&block)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather leave this as:

 content_blocks[key.to_sym] << capture_later(&block)

and delete the assignment key = key.to_sym above.

end

# Check if a block of content with the given key was defined. For
Expand Down Expand Up @@ -172,8 +175,10 @@ def clear_content_for(key)
# Would pass <tt>1</tt> and <tt>2</tt> to all the blocks registered
# for <tt>:head</tt>.
def yield_content(key, *args)
return yield(*args) if block_given? && content_blocks[key.to_sym].empty?
content_blocks[key.to_sym].map { |b| capture(*args, &b) }.join
key = key.to_sym
return yield(*args) if block_given? && content_blocks[key].empty?

content_blocks[key].map { |b| capture(*args, &b) }.join
end

private
Expand Down
16 changes: 16 additions & 0 deletions sinatra-contrib/spec/content_for_spec.rb
Expand Up @@ -70,6 +70,22 @@ def render(engine, template)
content_for(:foo, "foo")
expect(yield_content(:foo)).to eq("foo")
end

context 'when flush option was disabled' do
it 'append content' do
content_for(:foo, "foo")
content_for(:foo, "bar")
expect(yield_content(:foo)).to eq("foobar")
end
end

context 'when flush option was enabled' do
it 'flush first content' do
content_for(:foo, "foo")
content_for(:foo, "bar", flush: true)
expect(yield_content(:foo)).to eq("bar")
end
end
end

# TODO: liquid radius markaby builder nokogiri
Expand Down