Skip to content

Commit

Permalink
Added CollectionResponder which allows you to always redirect to the …
Browse files Browse the repository at this point in the history
…collection path (index action) after POST/PUT/DELETE.
  • Loading branch information
josevalim committed Jun 26, 2010
1 parent e2a3174 commit 3af1895
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rdoc
Expand Up @@ -2,7 +2,7 @@

* :js now sets the flash.now by default, instead of flash
* Renamed responders_install generator to responders:install
* Added a fix for Rails 3.0.0.beta3
* Added CollectionResponder which allows you to always redirect to the collection path (index action) after POST/PUT/DELETE

== 0.5

Expand Down
4 changes: 1 addition & 3 deletions lib/generators/rails/responders_controller_generator.rb
Expand Up @@ -3,9 +3,7 @@
module Rails
module Generators
class RespondersControllerGenerator < ScaffoldControllerGenerator
def self.source_root
@_source_root ||= File.expand_path("templates", File.dirname(__FILE__))
end
source_root File.expand_path("../templates", __FILE__)

protected

Expand Down
8 changes: 5 additions & 3 deletions lib/generators/responders/install_generator.rb
@@ -1,9 +1,7 @@
module Responders
module Generators
class InstallGenerator < Rails::Generators::Base
def self.source_root
@_source_root ||= File.expand_path("..", __FILE__)
end
source_root File.expand_path("..", __FILE__)

desc "Creates an initializer with default responder configuration and copy locale file"

Expand All @@ -12,6 +10,10 @@ def create_responder_file
class ApplicationResponder < ActionController::Responder
include Responders::FlashResponder
include Responders::HttpCacheResponder
# Uncomment this responder if you want your resources to redirect to the collection
# path (index action) instead of the resource path for POST/PUT/DELETE requests.
# include Responders::CollectionResponder
end
RUBY
end
Expand Down
5 changes: 3 additions & 2 deletions lib/responders.rb
@@ -1,8 +1,9 @@
require 'action_controller/base'

module Responders
autoload :FlashResponder, 'responders/flash_responder'
autoload :HttpCacheResponder, 'responders/http_cache_responder'
autoload :FlashResponder, 'responders/flash_responder'
autoload :HttpCacheResponder, 'responders/http_cache_responder'
autoload :CollectionResponder, 'responders/collection_responder'

require 'responders/controller_method'

Expand Down
30 changes: 30 additions & 0 deletions lib/responders/collection_responder.rb
@@ -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
62 changes: 62 additions & 0 deletions test/collection_responder_test.rb
@@ -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
9 changes: 8 additions & 1 deletion test/test_helper.rb
Expand Up @@ -40,7 +40,8 @@ class ActiveSupport::TestCase
end
end

class Address
class Model
include ActiveModel::Conversion
include ActiveModel::Validations

attr_accessor :persisted, :updated_at
Expand All @@ -58,4 +59,10 @@ def initialize(updated_at=nil)
@persisted = true
self.updated_at = updated_at
end
end

class Address < Model
end

class User < Model
end

0 comments on commit 3af1895

Please sign in to comment.