Changeset 6453
- Timestamp:
- 03/22/07 10:12:34 (2 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/caching.rb (modified) (5 diffs)
- trunk/actionpack/test/controller/caching_test.rb (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r6435 r6453 1 1 *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 2 11 3 12 * 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 162 162 # 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 163 163 # 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 164 177 module Actions 165 178 def self.included(base) #:nodoc: … … 189 202 def initialize(*actions, &block) 190 203 @actions = actions 204 @options = @actions.last.is_a?(Hash) ? @actions.pop : {} 191 205 end 192 206 193 207 def before(controller) 194 208 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)) 196 210 if cache = controller.read_fragment(action_cache_path.path) 197 211 controller.rendered_action_cache = true … … 204 218 def after(controller) 205 219 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) 207 221 end 208 222 … … 214 228 action_cache_path.controller.response.content_type = content_type.to_s 215 229 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]) || {} 216 234 end 217 235 … … 599 617 end 600 618 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 601 625 private 602 626 def callback(timing) trunk/actionpack/test/controller/caching_test.rb
r6230 r6453 99 99 class ActionCachingTestController < ActionController::Base 100 100 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" } 101 103 102 104 def index … … 104 106 render :text => @cache_this 105 107 end 108 109 alias_method :show, :index 110 alias_method :edit, :index 106 111 107 112 def expire … … 149 154 cached_time = content_to_cache 150 155 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' 155 181 end 156 182 … … 229 255 @request.host = 'hostname.com' 230 256 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 262 end