From 0ccedabe214e66e0f18566b008c90cf346caee81 Mon Sep 17 00:00:00 2001 From: Bill Kirtley Date: Wed, 3 Feb 2010 11:11:09 -0500 Subject: [PATCH] Add :max_age option to stale? and fresh_when. Fixes #3839 for 2-3-stable branch. --- actionpack/lib/action_controller/base.rb | 8 +++++--- actionpack/test/controller/render_test.rb | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index d66e666..e1e0a76 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -502,7 +502,7 @@ module ActionController #:nodoc: end elsif block_given? key = key.dup - value = value.dup if value.duplicable? + value = value.dup if value yield key, value filtered_parameters[key] = value else @@ -1173,19 +1173,20 @@ module ActionController #:nodoc: # * :etag # * :last_modified # * :public By default the Cache-Control header is private, set this to true if you want your application to be cachable by other devices (proxy caches). + # * :max_age Specify how long the resource should be considered fresh (by proxy caches) before coming back to the origin # # Example: # # def show # @article = Article.find(params[:id]) - # fresh_when(:etag => @article, :last_modified => @article.created_at.utc, :public => true) + # fresh_when(:etag => @article, :last_modified => @article.created_at.utc, :public => true, :max_age => 5.minutes) # end # # This will render the show template if the request isn't sending a matching etag or # If-Modified-Since header and just a "304 Not Modified" response if there's a match. # def fresh_when(options) - options.assert_valid_keys(:etag, :last_modified, :public) + options.assert_valid_keys(:etag, :last_modified, :public, :max_age) response.etag = options[:etag] if options[:etag] response.last_modified = options[:last_modified] if options[:last_modified] @@ -1195,6 +1196,7 @@ module ActionController #:nodoc: cache_control.delete("private") cache_control.delete("no-cache") cache_control << "public" + cache_control << "max-age=#{options[:max_age]}" if options[:max_age] response.headers["Cache-Control"] = cache_control.join(', ') end diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 112e9ef..ff5a255 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -79,6 +79,15 @@ class TestController < ActionController::Base fresh_when(:last_modified => Time.now.utc.beginning_of_day, :etag => [ :foo, 123 ]) end + def conditional_hello_with_bangs_public + render :action => 'hello_world' + end + before_filter :handle_last_modified_and_etags_with_public, :only=>:conditional_hello_with_bangs_public + + def handle_last_modified_and_etags_with_public + fresh_when(:last_modified => Time.now.utc.beginning_of_day, :etag => [ :foo, 123 ], :public => true, :max_age => 5.minutes) + end + def render_hello_world render :template => "test/hello_world" end @@ -1743,6 +1752,12 @@ class LastModifiedRenderTest < ActionController::TestCase get :conditional_hello_with_bangs assert_response :success end + + def test_last_modified_public_sets_cache_control + @request.if_modified_since = @last_modified + get :conditional_hello_with_bangs_public + assert_equal "public, max-age=300", @response.headers['Cache-Control'] + end end class RenderingLoggingTest < ActionController::TestCase -- 1.6.4