From 610b3a7993119d511d3159d987045b34e161fd6e Mon Sep 17 00:00:00 2001 From: Paul Horsfall Date: Fri, 18 Apr 2008 19:01:46 +0100 Subject: [PATCH] Add an :if option to caches_page to allow conditional caching of pages --- actionpack/lib/action_controller/caching/pages.rb | 12 ++++++++++-- actionpack/test/controller/caching_test.rb | 9 ++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_controller/caching/pages.rb b/actionpack/lib/action_controller/caching/pages.rb index 755f1e4..d8505d7 100644 --- a/actionpack/lib/action_controller/caching/pages.rb +++ b/actionpack/lib/action_controller/caching/pages.rb @@ -78,10 +78,18 @@ module ActionController #:nodoc: # Caches the +actions+ using the page-caching approach that'll store the cache in a path within the page_cache_directory that # matches the triggering url. + # + # Usage: + # + # # cache the index action + # caches_page :index + # + # # cache the index action except for JSON requests + # caches_page :index, :if => Proc.new { |c| !c.request.format.json? } def caches_page(*actions) return unless perform_caching - actions = actions.map(&:to_s) - after_filter { |c| c.cache_page if actions.include?(c.action_name) } + options = actions.extract_options! + after_filter(:only => actions) { |c| c.cache_page if options[:if].nil? || options[:if].call(c) } end private diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index 7bd64e2..ddc1c68 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -8,7 +8,8 @@ ActionController::Base.page_cache_directory = FILE_STORE_PATH ActionController::Base.cache_store = :file_store, FILE_STORE_PATH class PageCachingTestController < ActionController::Base - caches_page :ok, :no_content, :found, :not_found + caches_page :ok, :no_content, :if => Proc.new { |c| !c.request.format.json? } + caches_page :found, :not_found def ok head :ok @@ -127,6 +128,12 @@ class PageCachingTest < Test::Unit::TestCase end end end + + def test_page_caching_conditional_options + @request.env['HTTP_ACCEPT'] = 'application/json' + get :ok + assert_page_not_cached :ok + end private def assert_page_cached(action, message = "#{action} should have been cached") -- 1.5.4.5