1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

read_ and write_fragment cache preserve html safety yet cache strings only

This commit is contained in:
Jeremy Kemper 2010-03-14 18:55:13 -07:00
parent 95bd56e3cc
commit 16572fd46e
2 changed files with 19 additions and 6 deletions

View file

@ -41,9 +41,7 @@ module ActionController #: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
@ -53,9 +51,10 @@ module ActionController #:nodoc:
# Writes <tt>content</tt> to the location signified by <tt>key</tt> (see <tt>expire_fragment</tt> for acceptable formats)
def write_fragment(key, content, options = nil)
return content unless cache_configured?
key = fragment_cache_key(key)
key = fragment_cache_key(key)
instrument_fragment_cache :write_fragment, key do
content = content.html_safe.as_str if content.respond_to?(:html_safe)
cache_store.write(key, content, options)
end
content
@ -64,10 +63,11 @@ module ActionController #:nodoc:
# Reads a cached fragment from the location signified by <tt>key</tt> (see <tt>expire_fragment</tt> for acceptable formats)
def read_fragment(key, options = nil)
return unless cache_configured?
key = fragment_cache_key(key)
key = fragment_cache_key(key)
instrument_fragment_cache :read_fragment, key do
cache_store.read(key, options)
result = cache_store.read(key, options)
result.respond_to?(:html_safe) ? result.html_safe : result
end
end

View file

@ -634,6 +634,19 @@ class FragmentCachingTest < ActionController::TestCase
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 < CachingController