Create a gist now

Instantly share code, notes, and snippets.

def self.setup_rails_for_action_cache_options
::ActionController::Caching::Actions::ActionCacheFilter.class_eval do
def initialize(*actions, &block)
if [].respond_to?(:extract_options!)
@options = actions.extract_options!
@actions = actions.inject(@options.except(:cache_path)) do |hsh, action|
action.is_a?(Hash) ? hsh.merge(action) : hsh.merge(action => { :ttl => nil })
end
@options.slice!(:cache_path)
else
@actions = actions.inject({}) do |hsh, action|
action.is_a?(Hash) ? hsh.merge(action) : hsh.merge(action => { :ttl => nil })
end
end
end
def before(controller)
if !@actions.include?(controller.action_name.intern)
#return #this line has been commented out
end
if @options
action_cache_path = ActionController::Caching::Actions::ActionCachePath.new(controller, path_options_for(controller, @options))
else
action_cache_path = ActionController::Caching::Actions::ActionCachePath.new(controller)
end
#if conditional = @actions[controller.action_name.intern][:if] # existing code
conditional = @options[:if] # revised code
if conditional = @options[:if]
conditional = conditional.respond_to?(:call) ? conditional.call(controller) : controller.send(conditional)
end
@actions.delete(controller.action_name.intern) if conditional == false
tmp = action_cache_path.path
cache = controller.read_fragment(tmp)
if cache && (conditional || conditional.nil?)
controller.rendered_action_cache = true
if method(:set_content_type!).arity == 2
set_content_type!(controller, action_cache_path.extension)
else
set_content_type!(action_cache_path)
end
controller.send(:render, :text => cache)
false
else
controller.action_cache_path = action_cache_path if controller.respond_to? :action_cache_path
end
end
def after(controller)
puts 'after in plugin is called'
#return if !@actions.include?(controller.action_name.intern) || controller.rendered_action_cache # existing code
return if controller.rendered_action_cache || !caching_allowed(controller) # revised code
path = controller.respond_to?(:action_cache_path) ? controller.action_cache_path.path : ActionController::Caching::Actions::ActionCachePath.path_for(controller)
controller.write_fragment(path, controller.response.body, action_ttl(controller))
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment