public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
mephisto / lib / mephisto / caching_methods.rb
100644 40 lines (36 sloc) 1.346 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
module Mephisto # :nodoc:
  module CachingMethods
    def self.included(base)
      base.helper_method :cached_references
      base.extend ClassMethods
      base.alias_method_chain :caching_allowed, :skipping
      base.send :attr_writer, :cached_references
    end
 
    module ClassMethods
      # Caches the actions using the page-caching approach and saves the references for the page
      def caches_page_with_references(*actions)
        return unless perform_caching
        caches_page *actions
        after_filter :cache_page_with_references, :only => actions
      end
    end
 
    protected
      # An array of the current page's references.
      #
      # self.cached_references << @post
      # self.cached_references += @post.comments
      #
      def cached_references
        @cached_references ||= []
      end
 
      def caching_allowed_with_skipping
        caching_allowed_without_skipping && !@skip_caching
      end
 
      # Saves a CachedPage for the current request with the current references. This is called in an after filter if #caches_page_with_references
      # is used.
      def cache_page_with_references
        return unless perform_caching && caching_allowed
        CachedPage.create_by_url(site, url_for(:only_path => true, :skip_relative_url_root => true), cached_references)
      end
  end
end