Added map.namespace to deal with the common situation of admin sections and the like [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6594 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2007-04-27 16:58:36 +00:00
parent f985bfd8f8
commit 8326a15784
3 changed files with 69 additions and 5 deletions

@ -1,5 +1,23 @@
*SVN*
* Added map.namespace to deal with the common situation of admin sections and the like [DHH]
Before:
map.resources :products, :path_prefix => "admin", :controller => "admin/products", :collection => { :inventory => :get }, :member => { :duplicate => :post }
map.resources :tags, :name_prefix => 'admin_product_', :path_prefix => "admin/products/:product_id", :controller => "admin/product_tags"
map.resources :images, :name_prefix => 'admin_product_', :path_prefix => "admin/products/:product_id", :controller => "admin/product_images"
map.resources :variants, :name_prefix => 'admin_product_', :path_prefix => "admin/products/:product_id", :controller => "admin/product_variants"
After:
map.namespace(:admin) do |admin|
admin.resources :products,
:collection => { :inventory => :get },
:member => { :duplicate => :post },
:has_many => [ :tags, :images, :variants ]
end
* Added :name_prefix as standard for nested resources [DHH]. WARNING: May be backwards incompatible with your app
Before:

@ -18,7 +18,7 @@ def initialize(entities, options)
end
def controller
@controller ||= (options[:controller] || plural).to_s
@controller ||= "#{options[:namespace]}#{(options[:controller] || plural).to_s}"
end
def requirements(with_id = false)
@ -301,6 +301,22 @@ def resource(*entities, &block)
entities.each { |entity| map_singleton_resource(entity, options.dup, &block) }
end
# Enables the use of resources in a module by setting the name_prefix, path_prefix, and namespace for the model.
# Example:
#
# map.namespace(:admin) do |admin|
# admin.resources :products,
# :has_many => [ :tags, :images, :variants ]
# end
#
# This will create admin_products_url pointing to "admin/products", which will look for an Admin::ProductsController.
# It'll also create admin_product_tags_url pointing to "admin/products/#{product_id}/tags", which will look for
# Admin::TagsController.
def namespace(name, options = {}, &block)
with_options({ :path_prefix => name, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(options), &block)
end
private
def map_resource(entities, options = {}, &block)
resource = Resource.new(entities, options)

@ -15,6 +15,10 @@ class LogoController < ResourcesController; end
class AccountController < ResourcesController; end
class AdminController < ResourcesController; end
module Backoffice
class ProductsController < ResourcesController; end
end
class ResourcesTest < Test::Unit::TestCase
def test_should_arrange_actions
resource = ActionController::Resources::Resource.new(:messages,
@ -384,6 +388,28 @@ def test_should_not_allow_delete_or_put_on_collection_path
end
end
def test_resources_in_namespace
with_routing do |set|
set.draw do |map|
map.namespace :backoffice do |backoffice|
backoffice.resources :products
end
end
assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
end
end
def test_resources_using_namespace
with_routing do |set|
set.draw do |map|
map.resources :products, :namespace => "backoffice/"
end
assert_simply_restful_for :products, :controller => "backoffice/products"
end
end
protected
def with_restful_routing(*args)
with_routing do |set|
@ -402,7 +428,7 @@ def with_singleton_resources(*args)
# runs assert_restful_routes_for and assert_restful_named_routes for on the controller_name and options, without passing a block.
def assert_simply_restful_for(controller_name, options = {})
assert_restful_routes_for controller_name, options
assert_restful_named_routes_for controller_name, options
assert_restful_named_routes_for controller_name, nil, options
end
def assert_singleton_restful_for(singleton_name, options = {})
@ -411,7 +437,8 @@ def assert_singleton_restful_for(singleton_name, options = {})
end
def assert_restful_routes_for(controller_name, options = {})
(options[:options] ||= {})[:controller] = controller_name.to_s
options[:options] ||= {}
options[:options][:controller] = options[:controller] || controller_name.to_s
collection_path = "/#{options[:path_prefix]}#{controller_name}"
member_path = "#{collection_path}/1"
@ -456,8 +483,11 @@ def assert_restful_named_routes_for(controller_name, singular_name = nil, option
singular_name = nil
end
singular_name ||= controller_name.to_s.singularize
(options[:options] ||= {})[:controller] = controller_name.to_s
@controller = "#{controller_name.to_s.camelize}Controller".constantize.new
options[:options] ||= {}
options[:options][:controller] = options[:controller] || controller_name.to_s
@controller = "#{options[:options][:controller].camelize}Controller".constantize.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
get :index, options[:options]