diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-01-10 03:17:20 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-01-10 03:17:20 +0000 |
commit | a8eb90fcee1efdc491c9eb030827b46929ea979c (patch) | |
tree | aad46c0c0e483ad77a129777f1d4a641c1d334c0 | |
parent | 59f222dbf7c05e3d1f9e71831bc79e2a5ed4a5b5 (diff) | |
download | rails-a8eb90fcee1efdc491c9eb030827b46929ea979c.tar.gz rails-a8eb90fcee1efdc491c9eb030827b46929ea979c.tar.bz2 rails-a8eb90fcee1efdc491c9eb030827b46929ea979c.zip |
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
7 files changed, 33 insertions, 47 deletions
diff --git a/actionpack/lib/action_controller/caching/fragments.rb b/actionpack/lib/action_controller/caching/fragments.rb index 868af19780..e4d8678a07 100644 --- a/actionpack/lib/action_controller/caching/fragments.rb +++ b/actionpack/lib/action_controller/caching/fragments.rb @@ -74,32 +74,6 @@ module ActionController #: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? diff --git a/actionpack/lib/action_view/helpers/cache_helper.rb b/actionpack/lib/action_view/helpers/cache_helper.rb index 783a5acdbc..059f2edf0f 100644 --- a/actionpack/lib/action_view/helpers/cache_helper.rb +++ b/actionpack/lib/action_view/helpers/cache_helper.rb @@ -33,23 +33,8 @@ module ActionView # <% 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 diff --git a/actionpack/lib/action_view/template_handler.rb b/actionpack/lib/action_view/template_handler.rb index b9f4330a23..8ce31ade49 100644 --- a/actionpack/lib/action_view/template_handler.rb +++ b/actionpack/lib/action_view/template_handler.rb @@ -13,5 +13,9 @@ module ActionView def compile(template) end + + # Called by CacheHelper#cache + def cache_fragment(block, name = {}, options = nil) + end end end diff --git a/actionpack/lib/action_view/template_handlers/builder.rb b/actionpack/lib/action_view/template_handlers/builder.rb index 0f49d6ab31..88044917dd 100644 --- a/actionpack/lib/action_view/template_handlers/builder.rb +++ b/actionpack/lib/action_view/template_handlers/builder.rb @@ -14,6 +14,12 @@ module ActionView 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 diff --git a/actionpack/lib/action_view/template_handlers/erb.rb b/actionpack/lib/action_view/template_handlers/erb.rb index 87cb09952c..26cca586ba 100644 --- a/actionpack/lib/action_view/template_handlers/erb.rb +++ b/actionpack/lib/action_view/template_handlers/erb.rb @@ -25,6 +25,12 @@ module ActionView 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 diff --git a/actionpack/lib/action_view/template_handlers/rjs.rb b/actionpack/lib/action_view/template_handlers/rjs.rb index 4ca9fc3277..bd51cc4d39 100644 --- a/actionpack/lib/action_view/template_handlers/rjs.rb +++ b/actionpack/lib/action_view/template_handlers/rjs.rb @@ -9,6 +9,17 @@ module ActionView "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 diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index 12397ee8c0..9d4dc4d01c 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -445,7 +445,7 @@ class FragmentCachingTest < Test::Unit::TestCase _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,7 +454,7 @@ class FragmentCachingTest < Test::Unit::TestCase 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 @@ -462,7 +462,7 @@ class FragmentCachingTest < Test::Unit::TestCase 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 @@ class FragmentCachingTest < Test::Unit::TestCase 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 |