forked from heartcombo/responders
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added CollectionResponder which allows you to always redirect to the …
…collection path (index action) after POST/PUT/DELETE.
- Loading branch information
Showing
7 changed files
with
110 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Responders | ||
# This responder modifies your current responder to redirect | ||
# to the collection page on POST/PUT/DELETE. | ||
module CollectionResponder | ||
protected | ||
|
||
# Returns the collection location for redirecting after POST/PUT/DELETE. | ||
# This method, converts the following resources array to the following: | ||
# | ||
# [:admin, @post] #=> [:admin, :posts] | ||
# [@user, @post] #=> [@user, :posts] | ||
# | ||
# When these new arrays are given to redirect_to, it will generate the | ||
# proper URL pointing to the index action. | ||
# | ||
# [:admin, @post] #=> admin_posts_url | ||
# [@user, @post] #=> user_posts_url(@user.to_param) | ||
# | ||
def navigation_location | ||
return options[:location] if options[:location] | ||
klass = resources.last.class | ||
|
||
if klass.respond_to?(:model_name) | ||
resources[0...-1] << klass.model_name.plural.to_sym | ||
else | ||
resources | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'test_helper' | ||
|
||
class CollectionResponder < ActionController::Responder | ||
include Responders::CollectionResponder | ||
end | ||
|
||
class CollectionController < ApplicationController | ||
self.responder = CollectionResponder | ||
|
||
def single | ||
respond_with Address.new | ||
end | ||
|
||
def namespaced | ||
respond_with :admin, Address.new | ||
end | ||
|
||
def nested | ||
respond_with User.new, Address.new | ||
end | ||
|
||
def only_symbols | ||
respond_with :admin, :addresses | ||
end | ||
|
||
def with_location | ||
respond_with Address.new, :location => "given_location" | ||
end | ||
end | ||
|
||
class CollectionResponderTest < ActionController::TestCase | ||
tests CollectionController | ||
|
||
def test_collection_with_single_resource | ||
@controller.expects(:addresses_url).returns("addresses_url") | ||
post :single | ||
assert_redirected_to "addresses_url" | ||
end | ||
|
||
def test_collection_with_namespaced_resource | ||
@controller.expects(:admin_addresses_url).returns("admin_addresses_url") | ||
put :namespaced | ||
assert_redirected_to "admin_addresses_url" | ||
end | ||
|
||
def test_collection_with_nested_resource | ||
@controller.expects(:user_addresses_url).returns("user_addresses_url") | ||
delete :nested | ||
assert_redirected_to "user_addresses_url" | ||
end | ||
|
||
def test_collection_respects_location_option | ||
delete :with_location | ||
assert_redirected_to "given_location" | ||
end | ||
|
||
def test_collection_respects_only_symbols | ||
@controller.expects(:admin_addresses_url).returns("admin_addresses_url") | ||
post :only_symbols | ||
assert_redirected_to "admin_addresses_url" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters