aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_controller/caching.rb32
-rw-r--r--actionpack/lib/action_view/helpers/cache_helper.rb16
2 files changed, 44 insertions, 4 deletions
diff --git a/actionpack/lib/action_controller/caching.rb b/actionpack/lib/action_controller/caching.rb
index e3e48f660b..024f633d7c 100644
--- a/actionpack/lib/action_controller/caching.rb
+++ b/actionpack/lib/action_controller/caching.rb
@@ -371,11 +371,10 @@ module ActionController #:nodoc:
name.is_a?(Hash) ? url_for(name).split("://").last : name
end
- # Called by CacheHelper#cache
- def cache_erb_fragment(block, name = {}, options = nil)
+ def fragment_for(block, name = {}, options = nil) #:nodoc:
unless perform_caching then block.call; return end
- buffer = eval(ActionView::Base.erb_variable, block.binding)
+ buffer = yield
if cache = read_fragment(name, options)
buffer.concat(cache)
@@ -386,6 +385,33 @@ 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>name</tt> (see <tt>expire_fragment</tt> for acceptable formats)
def write_fragment(name, content, options = nil)
return unless perform_caching
diff --git a/actionpack/lib/action_view/helpers/cache_helper.rb b/actionpack/lib/action_view/helpers/cache_helper.rb
index cf5420a35e..87a8e8dc98 100644
--- a/actionpack/lib/action_view/helpers/cache_helper.rb
+++ b/actionpack/lib/action_view/helpers/cache_helper.rb
@@ -32,7 +32,21 @@ module ActionView
# <i>Topics listed alphabetically</i>
# <% end %>
def cache(name = {}, &block)
- @controller.cache_erb_fragment(block, name)
+ template_extension = first_render[/\.(\w+)$/, 1].to_sym
+ case template_extension
+ when :erb, :rhtml
+ @controller.cache_erb_fragment(block, name)
+ when :rjs
+ @controller.cache_rjs_fragment(block, name)
+ when :builder, :rxml
+ @controller.cache_rxml_fragment(block, name)
+ else
+ # do a last ditch effort for those brave souls using
+ # different template engines. This should give plugin
+ # writters a simple hook.
+ raise "fragment caching not supported for #{template_extension} files." unless @controller.respond_to?("cache_#{template_extension}_fragment")
+ @controller.send "cache_#{template_extension}_fragment", block, name
+ end
end
end
end