Skip to content

Commit

Permalink
Merge pull request #2219 from kommen/fix_fragment_caching_squashed
Browse files Browse the repository at this point in the history
Fix fragment caching (squashed commits)
  • Loading branch information
spastorino committed Jul 23, 2011
2 parents b2285ba + 15c8bf1 commit f41a98e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/cache_helper.rb
Expand Up @@ -54,7 +54,7 @@ def fragment_for(name = {}, options = nil, &block) #:nodoc:
output_safe = output_buffer.html_safe? output_safe = output_buffer.html_safe?
fragment = output_buffer.slice!(pos..-1) fragment = output_buffer.slice!(pos..-1)
if output_safe if output_safe
self.output_buffer = output_buffer.html_safe self.output_buffer = output_buffer.class.new(output_buffer)
end end
controller.write_fragment(name, fragment, options) controller.write_fragment(name, fragment, options)
end end
Expand Down
49 changes: 49 additions & 0 deletions actionpack/test/controller/caching_test.rb
Expand Up @@ -742,6 +742,7 @@ def test_fragment_caching
expected_body = <<-CACHED expected_body = <<-CACHED
Hello Hello
This bit's fragment cached This bit's fragment cached
Ciao
CACHED CACHED
assert_equal expected_body, @response.body assert_equal expected_body, @response.body


Expand Down Expand Up @@ -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') assert_equal " <p>Builder</p>\n", @store.read('views/test.host/functional_caching/formatted_fragment_cached')
end end
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
@@ -1,2 +1,3 @@
Hello Hello
<%= cache do %>This bit's fragment cached<% end %> <%= cache do %>This bit's fragment cached<% end %>
<%= 'Ciao' %>

0 comments on commit f41a98e

Please sign in to comment.