mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Added custom path cache_page/expire_page parameters in addition to the options hashes [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6868 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
cd599aad71
commit
d3b5db8919
4 changed files with 49 additions and 5 deletions
|
@ -1,5 +1,11 @@
|
||||||
*SVN*
|
*SVN*
|
||||||
|
|
||||||
|
* Added custom path cache_page/expire_page parameters in addition to the options hashes [DHH]. Example:
|
||||||
|
|
||||||
|
def index
|
||||||
|
caches_page(response.body, "/index.html")
|
||||||
|
end
|
||||||
|
|
||||||
* Action Caching speedup. #8231 [skaes]
|
* Action Caching speedup. #8231 [skaes]
|
||||||
|
|
||||||
* Wordsmith resources documentation. #8484 [marclove]
|
* Wordsmith resources documentation. #8484 [marclove]
|
||||||
|
|
|
@ -118,6 +118,8 @@ module ActionController #:nodoc:
|
||||||
# expire_page :controller => "lists", :action => "show"
|
# expire_page :controller => "lists", :action => "show"
|
||||||
def expire_page(options = {})
|
def expire_page(options = {})
|
||||||
return unless perform_caching
|
return unless perform_caching
|
||||||
|
|
||||||
|
if options.is_a?(Hash)
|
||||||
if options[:action].is_a?(Array)
|
if options[:action].is_a?(Array)
|
||||||
options[:action].dup.each do |action|
|
options[:action].dup.each do |action|
|
||||||
self.class.expire_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :action => action)))
|
self.class.expire_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :action => action)))
|
||||||
|
@ -125,6 +127,9 @@ module ActionController #:nodoc:
|
||||||
else
|
else
|
||||||
self.class.expire_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true)))
|
self.class.expire_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true)))
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
self.class.expire_page(options)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Manually cache the +content+ in the key determined by +options+. If no content is provided, the contents of response.body is used
|
# Manually cache the +content+ in the key determined by +options+. If no content is provided, the contents of response.body is used
|
||||||
|
@ -132,7 +137,14 @@ module ActionController #:nodoc:
|
||||||
# cache_page "I'm the cached content", :controller => "lists", :action => "show"
|
# cache_page "I'm the cached content", :controller => "lists", :action => "show"
|
||||||
def cache_page(content = nil, options = {})
|
def cache_page(content = nil, options = {})
|
||||||
return unless perform_caching && caching_allowed
|
return unless perform_caching && caching_allowed
|
||||||
self.class.cache_page(content || response.body, url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format])))
|
|
||||||
|
if options.is_a?(Hash)
|
||||||
|
path = url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format]))
|
||||||
|
else
|
||||||
|
path = options
|
||||||
|
end
|
||||||
|
|
||||||
|
self.class.cache_page(content || response.body, path)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -9,6 +9,8 @@ require 'action_controller'
|
||||||
require 'action_controller/cgi_ext'
|
require 'action_controller/cgi_ext'
|
||||||
require 'action_controller/test_process'
|
require 'action_controller/test_process'
|
||||||
|
|
||||||
|
require 'ruby-debug'
|
||||||
|
|
||||||
# Show backtraces for deprecated behavior for quicker cleanup.
|
# Show backtraces for deprecated behavior for quicker cleanup.
|
||||||
ActiveSupport::Deprecation.debug = true
|
ActiveSupport::Deprecation.debug = true
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,16 @@ class PageCachingTestController < ActionController::Base
|
||||||
def not_found
|
def not_found
|
||||||
head :not_found
|
head :not_found
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def custom_path
|
||||||
|
render :text => "Super soaker"
|
||||||
|
cache_page("Super soaker", "/index.html")
|
||||||
|
end
|
||||||
|
|
||||||
|
def expire_custom_path
|
||||||
|
expire_page("/index.html")
|
||||||
|
head :ok
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class PageCachingTest < Test::Unit::TestCase
|
class PageCachingTest < Test::Unit::TestCase
|
||||||
|
@ -69,6 +79,19 @@ class PageCachingTest < Test::Unit::TestCase
|
||||||
assert_page_cached :ok, "get with ok status should have been cached"
|
assert_page_cached :ok, "get with ok status should have been cached"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_should_cache_with_custom_path
|
||||||
|
get :custom_path
|
||||||
|
assert File.exist?("#{FILE_STORE_PATH}/index.html")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_should_expire_cache_with_custom_path
|
||||||
|
get :custom_path
|
||||||
|
assert File.exist?("#{FILE_STORE_PATH}/index.html")
|
||||||
|
|
||||||
|
get :expire_custom_path
|
||||||
|
assert !File.exist?("#{FILE_STORE_PATH}/index.html")
|
||||||
|
end
|
||||||
|
|
||||||
[:ok, :no_content, :found, :not_found].each do |status|
|
[:ok, :no_content, :found, :not_found].each do |status|
|
||||||
[:get, :post, :put, :delete].each do |method|
|
[:get, :post, :put, :delete].each do |method|
|
||||||
unless method == :get and status == :ok
|
unless method == :get and status == :ok
|
||||||
|
@ -96,6 +119,7 @@ class PageCachingTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
class ActionCachingTestController < ActionController::Base
|
class ActionCachingTestController < ActionController::Base
|
||||||
caches_action :index
|
caches_action :index
|
||||||
caches_action :show, :cache_path => 'http://test.host/custom/show'
|
caches_action :show, :cache_path => 'http://test.host/custom/show'
|
||||||
|
|
Loading…
Reference in a new issue