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

Changeset 6453

Show
Ignore:
Timestamp:
03/22/07 10:12:34 (2 years ago)
Author:
rick
Message:

Allow configuration of the default action cache path for #caches_action calls. [Rick Olson]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r6435 r6453  
    11*SVN* 
     2 
     3* Allow configuration of the default action cache path for #caches_action calls.  [Rick Olson] 
     4 
     5  class ListsController < ApplicationController 
     6    caches_action :index, :cache_path => Proc.new { |controller|  
     7      controller.params[:user_id] ?  
     8        controller.send(:user_lists_url, c.params[:user_id]) : 
     9        controller.send(:lists_url) } 
     10  end 
    211 
    312* Performance: patch cgi/session/pstore to require digest/md5 once rather than per #initialize.  #7583 [Stefan Kaes] 
  • trunk/actionpack/lib/action_controller/caching.rb

    r6189 r6453  
    162162    # are treated like separate requests and so are cached separately. Keep in mind when expiring an action cache that <tt>:action => 'lists'</tt> is not the same 
    163163    # as <tt>:action => 'list', :format => :xml</tt>. 
     164    # 
     165    # You can set modify the default action cache path by passing a :cache_path option.  This will be passed directly to ActionCachePath.path_for.  This is handy 
     166    # for actions with multiple possible routes that should be cached differently.  If a block is given, it is called with the current controller instance. 
     167    # 
     168    #   class ListsController < ApplicationController 
     169    #     before_filter :authenticate, :except => :public 
     170    #     caches_page   :public 
     171    #     caches_action :show, :cache_path => { :project => 1 } 
     172    #     caches_action :show, :cache_path => Proc.new { |controller|  
     173    #       controller.params[:user_id] ?  
     174    #         controller.send(:user_list_url, c.params[:user_id], c.params[:id]) : 
     175    #         controller.send(:list_url, c.params[:id]) } 
     176    #   end 
    164177    module Actions 
    165178      def self.included(base) #:nodoc: 
     
    189202        def initialize(*actions, &block) 
    190203          @actions = actions 
     204          @options = @actions.last.is_a?(Hash) ? @actions.pop : {} 
    191205        end 
    192206 
    193207        def before(controller) 
    194208          return unless @actions.include?(controller.action_name.intern) 
    195           action_cache_path = ActionCachePath.new(controller
     209          action_cache_path = ActionCachePath.new(controller, path_options_for(controller)
    196210          if cache = controller.read_fragment(action_cache_path.path) 
    197211            controller.rendered_action_cache = true 
     
    204218        def after(controller) 
    205219          return if !@actions.include?(controller.action_name.intern) || controller.rendered_action_cache 
    206           controller.write_fragment(ActionCachePath.path_for(controller), controller.response.body) 
     220          controller.write_fragment(ActionCachePath.path_for(controller, path_options_for(controller)), controller.response.body) 
    207221        end 
    208222         
     
    214228              action_cache_path.controller.response.content_type = content_type.to_s 
    215229            end 
     230          end 
     231           
     232          def path_options_for(controller) 
     233            (@options[:cache_path].respond_to?(:call) ? @options[:cache_path].call(controller) : @options[:cache_path]) || {} 
    216234          end 
    217235           
     
    599617        end 
    600618 
     619        protected 
     620          # gets the action cache path for the given options. 
     621          def action_path_for(options) 
     622            ActionController::Caching::Actions::ActionCachePath.path_for(controller, options) 
     623          end 
     624 
    601625        private 
    602626          def callback(timing) 
  • trunk/actionpack/test/controller/caching_test.rb

    r6230 r6453  
    9999class ActionCachingTestController < ActionController::Base 
    100100  caches_action :index 
     101  caches_action :show, :cache_path => 'http://test.host/custom/show' 
     102  caches_action :edit, :cache_path => Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" } 
    101103 
    102104  def index 
     
    104106    render :text => @cache_this 
    105107  end 
     108   
     109  alias_method :show, :index 
     110  alias_method :edit, :index 
    106111 
    107112  def expire 
     
    149154    cached_time = content_to_cache 
    150155    assert_equal cached_time, @response.body 
    151     reset! 
    152  
    153     get :index 
    154     assert_equal cached_time, @response.body 
     156    assert_cache_exists 'hostname.com/action_caching_test' 
     157    reset! 
     158 
     159    get :index 
     160    assert_equal cached_time, @response.body 
     161  end 
     162   
     163  def test_action_cache_with_custom_cache_path 
     164    get :show 
     165    cached_time = content_to_cache 
     166    assert_equal cached_time, @response.body 
     167    assert_cache_exists 'test.host/custom/show' 
     168    reset! 
     169 
     170    get :show 
     171    assert_equal cached_time, @response.body 
     172  end 
     173 
     174  def test_action_cache_with_custom_cache_path_in_block 
     175    get :edit 
     176    assert_cache_exists 'test.host/edit' 
     177    reset! 
     178 
     179    get :edit, :id => 1 
     180    assert_cache_exists 'test.host/1;edit' 
    155181  end 
    156182 
     
    229255      @request.host = 'hostname.com' 
    230256    end 
    231 end 
     257     
     258    def assert_cache_exists(path) 
     259      full_path = File.join(FILE_STORE_PATH, path + '.cache') 
     260      assert File.exist?(full_path), "#{full_path.inspect} does not exist." 
     261    end 
     262end