aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorPaul Horsfall <horsfallp@gmail.com>2008-04-19 16:19:47 -0500
committerJoshua Peek <josh@joshpeek.com>2008-04-19 16:21:34 -0500
commit14a40804a29a57ad05ca6bffbe1e5334089593a9 (patch)
tree80e2d5f38334327a70a368cc2e57fc8c2e8f7c1c /actionpack
parent3f8d3cd04ff0bd7cbf70c11d49a3dc009dfa98a0 (diff)
downloadrails-14a40804a29a57ad05ca6bffbe1e5334089593a9.tar.gz
rails-14a40804a29a57ad05ca6bffbe1e5334089593a9.tar.bz2
rails-14a40804a29a57ad05ca6bffbe1e5334089593a9.zip
Add conditional options to caches_page method [#25 state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_controller/caching/pages.rb12
-rw-r--r--actionpack/test/controller/caching_test.rb9
3 files changed, 20 insertions, 3 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 4694c0e996..9c72fd945a 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add conditional options to caches_page method. [Paul Horsfall]
+
* Move missing template logic to ActionView. [Pratik]
* Introduce ActionView::InlineTemplate class. [Pratik]
diff --git a/actionpack/lib/action_controller/caching/pages.rb b/actionpack/lib/action_controller/caching/pages.rb
index 755f1e4e0a..7aa6ce154b 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}.merge(options)) { |c| c.cache_page }
end
private
diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb
index 7bd64e2870..ddc1c68383 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")