1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix action-cached exception responses.

Methods raising ActiveRecord::RecordNotFound were returning 404 on first request and 200 OK with blank body on subsequent requests.

[#2533 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
This commit is contained in:
John F. Douthat 2009-04-21 13:35:54 -05:00 committed by Jeremy Kemper
parent 00d1a57e9f
commit e59835bd09
2 changed files with 44 additions and 1 deletions

View file

@ -61,7 +61,9 @@ module ActionController #:nodoc:
filter_options = { :only => actions, :if => options.delete(:if), :unless => options.delete(:unless) } filter_options = { :only => actions, :if => options.delete(:if), :unless => options.delete(:unless) }
cache_filter = ActionCacheFilter.new(:layout => options.delete(:layout), :cache_path => options.delete(:cache_path), :store_options => options) cache_filter = ActionCacheFilter.new(:layout => options.delete(:layout), :cache_path => options.delete(:cache_path), :store_options => options)
around_filter(cache_filter, filter_options) around_filter(filter_options) do |controller, action|
cache_filter.filter(controller, action)
end
end end
end end
@ -83,6 +85,12 @@ module ActionController #:nodoc:
@options = options @options = options
end end
def filter(controller, action)
should_continue = before(controller)
action.call if should_continue
after(controller)
end
def before(controller) def before(controller)
cache_path = ActionCachePath.new(controller, path_options_for(controller, @options.slice(:cache_path))) cache_path = ActionCachePath.new(controller, path_options_for(controller, @options.slice(:cache_path)))
if cache = controller.read_fragment(cache_path.path, @options[:store_options]) if cache = controller.read_fragment(cache_path.path, @options[:store_options])

View file

@ -1,5 +1,6 @@
require 'fileutils' require 'fileutils'
require 'abstract_unit' require 'abstract_unit'
require 'active_record_unit'
CACHE_DIR = 'test_cache' CACHE_DIR = 'test_cache'
# Don't change '/../temp/' cavalierly or you might hose something you don't want hosed # Don't change '/../temp/' cavalierly or you might hose something you don't want hosed
@ -153,6 +154,7 @@ class ActionCachingTestController < ActionController::Base
caches_action :edit, :cache_path => Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" } caches_action :edit, :cache_path => Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" }
caches_action :with_layout caches_action :with_layout
caches_action :layout_false, :layout => false caches_action :layout_false, :layout => false
caches_action :record_not_found, :four_oh_four, :simple_runtime_error
layout 'talk_from_action.erb' layout 'talk_from_action.erb'
@ -175,6 +177,18 @@ class ActionCachingTestController < ActionController::Base
render :text => @cache_this, :layout => true render :text => @cache_this, :layout => true
end end
def record_not_found
raise ActiveRecord::RecordNotFound, "oops!"
end
def four_oh_four
render :text => "404'd!", :status => 404
end
def simple_runtime_error
raise "oops!"
end
alias_method :show, :index alias_method :show, :index
alias_method :edit, :index alias_method :edit, :index
alias_method :destroy, :index alias_method :destroy, :index
@ -458,6 +472,27 @@ class ActionCacheTest < ActionController::TestCase
assert_response :success assert_response :success
end end
def test_record_not_found_returns_404_for_multiple_requests
get :record_not_found
assert_response 404
get :record_not_found
assert_response 404
end
def test_four_oh_four_returns_404_for_multiple_requests
get :four_oh_four
assert_response 404
get :four_oh_four
assert_response 404
end
def test_simple_runtime_error_returns_500_for_multiple_requests
get :simple_runtime_error
assert_response 500
get :simple_runtime_error
assert_response 500
end
private private
def content_to_cache def content_to_cache
assigns(:cache_this) assigns(:cache_this)