Skip to content
This repository
Browse code

Merge pull request #2799 from tomstuart/3-1-stable

Never return stored content from content_for when a block is given
  • Loading branch information...
commit a2baf03d8ac3468348515c904a9ef15b003d3ac5 2 parents 0b7a1f8 + f49faeb
Santiago Pastorino authored September 05, 2011
6  actionpack/lib/action_view/helpers/capture_helper.rb
@@ -134,9 +134,9 @@ def capture(*args)
134 134
       # WARNING: content_for is ignored in caches. So you shouldn't use it
135 135
       # for elements that will be fragment cached.
136 136
       def content_for(name, content = nil, &block)
137  
-        content = capture(&block) if block_given?
138  
-        if content
139  
-          @view_flow.append(name, content)
  137
+        if content || block_given?
  138
+          content = capture(&block) if block_given?
  139
+          @view_flow.append(name, content) if content
140 140
           nil
141 141
         else
142 142
           @view_flow.get(name)
36  actionpack/test/template/capture_helper_test.rb
@@ -46,6 +46,42 @@ def test_capture_used_for_read
46 46
     assert_equal "bar", content_for(:bar)
47 47
   end
48 48
 
  49
+  def test_content_for_with_multiple_calls
  50
+    assert ! content_for?(:title)
  51
+    content_for :title, 'foo'
  52
+    content_for :title, 'bar'
  53
+    assert_equal 'foobar', content_for(:title)
  54
+  end
  55
+
  56
+  def test_content_for_with_block
  57
+    assert ! content_for?(:title)
  58
+    content_for :title do
  59
+      output_buffer << 'foo'
  60
+      output_buffer << 'bar'
  61
+      nil
  62
+    end
  63
+    assert_equal 'foobar', content_for(:title)
  64
+  end
  65
+
  66
+  def test_content_for_with_whitespace_block
  67
+    assert ! content_for?(:title)
  68
+    content_for :title, 'foo'
  69
+    content_for :title do
  70
+      output_buffer << "  \n  "
  71
+      nil
  72
+    end
  73
+    content_for :title, 'bar'
  74
+    assert_equal 'foobar', content_for(:title)
  75
+  end
  76
+
  77
+  def test_content_for_returns_nil_when_writing
  78
+    assert ! content_for?(:title)
  79
+    assert_equal nil, content_for(:title, 'foo')
  80
+    assert_equal nil, content_for(:title) { output_buffer << 'bar'; nil }
  81
+    assert_equal nil, content_for(:title) { output_buffer << "  \n  "; nil }
  82
+    assert_equal 'foobar', content_for(:title)
  83
+  end
  84
+
49 85
   def test_content_for_question_mark
50 86
     assert ! content_for?(:title)
51 87
     content_for :title, 'title'

0 notes on commit a2baf03

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