1
module ActionController #:nodoc:
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:
6
# class ListSweeper < ActionController::Caching::Sweeper
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) }
17
# The sweeper is assigned in the controllers that wish to have its job performed using the <tt>cache_sweeper</tt> class method:
19
# class ListsController < ApplicationController
20
# caches_action :index, :show, :public, :feed
21
# cache_sweeper :list_sweeper, :only => [ :edit, :destroy, :share ]
24
# In the example above, four actions are cached and three actions are responsible for expiring those caches.
26
# You can also name an explicit class in the declaration of a sweeper, which is needed if the sweeper is in a module:
28
# class ListsController < ApplicationController
29
# caches_action :index, :show, :public, :feed
30
# cache_sweeper OpenBar::Sweeper, :only => [ :edit, :destroy, :share ]
33
def self.included(base) #:nodoc:
34
base.extend(ClassMethods)
37
module ClassMethods #:nodoc:
38
def cache_sweeper(*sweepers)
39
configuration = sweepers.extract_options!
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
45
if sweeper_instance.is_a?(Sweeper)
46
around_filter(sweeper_instance, :only => configuration[:only])
48
after_filter(sweeper_instance, :only => configuration[:only])