Ticket #11184: caching_extensions.diff
| File caching_extensions.diff, 6.4 kB (added by Catfish, 6 months ago) |
|---|
-
a/actionpack/lib/action_controller/caching/actions.rb
old new 31 31 # before_filter :authenticate, :except => :public 32 32 # caches_page :public 33 33 # caches_action :show, :cache_path => { :project => 1 } 34 # caches_action : show, :cache_path => Proc.new { |controller|34 # caches_action :list, :cache_path => Proc.new { |controller| 35 35 # controller.params[:user_id] ? 36 36 # controller.send(:user_list_url, c.params[:user_id], c.params[:id]) : 37 37 # controller.send(:list_url, c.params[:id]) } … … 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 … … 115 115 attr_reader :path, :extension 116 116 117 117 class << self 118 def path_for(controller, options )119 new(controller, options ).path118 def path_for(controller, options, infer_extension=true) 119 new(controller, options, infer_extension).path 120 120 end 121 121 end 122 122 123 def initialize(controller, options = {}) 124 @extension = extract_extension(controller.request.path) 123 # When true, infer_extension will look up the cache path extension from the request's path & format. 124 # 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. 125 def initialize(controller, options = {}, infer_extension=true) 126 if infer_extension and options.is_a? Hash 127 request_extension = extract_extension(controller.request) 128 options = options.reverse_merge(:format => request_extension) 129 end 125 130 path = controller.url_for(options).split('://').last 126 131 normalize!(path) 127 add_extension!(path, @extension) 132 if infer_extension 133 @extension = request_extension 134 add_extension!(path, @extension) 135 end 128 136 @path = URI.unescape(path) 129 137 end 130 138 … … 134 142 end 135 143 136 144 def add_extension!(path, extension) 137 path << ".#{extension}" if extension 145 path << ".#{extension}" if extension and !path.ends_with?(extension) 138 146 end 139 147 140 def extract_extension( file_path)148 def extract_extension(request) 141 149 # Don't want just what comes after the last '.' to accommodate multi part extensions 142 150 # such as tar.gz. 143 file_path[/^[^.]+\.(.+)$/, 1] 151 extension = request.path[/^[^.]+\.(.+)$/, 1] 152 153 # If there's no extension in the path, check request.format 154 if extension.nil? 155 extension = request.format.to_sym.to_s 156 if extension=='all' 157 extension = nil 158 end 159 end 160 extension 144 161 end 145 162 end 146 163 end -
a/actionpack/test/controller/caching_test.rb
old new 169 169 expire_action :controller => 'action_caching_test', :action => 'index' 170 170 render :nothing => true 171 171 end 172 172 def expire_xml 173 expire_action :controller => 'action_caching_test', :action => 'index', :format => 'xml' 174 render :nothing => true 175 end 173 176 end 174 177 175 178 class ActionCachingMockController … … 188 191 mocked_path = @mock_path 189 192 Object.new.instance_eval(<<-EVAL) 190 193 def path; '#{@mock_path}' end 194 def format; 'all' end 191 195 self 192 196 EVAL 193 197 end … … 258 262 assert_equal new_cached_time, @response.body 259 263 end 260 264 265 def test_cache_expiration_isnt_affected_by_request_format 266 get :index 267 cached_time = content_to_cache 268 reset! 269 270 @request.set_REQUEST_URI "/action_caching_test/expire.xml" 271 get :expire, :format => :xml 272 reset! 273 274 get :index 275 new_cached_time = content_to_cache 276 assert_not_equal cached_time, @response.body 277 end 278 261 279 def test_cache_is_scoped_by_subdomain 262 280 @request.host = 'jamis.hostname.com' 263 281 get :index … … 302 320 end 303 321 304 322 def test_xml_version_of_resource_is_treated_as_different_cache 305 @mock_controller.mock_url_for = 'http://example.org/posts/' 306 @mock_controller.mock_path = '/posts/index.xml' 307 path_object = @path_class.new(@mock_controller, {}) 308 assert_equal 'xml', path_object.extension 309 assert_equal 'example.org/posts/index.xml', path_object.path 323 with_routing do |set| 324 ActionController::Routing::Routes.draw do |map| 325 map.connect ':controller/:action.:format' 326 map.connect ':controller/:action' 327 end 328 329 get :index, :format => 'xml' 330 cached_time = content_to_cache 331 assert_equal cached_time, @response.body 332 assert_cache_exists 'hostname.com/action_caching_test/index.xml' 333 reset! 334 335 get :index, :format => 'xml' 336 assert_equal cached_time, @response.body 337 assert_equal 'application/xml', @response.content_type 338 reset! 339 340 @request.env['HTTP_ACCEPT'] = "application/xml" 341 get :index 342 assert_equal cached_time, @response.body 343 assert_equal 'application/xml', @response.content_type 344 reset! 345 346 get :expire_xml 347 reset! 348 349 get :index, :format => 'xml' 350 assert_not_equal cached_time, @response.body 351 end 310 352 end 311 353 312 354 def test_correct_content_type_is_returned_for_cache_hit