| 1 |
module ActionController |
|---|
| 2 |
module Caching |
|---|
| 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 |
module Sweeping |
|---|
| 33 |
def self.included(base) |
|---|
| 34 |
base.extend(ClassMethods) |
|---|
| 35 |
end |
|---|
| 36 |
|
|---|
| 37 |
module ClassMethods |
|---|
| 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(Inflector.classify(sweeper)) : 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 |
|
|---|
| 55 |
if defined?(ActiveRecord) and defined?(ActiveRecord::Observer) |
|---|
| 56 |
class Sweeper < ActiveRecord::Observer |
|---|
| 57 |
attr_accessor :controller |
|---|
| 58 |
|
|---|
| 59 |
def before(controller) |
|---|
| 60 |
self.controller = controller |
|---|
| 61 |
callback(:before) if controller.perform_caching |
|---|
| 62 |
end |
|---|
| 63 |
|
|---|
| 64 |
def after(controller) |
|---|
| 65 |
callback(:after) if controller.perform_caching |
|---|
| 66 |
|
|---|
| 67 |
self.controller = nil |
|---|
| 68 |
end |
|---|
| 69 |
|
|---|
| 70 |
protected |
|---|
| 71 |
|
|---|
| 72 |
def action_path_for(options) |
|---|
| 73 |
ActionController::Caching::Actions::ActionCachePath.path_for(controller, options) |
|---|
| 74 |
end |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
def assigns(key) |
|---|
| 78 |
controller.instance_variable_get("@#{key}") |
|---|
| 79 |
end |
|---|
| 80 |
|
|---|
| 81 |
private |
|---|
| 82 |
def callback(timing) |
|---|
| 83 |
controller_callback_method_name = "#{timing}_#{controller.controller_name.underscore}" |
|---|
| 84 |
action_callback_method_name = "#{controller_callback_method_name}_#{controller.action_name}" |
|---|
| 85 |
|
|---|
| 86 |
send!(controller_callback_method_name) if respond_to?(controller_callback_method_name, true) |
|---|
| 87 |
send!(action_callback_method_name) if respond_to?(action_callback_method_name, true) |
|---|
| 88 |
end |
|---|
| 89 |
|
|---|
| 90 |
def method_missing(method, *arguments) |
|---|
| 91 |
return if @controller.nil? |
|---|
| 92 |
@controller.send!(method, *arguments) |
|---|
| 93 |
end |
|---|
| 94 |
end |
|---|
| 95 |
end |
|---|
| 96 |
end |
|---|
| 97 |
end |
|---|