Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

content_for should work with provide.

  • Loading branch information...
commit ab105e6072d291d7024e4e645defa5eff31f6f21 1 parent 3e0aedb
@josevalim josevalim authored
View
9 actionpack/lib/action_view/flows.rb
@@ -19,6 +19,10 @@ def set(key, value)
def append(key, value)
@content[key] << value
end
+
+ def append!(key, value)
+ @content[key] << value
+ end
end
class StreamingFlow < OutputFlow
@@ -58,6 +62,11 @@ def get(key)
# by provides and resumes back to the fiber if it is
# the key it is waiting for.
def set(key, value)
+ @content[key] = (ActiveSupport::SafeBuffer.new << value)
+ end
+
+ # Append but also resume the fiber if it provided the right key.
+ def append!(key, value)
super
@fiber.resume if @waiting_for == key
end
View
8 actionpack/lib/action_view/helpers/capture_helper.rb
@@ -142,12 +142,12 @@ def content_for(name, content = nil, &block)
# The same as +content_for+ but when used with streaming flushes
# straight back to the layout. In other words, if you want to
# concatenate several times to the same buffer when rendering a given
- # template, you should use +content_for+, if not, use +provide+ as it
- # has better streaming support.
+ # template, you should use +content_for+, if not, use +provide+ to tell
+ # the layout to stop looking for more contents.
def provide(name, content = nil, &block)
content = capture(&block) if block_given?
- @_view_flow.set(name, content) if content
- content
+ result = @_view_flow.append!(name, content) if content
+ result unless content
end
# content_for? simply checks whether any content has been captured yet using content_for
View
4 actionpack/test/fixtures/layouts/streaming.erb
@@ -0,0 +1,4 @@
+<%= yield :header -%>
+<%= yield -%>
+<%= yield :footer -%>
+<%= yield(:unknown).presence || "." -%>
View
3  actionpack/test/fixtures/test/nested_streaming.erb
@@ -0,0 +1,3 @@
+<%- content_for :header do -%>?<%- end -%>
+<%= render :template => "test/streaming" %>
+?
View
3  actionpack/test/fixtures/test/streaming.erb
@@ -0,0 +1,3 @@
+<%- provide :header do -%>Yes, <%- end -%>
+this works
+<%- content_for :footer, " like a charm" -%>
View
3  actionpack/test/fixtures/test/streaming_buster.erb
@@ -0,0 +1,3 @@
+<%= yield :foo -%>
+This won't look
+<% provide :unknown, " good." -%>
View
11 actionpack/test/template/capture_helper_test.rb
@@ -47,13 +47,16 @@ def test_content_for
def test_provide
assert !content_for?(:title)
- provide :title, "title"
+ provide :title, "hi"
assert content_for?(:title)
- assert_equal "title", @_view_flow.get(:title)
+ assert_equal "hi", @_view_flow.get(:title)
provide :title, "<p>title</p>"
- assert_equal "&lt;p&gt;title&lt;/p&gt;", @_view_flow.get(:title)
+ assert_equal "hi&lt;p&gt;title&lt;/p&gt;", @_view_flow.get(:title)
+
+ @_view_flow = ActionView::OutputFlow.new
+ provide :title, "hi"
provide :title, "<p>title</p>".html_safe
- assert_equal "<p>title</p>", @_view_flow.get(:title)
+ assert_equal "hi<p>title</p>", @_view_flow.get(:title)
end
def test_with_output_buffer_swaps_the_output_buffer_given_no_argument
View
12 actionpack/test/template/streaming_render_test.rb
@@ -88,10 +88,18 @@ def test_render_with_handler_without_streaming_support
end
def test_render_with_streaming_multiple_yields_provide_and_content_for
- assert_equal "Yes, \nthis works\n like a charm.", buffered_render(:template => "test/streaming", :layout => "layouts/streaming")
+ assert_equal "Yes, \nthis works\n like a charm.",
+ buffered_render(:template => "test/streaming", :layout => "layouts/streaming")
end
def test_render_with_streaming_with_fake_yields_and_streaming_buster
- assert_equal "This won't look\n good.", buffered_render(:template => "test/streaming_buster", :layout => "layouts/streaming")
+ assert_equal "This won't look\n good.",
+ buffered_render(:template => "test/streaming_buster", :layout => "layouts/streaming")
end
+
+ def test_render_with_nested_streaming_multiple_yields_provide_and_content_for
+ assert_equal "?Yes, \n\nthis works\n\n? like a charm.",
+ buffered_render(:template => "test/nested_streaming", :layout => "layouts/streaming")
+ end
+
end if defined?(Fiber)

0 comments on commit ab105e6

Please sign in to comment.
Something went wrong with that request. Please try again.