Permalink
Browse files

Move fragment caching from special helper methods to TemplateHandler.…

… Closes #10754 [Josh Peek]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8619 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 59f222d commit a8eb90fcee1efdc491c9eb030827b46929ea979c @jeremy jeremy committed Jan 10, 2008
@@ -74,32 +74,6 @@ def fragment_for(block, name = {}, options = nil) #:nodoc:
end
end
- # Called by CacheHelper#cache
- def cache_rxml_fragment(block, name = {}, options = nil) #:nodoc:
- fragment_for(block, name, options) do
- eval('xml.target!', block.binding)
- end
- end
-
- # Called by CacheHelper#cache
- def cache_rjs_fragment(block, name = {}, options = nil) #:nodoc:
- fragment_for(block, name, options) do
- begin
- debug_mode, ActionView::Base.debug_rjs = ActionView::Base.debug_rjs, false
- eval('page.to_s', block.binding)
- ensure
- ActionView::Base.debug_rjs = debug_mode
- end
- end
- end
-
- # Called by CacheHelper#cache
- def cache_erb_fragment(block, name = {}, options = nil) #:nodoc:
- fragment_for(block, name, options) do
- eval(ActionView::Base.erb_variable, block.binding)
- end
- end
-
# 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 unless cache_configured?
@@ -33,23 +33,8 @@ module CacheHelper
# <% end %>
def cache(name = {}, options = nil, &block)
template_extension = find_template_extension_for(first_render)[/\.(\w+)$/, 1].to_sym
-
- case template_extension
- when :erb, :rhtml
- @controller.cache_erb_fragment(block, name, options)
- when :rjs
- @controller.cache_rjs_fragment(block, name, options)
- when :builder, :rxml
- @controller.cache_rxml_fragment(block, name, options)
- else
- # Give template engine writers a hook to implement their own caching approach
- if @controller.respond_to?("cache_#{template_extension}_fragment")
- @controller.send!("cache_#{template_extension}_fragment", block, name, options)
- else
- # Let the ERb approach be the default one if the plugin doesn't specify it's own
- @controller.cache_erb_fragment(block, name, options)
- end
- end
+ handler = Base.handler_for_extension(template_extension)
+ handler.new(@controller).cache_fragment(block, name, options)
end
end
end
@@ -13,5 +13,9 @@ def render(template, local_assigns)
def compile(template)
end
+
+ # Called by CacheHelper#cache
+ def cache_fragment(block, name = {}, options = nil)
+ end
end
end
@@ -14,6 +14,12 @@ def compile(template)
template +
"\nxml.target!\n"
end
+
+ def cache_fragment(block, name = {}, options = nil)
+ @view.fragment_for(block, name, options) do
+ eval('xml.target!', block.binding)
+ end
+ end
end
end
end
@@ -25,6 +25,12 @@ class ERB < TemplateHandler
def compile(template)
::ERB.new(template, nil, @view.erb_trim_mode).src
end
+
+ def cache_fragment(block, name = {}, options = nil) #:nodoc:
+ @view.fragment_for(block, name, options) do
+ eval(ActionView::Base.erb_variable, block.binding)
+ end
+ end
end
end
end
@@ -9,6 +9,17 @@ def compile(template)
"controller.response.content_type ||= Mime::JS\n" +
"update_page do |page|\n#{template}\nend"
end
+
+ def cache_fragment(block, name = {}, options = nil) #:nodoc:
+ @view.fragment_for(block, name, options) do
+ begin
+ debug_mode, ActionView::Base.debug_rjs = ActionView::Base.debug_rjs, false
+ eval('page.to_s', block.binding)
+ ensure
+ ActionView::Base.debug_rjs = debug_mode
+ end
+ end
+ end
end
end
end
@@ -445,7 +445,7 @@ def test_cache_erb_fragment
_erbout = 'generated till now -> '
assert_equal( 'generated till now -> fragment content',
- @controller.cache_erb_fragment(Proc.new{ }, 'expensive'))
+ ActionView::TemplateHandlers::ERB.new(@controller).cache_fragment(Proc.new{ }, 'expensive'))
end
def test_cache_rxml_fragment
@@ -454,15 +454,15 @@ def test_cache_rxml_fragment
class << xml; def target!; to_s; end; end
assert_equal( 'generated till now -> fragment content',
- @controller.cache_rxml_fragment(Proc.new{ }, 'expensive'))
+ ActionView::TemplateHandlers::Builder.new(@controller).cache_fragment(Proc.new{ }, 'expensive'))
end
def test_cache_rjs_fragment
@store.write('views/expensive', 'fragment content')
page = 'generated till now -> '
assert_equal( 'generated till now -> fragment content',
- @controller.cache_rjs_fragment(Proc.new{ }, 'expensive'))
+ ActionView::TemplateHandlers::RJS.new(@controller).cache_fragment(Proc.new{ }, 'expensive'))
end
def test_cache_rjs_fragment_debug_mode_does_not_interfere
@@ -472,7 +472,7 @@ def test_cache_rjs_fragment_debug_mode_does_not_interfere
begin
debug_mode, ActionView::Base.debug_rjs = ActionView::Base.debug_rjs, true
assert_equal( 'generated till now -> fragment content',
- @controller.cache_rjs_fragment(Proc.new{ }, 'expensive'))
+ ActionView::TemplateHandlers::RJS.new(@controller).cache_fragment(Proc.new{ }, 'expensive'))
assert ActionView::Base.debug_rjs
ensure
ActionView::Base.debug_rjs = debug_mode

0 comments on commit a8eb90f

Please sign in to comment.