Skip to content
Browse files

Ensure that while caching a page rails takes into

account the resolved mime type for the request

This is a port of fix on master to 3-0-stable

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent 6dcde27 commit 890d005d50ddca652ccb2239a1e1860a4c76517d Neeraj Singh committed with josevalim Dec 9, 2010
Showing with 28 additions and 7 deletions.
  1. +14 −7 actionpack/lib/action_controller/caching/pages.rb
  2. +14 −0 actionpack/test/controller/caching_test.rb
View
21 actionpack/lib/action_controller/caching/pages.rb
@@ -71,9 +71,9 @@ def expire_page(path)
# Manually cache the +content+ in the key determined by +path+. Example:
# cache_page "I'm the cached content", "/lists/show"
- def cache_page(content, path)
+ def cache_page(content, path, extension = nil)
return unless perform_caching
- path = page_cache_path(path)
+ path = page_cache_path(path, extension)
instrument_page_cache :write_page, path do
FileUtils.makedirs(File.dirname(path))
@@ -98,14 +98,16 @@ def caches_page(*actions)
end
private
- def page_cache_file(path)
+ def page_cache_file(path, extension)
name = (path.empty? || path == "/") ? "/index" : URI.unescape(path.chomp('/'))
- name << page_cache_extension unless (name.split('/').last || name).include? '.'
+ unless (name.split('/').last || name).include? '.'
+ name << (extension || self.page_cache_extension)
+ end
return name
end
- def page_cache_path(path)
- page_cache_directory + page_cache_file(path)
+ def page_cache_path(path, extension = nil)
+ page_cache_directory + page_cache_file(path, extension)
end
def instrument_page_cache(name, path)
@@ -146,7 +148,12 @@ def cache_page(content = nil, options = nil)
request.path
end
- self.class.cache_page(content || response.body, path)
+
+ if (type = Mime::LOOKUP[self.content_type]) && (type_symbol = type.symbol).present?
+ extension = ".#{type_symbol}"
+ end
+
+ self.class.cache_page(content || response.body, path, extension)
end
private
View
14 actionpack/test/controller/caching_test.rb
@@ -16,7 +16,14 @@ class CachingController < ActionController::Base
class PageCachingTestController < CachingController
caches_page :ok, :no_content, :if => Proc.new { |c| !c.request.format.json? }
caches_page :found, :not_found
+ caches_page :about_me
+ def about_me
+ respond_to do |format|
+ format.html {render :text => 'I am html'}
+ format.xml {render :text => 'I am xml'}
+ end
+ end
def ok
head :ok
@@ -74,6 +81,13 @@ def teardown
@controller.perform_caching = false
end
+ def test_should_obey_http_accept_attribute
+ @request.env['HTTP_ACCEPT'] = 'text/xml'
+ get :about_me
+ assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/about_me.xml")
+ assert_equal 'I am xml', @response.body
+ end
+
def test_page_caching_resources_saves_to_correct_path_with_extension_even_if_default_route
with_routing do |set|
set.draw do |map|

0 comments on commit 890d005

Please sign in to comment.
Something went wrong with that request. Please try again.