Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

read_ and write_fragment cache preserve html safety yet cache strings…

… only
  • Loading branch information...
commit 9b209e8cb89203b395622030bf526e3c3279722b 1 parent 056f957
@jeremy jeremy authored
View
14 actionpack/lib/action_controller/caching/fragments.rb
@@ -41,9 +41,7 @@ def fragment_for(buffer, name = {}, options = nil, &block) #:nodoc:
else
pos = buffer.length
block.call
- content = buffer[pos..-1]
- content = content.as_str if content.respond_to?(:as_str)
- write_fragment(name, content, options)
+ write_fragment(name, buffer[pos..-1], options)
end
else
block.call
@@ -54,9 +52,9 @@ def fragment_for(buffer, name = {}, options = nil, &block) #:nodoc:
def write_fragment(key, content, options = nil)
return content unless cache_configured?
- key = fragment_cache_key(key)
-
self.class.benchmark "Cached fragment miss: #{key}" do
+ key = fragment_cache_key(key)
+ content = content.html_safe.as_str if content.respond_to?(:html_safe)
cache_store.write(key, content, options)
end
@@ -67,10 +65,10 @@ def write_fragment(key, content, options = nil)
def read_fragment(key, options = nil)
return unless cache_configured?
- key = fragment_cache_key(key)
-
self.class.benchmark "Cached fragment hit: #{key}" do
- cache_store.read(key, options)
+ key = fragment_cache_key(key)
+ result = cache_store.read(key, options)
+ result.respond_to?(:html_safe) ? result.html_safe : result
end
end
View
14 actionpack/test/controller/caching_test.rb
@@ -621,6 +621,20 @@ def test_fragment_for
assert !fragment_computed
assert_equal 'generated till now -> fragment content', buffer
end
+
+ def test_html_safety
+ assert_nil @store.read('views/name')
+ content = 'value'.html_safe
+ assert_equal content, @controller.write_fragment('name', content)
+
+ cached = @store.read('views/name')
+ assert_equal content, cached
+ assert_equal String, cached.class
+
+ html_safe = @controller.read_fragment('name')
+ assert_equal content, html_safe
+ assert html_safe.html_safe?
+ end
end
class FunctionalCachingController < ActionController::Base
Please sign in to comment.
Something went wrong with that request. Please try again.