diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2007-04-27 16:58:36 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2007-04-27 16:58:36 +0000 |
commit | 8326a15784550f8a909e140f828dc0b1c83d617c (patch) | |
tree | 7c57a4c50944c6a2df6a5af63e2dd11a0fd2215b | |
parent | f985bfd8f8854a3d27da39237ce410d12955595a (diff) | |
download | rails-8326a15784550f8a909e140f828dc0b1c83d617c.tar.gz rails-8326a15784550f8a909e140f828dc0b1c83d617c.tar.bz2 rails-8326a15784550f8a909e140f828dc0b1c83d617c.zip |
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
-rw-r--r-- | actionpack/CHANGELOG | 18 | ||||
-rw-r--r-- | actionpack/lib/action_controller/resources.rb | 18 | ||||
-rw-r--r-- | actionpack/test/controller/resources_test.rb | 38 |
3 files changed, 69 insertions, 5 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 773458574a..9a676d5d8b 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -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: diff --git a/actionpack/lib/action_controller/resources.rb b/actionpack/lib/action_controller/resources.rb index 63f2b67755..6a2280a6d3 100644 --- a/actionpack/lib/action_controller/resources.rb +++ b/actionpack/lib/action_controller/resources.rb @@ -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) diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb index 337dd4abce..45e4003021 100644 --- a/actionpack/test/controller/resources_test.rb +++ b/actionpack/test/controller/resources_test.rb @@ -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] |