Responder redirects to resource if destroy fails.
Signed-off-by: Yehuda Katz <wycats@Yehuda-Katz.local>
This commit is contained in:
parent
324fa688fc
commit
2ecfa817c9
|
@ -80,6 +80,11 @@ module ActionController #:nodoc:
|
|||
class Responder
|
||||
attr_reader :controller, :request, :format, :resource, :resources, :options
|
||||
|
||||
ACTIONS_FOR_VERBS = {
|
||||
:post => :new,
|
||||
:put => :edit
|
||||
}
|
||||
|
||||
def initialize(controller, resources, options={})
|
||||
@controller = controller
|
||||
@request = controller.request
|
||||
|
@ -138,7 +143,7 @@ module ActionController #:nodoc:
|
|||
def navigation_behavior(error)
|
||||
if get?
|
||||
raise error
|
||||
elsif has_errors?
|
||||
elsif has_errors? && default_action
|
||||
render :action => default_action
|
||||
else
|
||||
redirect_to resource_location
|
||||
|
@ -209,7 +214,7 @@ module ActionController #:nodoc:
|
|||
# the verb is post.
|
||||
#
|
||||
def default_action
|
||||
@action || (request.post? ? :new : :edit)
|
||||
@action ||= ACTIONS_FOR_VERBS[request.method]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -599,14 +599,18 @@ class RespondWithControllerTest < ActionController::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_using_resource_for_post_with_html
|
||||
def test_using_resource_for_post_with_html_redirects_on_success
|
||||
with_test_route_set do
|
||||
post :using_resource
|
||||
assert_equal "text/html", @response.content_type
|
||||
assert_equal 302, @response.status
|
||||
assert_equal "http://www.example.com/customers/13", @response.location
|
||||
assert @response.redirect?
|
||||
end
|
||||
end
|
||||
|
||||
def test_using_resource_for_post_with_html_rerender_on_failure
|
||||
with_test_route_set do
|
||||
errors = { :name => :invalid }
|
||||
Customer.any_instance.stubs(:errors).returns(errors)
|
||||
post :using_resource
|
||||
|
@ -617,16 +621,20 @@ class RespondWithControllerTest < ActionController::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_using_resource_for_post_with_xml
|
||||
def test_using_resource_for_post_with_xml_yields_created_on_success
|
||||
with_test_route_set do
|
||||
@request.accept = "application/xml"
|
||||
|
||||
post :using_resource
|
||||
assert_equal "application/xml", @response.content_type
|
||||
assert_equal 201, @response.status
|
||||
assert_equal "<name>david</name>", @response.body
|
||||
assert_equal "http://www.example.com/customers/13", @response.location
|
||||
end
|
||||
end
|
||||
|
||||
def test_using_resource_for_post_with_xml_yields_unprocessable_entity_on_failure
|
||||
with_test_route_set do
|
||||
@request.accept = "application/xml"
|
||||
errors = { :name => :invalid }
|
||||
Customer.any_instance.stubs(:errors).returns(errors)
|
||||
post :using_resource
|
||||
|
@ -637,14 +645,18 @@ class RespondWithControllerTest < ActionController::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_using_resource_for_put_with_html
|
||||
def test_using_resource_for_put_with_html_redirects_on_success
|
||||
with_test_route_set do
|
||||
put :using_resource
|
||||
assert_equal "text/html", @response.content_type
|
||||
assert_equal 302, @response.status
|
||||
assert_equal "http://www.example.com/customers/13", @response.location
|
||||
assert @response.redirect?
|
||||
end
|
||||
end
|
||||
|
||||
def test_using_resource_for_put_with_html_rerender_on_failure
|
||||
with_test_route_set do
|
||||
errors = { :name => :invalid }
|
||||
Customer.any_instance.stubs(:errors).returns(errors)
|
||||
put :using_resource
|
||||
|
@ -655,14 +667,16 @@ class RespondWithControllerTest < ActionController::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_using_resource_for_put_with_xml
|
||||
def test_using_resource_for_put_with_xml_yields_ok_on_success
|
||||
@request.accept = "application/xml"
|
||||
|
||||
put :using_resource
|
||||
assert_equal "application/xml", @response.content_type
|
||||
assert_equal 200, @response.status
|
||||
assert_equal " ", @response.body
|
||||
end
|
||||
|
||||
def test_using_resource_for_put_with_xml_yields_unprocessable_entity_on_failure
|
||||
@request.accept = "application/xml"
|
||||
errors = { :name => :invalid }
|
||||
Customer.any_instance.stubs(:errors).returns(errors)
|
||||
put :using_resource
|
||||
|
@ -672,7 +686,7 @@ class RespondWithControllerTest < ActionController::TestCase
|
|||
assert_nil @response.location
|
||||
end
|
||||
|
||||
def test_using_resource_for_delete_with_html
|
||||
def test_using_resource_for_delete_with_html_redirects_on_success
|
||||
with_test_route_set do
|
||||
Customer.any_instance.stubs(:destroyed?).returns(true)
|
||||
delete :using_resource
|
||||
|
@ -682,7 +696,7 @@ class RespondWithControllerTest < ActionController::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_using_resource_for_delete_with_xml
|
||||
def test_using_resource_for_delete_with_xml_yields_ok_on_success
|
||||
Customer.any_instance.stubs(:destroyed?).returns(true)
|
||||
@request.accept = "application/xml"
|
||||
delete :using_resource
|
||||
|
@ -691,6 +705,18 @@ class RespondWithControllerTest < ActionController::TestCase
|
|||
assert_equal " ", @response.body
|
||||
end
|
||||
|
||||
def test_using_resource_for_delete_with_html_redirects_on_failure
|
||||
with_test_route_set do
|
||||
errors = { :name => :invalid }
|
||||
Customer.any_instance.stubs(:errors).returns(errors)
|
||||
Customer.any_instance.stubs(:destroyed?).returns(false)
|
||||
delete :using_resource
|
||||
assert_equal "text/html", @response.content_type
|
||||
assert_equal 302, @response.status
|
||||
assert_equal "http://www.example.com/customers/13", @response.location
|
||||
end
|
||||
end
|
||||
|
||||
def test_using_resource_with_parent_for_get
|
||||
@request.accept = "application/xml"
|
||||
get :using_resource_with_parent
|
||||
|
|
Loading…
Reference in New Issue