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

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

View file

@ -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:

View file

@ -18,7 +18,7 @@ module ActionController
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 @@ module ActionController
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)

View file

@ -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 @@ class ResourcesTest < Test::Unit::TestCase
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 @@ class ResourcesTest < Test::Unit::TestCase
# 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 @@ class ResourcesTest < Test::Unit::TestCase
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 @@ class ResourcesTest < Test::Unit::TestCase
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]