Ticket #11184: 0001-Improve-ActionCaching-s-format-handling.patch
| File 0001-Improve-ActionCaching-s-format-handling.patch, 6.5 kB (added by Catfish, 4 months ago) |
|---|
-
a/actionpack/lib/action_controller/caching/actions.rb
old new 64 64 65 65 if options[:action].is_a?(Array) 66 66 options[:action].dup.each do |action| 67 expire_fragment(ActionCachePath.path_for(self, options.merge({ :action => action }) ))67 expire_fragment(ActionCachePath.path_for(self, options.merge({ :action => action }), false)) 68 68 end 69 69 else 70 expire_fragment(ActionCachePath.path_for(self, options ))70 expire_fragment(ActionCachePath.path_for(self, options, false)) 71 71 end 72 72 end 73 73 … … 111 111 attr_reader :path, :extension 112 112 113 113 class << self 114 def path_for(controller, options )115 new(controller, options ).path114 def path_for(controller, options, infer_extension=true) 115 new(controller, options, infer_extension).path 116 116 end 117 117 end 118 119 def initialize(controller, options = {}) 120 @extension = extract_extension(controller.request.path) 118 119 # When true, infer_extension will look up the cache path extension from the request's path & format. 120 # This is desirable when reading and writing the cache, but not when expiring the cache - expire_action should expire the same files regardless of the request format. 121 def initialize(controller, options = {}, infer_extension=true) 122 if infer_extension and options.is_a? Hash 123 request_extension = extract_extension(controller.request) 124 options = options.reverse_merge(:format => request_extension) 125 end 121 126 path = controller.url_for(options).split('://').last 122 127 normalize!(path) 123 add_extension!(path, @extension) 128 if infer_extension 129 @extension = request_extension 130 add_extension!(path, @extension) 131 end 124 132 @path = URI.unescape(path) 125 133 end 126 134 … … 130 138 end 131 139 132 140 def add_extension!(path, extension) 133 path << ".#{extension}" if extension 141 path << ".#{extension}" if extension and !path.ends_with?(extension) 134 142 end 135 136 def extract_extension( file_path)143 144 def extract_extension(request) 137 145 # Don't want just what comes after the last '.' to accommodate multi part extensions 138 146 # such as tar.gz. 139 file_path[/^[^.]+\.(.+)$/, 1] 147 extension = request.path[/^[^.]+\.(.+)$/, 1] 148 149 # If there's no extension in the path, check request.format 150 if extension.nil? 151 extension = request.format.to_sym.to_s 152 if extension=='all' 153 extension = nil 154 end 155 end 156 extension 140 157 end 141 158 end 142 159 end -
a/actionpack/test/controller/caching_test.rb
old new 186 186 expire_action :controller => 'action_caching_test', :action => 'index' 187 187 render :nothing => true 188 188 end 189 def expire_xml 190 expire_action :controller => 'action_caching_test', :action => 'index', :format => 'xml' 191 render :nothing => true 192 end 189 193 end 190 194 191 195 class MockTime < Time … … 211 215 mocked_path = @mock_path 212 216 Object.new.instance_eval(<<-EVAL) 213 217 def path; '#{@mock_path}' end 218 def format; 'all' end 214 219 self 215 220 EVAL 216 221 end … … 311 316 assert_equal new_cached_time, @response.body 312 317 end 313 318 319 def test_cache_expiration_isnt_affected_by_request_format 320 get :index 321 cached_time = content_to_cache 322 reset! 323 324 @request.set_REQUEST_URI "/action_caching_test/expire.xml" 325 get :expire, :format => :xml 326 reset! 327 328 get :index 329 new_cached_time = content_to_cache 330 assert_not_equal cached_time, @response.body 331 end 332 314 333 def test_cache_is_scoped_by_subdomain 315 334 @request.host = 'jamis.hostname.com' 316 335 get :index … … 355 374 end 356 375 357 376 def test_xml_version_of_resource_is_treated_as_different_cache 358 @mock_controller.mock_url_for = 'http://example.org/posts/' 359 @mock_controller.mock_path = '/posts/index.xml' 360 path_object = @path_class.new(@mock_controller, {}) 361 assert_equal 'xml', path_object.extension 362 assert_equal 'example.org/posts/index.xml', path_object.path 377 with_routing do |set| 378 ActionController::Routing::Routes.draw do |map| 379 map.connect ':controller/:action.:format' 380 map.connect ':controller/:action' 381 end 382 383 get :index, :format => 'xml' 384 cached_time = content_to_cache 385 assert_equal cached_time, @response.body 386 assert fragment_exist?('hostname.com/action_caching_test/index.xml') 387 reset! 388 389 get :index, :format => 'xml' 390 assert_equal cached_time, @response.body 391 assert_equal 'application/xml', @response.content_type 392 reset! 393 394 @request.env['HTTP_ACCEPT'] = "application/xml" 395 get :index 396 assert_equal cached_time, @response.body 397 assert_equal 'application/xml', @response.content_type 398 reset! 399 400 get :expire_xml 401 reset! 402 403 get :index, :format => 'xml' 404 assert_not_equal cached_time, @response.body 405 end 363 406 end 364 407 365 408 def test_correct_content_type_is_returned_for_cache_hit