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

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

merged 2 commits into from Dec 21, 2018

Conversation

iguchi1124
Copy link
Contributor

@iguchi1124 iguchi1124 commented Dec 30, 2016

close #1138

I added flush option to content_for method for flush content. which like content_for :flush options of Rails.

see: http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for

content_for :something_else do
  :that_one_thing
end

# flush option
content_for :something_else, flush: true do
  yield_content :that_other_thing
end

yield_content :something_else # returns content :that_other_thing

@iguchi1124 iguchi1124 changed the title [WIP] Add flush option for Sinatra::ContentFor flush content Add flush option for Sinatra::ContentFor flush content Dec 30, 2016
@jkowens
Copy link
Member

jkowens commented Jan 17, 2017

@iguchi1124 I don't think that the way you've implemented :flush quite lines up with the Rails behavior.

This is how I would expect flush to behave:

# flush option
content_for :something_else do
  :that_one_thing
end

content_for :something_else, flush: true do
  :that_other_thing
end

yield_content :something_else # returns content :that_other_thing
yield_content :something_else # returns content :that_other_thing

So instead of just rendering the content one time, it renders only the last content with flush: true every time afterwards.

@@ -128,9 +129,25 @@ 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, *args, &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 see the method signature:

def content_for(key, value = nil, options = {}, &block)

It would avoid the need to jump through the hoops below.

end

private

def content_blocks
@content_blocks ||= Hash.new { |h, k| h[k] = [] }
def contents
Copy link
Member

Choose a reason for hiding this comment

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

I kind of like the method name content_blocks, I feel like it is a bit more descriptive of what the hash represents.

block ||= proc { |*| value }
content_blocks[key.to_sym] << capture_later(&block)
flush_key_set << 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.

I think this could be simplified to just something like:

clear_content_for(key) if options[:flush]

I'm almost certain that would take care of mimicking the Rails behavior.

@jkowens
Copy link
Member

jkowens commented Jan 19, 2017

@iguchi1124 thanks for getting started on this feature! I think you're close, just a few tweaks should do it 👍

@iguchi1124
Copy link
Contributor Author

I'll try to fix the review points soon, thanks for reviews!

@zzak zzak added the feature label Jan 30, 2017
@zzak zzak added this to the 2.0.0 milestone Jan 30, 2017
@iguchi1124
Copy link
Contributor Author

iguchi1124 commented Feb 6, 2017

Thank you for teaching me, I was misunderstanding about that options meanings.
I fixed this pull request.

@jkowens @zzak

@zzak zzak modified the milestones: Beyond, 2.0.0 Mar 27, 2017
@zzak
Copy link
Member

zzak commented Mar 27, 2017

Thank you for updating your patch @iguchi1124!

I will postpone this until after 2.0.0 release is made.

@javimbk
Copy link

javimbk commented Aug 17, 2017

Hello! Is this finally making it into Sinatra? @zzak

@iguchi1124
Copy link
Contributor Author

Hi there, do you have any progress regarding this releases or reviews?

@jkowens
Copy link
Member

jkowens commented Dec 21, 2018

@namusyaka if this can be rebased, can we schedule this for the 2.1 release?

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.

@namusyaka
Copy link
Member

I don't have to wait until 2.1 release because the public interface seems not to be broken by this patch. The internal key symbolization isn't problem, I think.

if block_given? && !content_for?(key)
haml? ? capture_haml(*args, &block) : yield(*args)
else
content = content_blocks[key.to_sym].map { |b| capture(*args, &b) }
content = content_blocks[key].map { |b| capture(*args, &b) }
Copy link
Member

Choose a reason for hiding this comment

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

This change doesn't seem necessary to me. What do you think?

block ||= proc { |*| value }
content_blocks[key.to_sym] << capture_later(&block)
clear_content_for(key) if options[:flush]
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.

@namusyaka
Copy link
Member

@jkowens Your reviews should be reflected after merging this.

@namusyaka namusyaka merged commit 84b290a into sinatra:master Dec 21, 2018
namusyaka added a commit that referenced this pull request Dec 21, 2018
@iguchi1124 iguchi1124 deleted the flush-content-for branch December 24, 2018 06:32
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Feb 6, 2019
### sinatra-contrib

* Add `flush` option to `content_for` [#1225](sinatra/sinatra#1225) by Shota Iguchi

* Drop activesupport dependency from sinatra-contrib [#1448](sinatra/sinatra#1448)

* Update `yield_content` to append default to ERB template buffer [#1500](sinatra/sinatra#1500) by Jordan Owens
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sinatra::ContentFor flush content
5 participants