~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/actionpack/lib/action_controller/caching/sweeping.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
module ActionController #:nodoc:
2
 
  module Caching
3
 
    # Sweepers are the terminators of the caching world and responsible for expiring caches when model objects change.
4
 
    # They do this by being half-observers, half-filters and implementing callbacks for both roles. A Sweeper example:
5
 
    #
6
 
    #   class ListSweeper < ActionController::Caching::Sweeper
7
 
    #     observe List, Item
8
 
    #
9
 
    #     def after_save(record)
10
 
    #       list = record.is_a?(List) ? record : record.list
11
 
    #       expire_page(:controller => "lists", :action => %w( show public feed ), :id => list.id)
12
 
    #       expire_action(:controller => "lists", :action => "all")
13
 
    #       list.shares.each { |share| expire_page(:controller => "lists", :action => "show", :id => share.url_key) }
14
 
    #     end
15
 
    #   end
16
 
    #
17
 
    # The sweeper is assigned in the controllers that wish to have its job performed using the <tt>cache_sweeper</tt> class method:
18
 
    #
19
 
    #   class ListsController < ApplicationController
20
 
    #     caches_action :index, :show, :public, :feed
21
 
    #     cache_sweeper :list_sweeper, :only => [ :edit, :destroy, :share ]
22
 
    #   end
23
 
    #
24
 
    # In the example above, four actions are cached and three actions are responsible for expiring those caches.
25
 
    #
26
 
    # You can also name an explicit class in the declaration of a sweeper, which is needed if the sweeper is in a module:
27
 
    #
28
 
    #   class ListsController < ApplicationController
29
 
    #     caches_action :index, :show, :public, :feed
30
 
    #     cache_sweeper OpenBar::Sweeper, :only => [ :edit, :destroy, :share ]
31
 
    #   end
32
 
    module Sweeping
33
 
      def self.included(base) #:nodoc:
34
 
        base.extend(ClassMethods)
35
 
      end
36
 
 
37
 
      module ClassMethods #:nodoc:
38
 
        def cache_sweeper(*sweepers)
39
 
          configuration = sweepers.extract_options!
40
 
 
41
 
          sweepers.each do |sweeper|
42
 
            ActiveRecord::Base.observers << sweeper if defined?(ActiveRecord) and defined?(ActiveRecord::Base)
43
 
            sweeper_instance = (sweeper.is_a?(Symbol) ? Object.const_get(sweeper.to_s.classify) : sweeper).instance
44
 
 
45
 
            if sweeper_instance.is_a?(Sweeper)
46
 
              around_filter(sweeper_instance, :only => configuration[:only])
47
 
            else
48
 
              after_filter(sweeper_instance, :only => configuration[:only])
49
 
            end
50
 
          end
51
 
        end
52
 
      end
53
 
    end
54
 
  end
55
 
end