public
Description: Add per-store options to caches_action in Rails 2.1
Homepage:
Clone URL: git://github.com/nickpad/rails-caches-action-patch.git
Anton Ageev (author)
Mon Oct 27 14:43:01 -0700 2008
nickpad (committer)
Mon Oct 27 15:21:33 -0700 2008
100644 40 lines (34 sloc) 1.625 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Only apply this patch if Rails version is 2.1.0, 2.1.1 or 2.1.2:
if ['2.1.0', '2.1.1', '2.1.2'].include?(Rails.version)
 
  module ::ActionController::Caching::Actions::ClassMethods
    # Declares that +actions+ should be cached.
    # See ActionController::Caching::Actions for details.
    def caches_action(*actions)
      return unless cache_configured?
      options = actions.extract_options!
      filter_options = { :only => actions, :if => options.delete(:if), :unless => options.delete(:unless) }
 
      cache_filter = ::ActionController::Caching::Actions::ActionCacheFilter.new(:layout => options.delete(:layout), :cache_path => options.delete(:cache_path), :store_options => options)
      around_filter(cache_filter, filter_options)
    end
  end
 
  module ::ActionController::Caching::Actions
    class ActionCacheFilter
      def before(controller)
        cache_path = ActionCachePath.new(controller, path_options_for(controller, @options.slice(:cache_path)))
        if cache = controller.read_fragment(cache_path.path, @options[:store_options])
          controller.rendered_action_cache = true
          set_content_type!(controller, cache_path.extension)
          options = { :text => cache }
          controller.send!(:render, options)
          false
        else
          controller.action_cache_path = cache_path
        end
      end
 
      def after(controller)
        return if controller.rendered_action_cache || !caching_allowed(controller)
        controller.write_fragment(controller.action_cache_path.path, controller.response.body, @options[:store_options])
      end
    end
  end
 
end