Skip to content

Commit

Permalink
read_ and write_fragment cache preserve html safety yet cache strings…
Browse files Browse the repository at this point in the history
… only
  • Loading branch information
jeremy committed Mar 15, 2010
1 parent 056f957 commit 9b209e8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
14 changes: 6 additions & 8 deletions actionpack/lib/action_controller/caching/fragments.rb
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down
14 changes: 14 additions & 0 deletions actionpack/test/controller/caching_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 9b209e8

Please sign in to comment.