Skip to content

Commit

Permalink
Add conditional options to caches_page method [#25 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
Paul Horsfall authored and josh committed Apr 19, 2008
1 parent 3f8d3cd commit 14a4080
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions 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]
Expand Down
12 changes: 10 additions & 2 deletions actionpack/lib/action_controller/caching/pages.rb
Expand Up @@ -78,10 +78,18 @@ def cache_page(content, path)

# 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
Expand Down
9 changes: 8 additions & 1 deletion actionpack/test/controller/caching_test.rb
Expand Up @@ -8,7 +8,8 @@
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
Expand Down Expand Up @@ -127,6 +128,12 @@ def test_should_cache_ok_at_custom_path
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")
Expand Down

0 comments on commit 14a4080

Please sign in to comment.