Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

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  
    3131    #     before_filter :authenticate, :except => :public 
    3232    #     caches_page   :public 
    3333    #     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| 
    3535    #       controller.params[:user_id] ?  
    3636    #         controller.send(:user_list_url, c.params[:user_id], c.params[:id]) : 
    3737    #         controller.send(:list_url, c.params[:id]) } 
     
    6464 
    6565          if options[:action].is_a?(Array) 
    6666            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)) 
    6868            end 
    6969          else 
    70             expire_fragment(ActionCachePath.path_for(self, options)) 
     70            expire_fragment(ActionCachePath.path_for(self, options, false)) 
    7171          end 
    7272        end 
    7373 
     
    115115        attr_reader :path, :extension 
    116116         
    117117        class << self 
    118           def path_for(controller, options
    119             new(controller, options).path 
     118          def path_for(controller, options, infer_extension=true
     119            new(controller, options, infer_extension).path 
    120120          end 
    121121        end 
    122122         
    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 
    125130          path = controller.url_for(options).split('://').last 
    126131          normalize!(path) 
    127           add_extension!(path, @extension) 
     132          if infer_extension 
     133            @extension = request_extension 
     134            add_extension!(path, @extension) 
     135          end 
    128136          @path = URI.unescape(path) 
    129137        end 
    130138         
     
    134142          end 
    135143         
    136144          def add_extension!(path, extension) 
    137             path << ".#{extension}" if extension 
     145            path << ".#{extension}" if extension and !path.ends_with?(extension) 
    138146          end 
    139147           
    140           def extract_extension(file_path
     148          def extract_extension(request
    141149            # Don't want just what comes after the last '.' to accommodate multi part extensions 
    142150            # 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 
    144161          end 
    145162      end 
    146163    end 
  • a/actionpack/test/controller/caching_test.rb

    old new  
    169169    expire_action :controller => 'action_caching_test', :action => 'index' 
    170170    render :nothing => true 
    171171  end 
    172  
     172  def expire_xml 
     173    expire_action :controller => 'action_caching_test', :action => 'index', :format => 'xml' 
     174    render :nothing => true 
     175  end 
    173176end 
    174177 
    175178class ActionCachingMockController 
     
    188191    mocked_path = @mock_path 
    189192    Object.new.instance_eval(<<-EVAL) 
    190193      def path; '#{@mock_path}' end 
     194      def format; 'all' end 
    191195      self 
    192196    EVAL 
    193197  end 
     
    258262    assert_equal new_cached_time, @response.body 
    259263  end 
    260264 
     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 
    261279  def test_cache_is_scoped_by_subdomain 
    262280    @request.host = 'jamis.hostname.com' 
    263281    get :index 
     
    302320  end 
    303321 
    304322  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 
    310352  end 
    311353 
    312354  def test_correct_content_type_is_returned_for_cache_hit