Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix fragment caching (squashed commits) #2219

Merged
merged 1 commit into from

3 participants

@kommen

As requested by @fxn I squashed the commits from pull request #2150 into one commit.

Fix fragment cache helper regression on cache miss introduced with 03d01ec.

Contains following patches cherry-picked from @lhahne's 3-0-stable branch:

  • Added tests for the output_buffer returned by CacheHelper (c476a6b)
    The output_buffer returned by CacheHelper should be html_safe if the original buffer is html_safe.

  • made sure that the possible new output_buffer created by CacheHelper is of the same type as the original (39a4f67)

@kommen kommen Fix fragment cache helper regression on cache miss introduced with 03…
…d01ec.

Contains following patches cherry-picked from @lhahne's 3-0-stable branch:

* Added tests for the output_buffer returned by CacheHelper (c476a6b)
The output_buffer returned by CacheHelper should be html_safe if the original buffer is html_safe.

* made sure that the possible new output_buffer created by CacheHelper is of the same type as the original (39a4f67)
15c8bf1
@spastorino spastorino merged commit f41a98e into rails:3-1-stable
@spastorino
Owner

@kommen Can you provide the same fix for master? thanks

@spastorino
Owner

@kommen also we need a backport for 3-0-stable too in order to close 2080

@jeremy
Owner

This was applied to 3-1-stable, but not master.

fuuu

@kommen

@jeremy, that's right. I opend pull request #2224 back then, but as of now it's still open and nobody gave feedback.

@kommen kommen deleted the kommen:fix_fragment_caching_squashed branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 23, 2011
  1. @kommen

    Fix fragment cache helper regression on cache miss introduced with 03…

    kommen authored
    …d01ec.
    
    Contains following patches cherry-picked from @lhahne's 3-0-stable branch:
    
    * Added tests for the output_buffer returned by CacheHelper (c476a6b)
    The output_buffer returned by CacheHelper should be html_safe if the original buffer is html_safe.
    
    * made sure that the possible new output_buffer created by CacheHelper is of the same type as the original (39a4f67)
This page is out of date. Refresh to see the latest.
View
2  actionpack/lib/action_view/helpers/cache_helper.rb
@@ -54,7 +54,7 @@ def fragment_for(name = {}, options = nil, &block) #:nodoc:
output_safe = output_buffer.html_safe?
fragment = output_buffer.slice!(pos..-1)
if output_safe
- self.output_buffer = output_buffer.html_safe
+ self.output_buffer = output_buffer.class.new(output_buffer)
end
controller.write_fragment(name, fragment, options)
end
View
49 actionpack/test/controller/caching_test.rb
@@ -742,6 +742,7 @@ def test_fragment_caching
expected_body = <<-CACHED
Hello
This bit's fragment cached
+Ciao
CACHED
assert_equal expected_body, @response.body
@@ -783,3 +784,51 @@ def test_xml_formatted_fragment_caching
assert_equal " <p>Builder</p>\n", @store.read('views/test.host/functional_caching/formatted_fragment_cached')
end
end
+
+class CacheHelperOutputBufferTest < ActionController::TestCase
+
+ class MockController
+ def read_fragment(name, options)
+ return false
+ end
+
+ def write_fragment(name, fragment, options)
+ fragment
+ end
+ end
+
+ def setup
+ super
+ end
+
+ def test_output_buffer
+ output_buffer = ActionView::OutputBuffer.new
+ controller = MockController.new
+ cache_helper = Object.new
+ cache_helper.extend(ActionView::Helpers::CacheHelper)
+ cache_helper.expects(:controller).returns(controller).at_least(0)
+ cache_helper.expects(:output_buffer).returns(output_buffer).at_least(0)
+ # if the output_buffer is changed, the new one should be html_safe and of the same type
+ cache_helper.expects(:output_buffer=).with(responds_with(:html_safe?, true)).with(instance_of(output_buffer.class)).at_least(0)
+
+ assert_nothing_raised do
+ cache_helper.send :fragment_for, 'Test fragment name', 'Test fragment', &Proc.new{ nil }
+ end
+ end
+
+ def test_safe_buffer
+ output_buffer = ActiveSupport::SafeBuffer.new
+ controller = MockController.new
+ cache_helper = Object.new
+ cache_helper.extend(ActionView::Helpers::CacheHelper)
+ cache_helper.expects(:controller).returns(controller).at_least(0)
+ cache_helper.expects(:output_buffer).returns(output_buffer).at_least(0)
+ # if the output_buffer is changed, the new one should be html_safe and of the same type
+ cache_helper.expects(:output_buffer=).with(responds_with(:html_safe?, true)).with(instance_of(output_buffer.class)).at_least(0)
+
+ assert_nothing_raised do
+ cache_helper.send :fragment_for, 'Test fragment name', 'Test fragment', &Proc.new{ nil }
+ end
+ end
+
+end
View
1  actionpack/test/fixtures/functional_caching/fragment_cached.html.erb
@@ -1,2 +1,3 @@
Hello
<%= cache do %>This bit's fragment cached<% end %>
+<%= 'Ciao' %>
Something went wrong with that request. Please try again.