Changeset 6594
- Timestamp:
- 04/27/07 16:58:36 (3 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/resources.rb (modified) (2 diffs)
- trunk/actionpack/test/controller/resources_test.rb (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r6588 r6594 1 1 *SVN* 2 3 * Added map.namespace to deal with the common situation of admin sections and the like [DHH] 4 5 Before: 6 7 map.resources :products, :path_prefix => "admin", :controller => "admin/products", :collection => { :inventory => :get }, :member => { :duplicate => :post } 8 map.resources :tags, :name_prefix => 'admin_product_', :path_prefix => "admin/products/:product_id", :controller => "admin/product_tags" 9 map.resources :images, :name_prefix => 'admin_product_', :path_prefix => "admin/products/:product_id", :controller => "admin/product_images" 10 map.resources :variants, :name_prefix => 'admin_product_', :path_prefix => "admin/products/:product_id", :controller => "admin/product_variants" 11 12 After: 13 14 map.namespace(:admin) do |admin| 15 admin.resources :products, 16 :collection => { :inventory => :get }, 17 :member => { :duplicate => :post }, 18 :has_many => [ :tags, :images, :variants ] 19 end 2 20 3 21 * Added :name_prefix as standard for nested resources [DHH]. WARNING: May be backwards incompatible with your app trunk/actionpack/lib/action_controller/resources.rb
r6592 r6594 19 19 20 20 def controller 21 @controller ||= (options[:controller] || plural).to_s21 @controller ||= "#{options[:namespace]}#{(options[:controller] || plural).to_s}" 22 22 end 23 23 … … 301 301 entities.each { |entity| map_singleton_resource(entity, options.dup, &block) } 302 302 end 303 304 # Enables the use of resources in a module by setting the name_prefix, path_prefix, and namespace for the model. 305 # Example: 306 # 307 # map.namespace(:admin) do |admin| 308 # admin.resources :products, 309 # :has_many => [ :tags, :images, :variants ] 310 # end 311 # 312 # This will create admin_products_url pointing to "admin/products", which will look for an Admin::ProductsController. 313 # It'll also create admin_product_tags_url pointing to "admin/products/#{product_id}/tags", which will look for 314 # Admin::TagsController. 315 def namespace(name, options = {}, &block) 316 with_options({ :path_prefix => name, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(options), &block) 317 end 318 303 319 304 320 private trunk/actionpack/test/controller/resources_test.rb
r6593 r6594 15 15 class AccountController < ResourcesController; end 16 16 class AdminController < ResourcesController; end 17 18 module Backoffice 19 class ProductsController < ResourcesController; end 20 end 17 21 18 22 class ResourcesTest < Test::Unit::TestCase … … 385 389 end 386 390 391 def test_resources_in_namespace 392 with_routing do |set| 393 set.draw do |map| 394 map.namespace :backoffice do |backoffice| 395 backoffice.resources :products 396 end 397 end 398 399 assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/' 400 end 401 end 402 403 def test_resources_using_namespace 404 with_routing do |set| 405 set.draw do |map| 406 map.resources :products, :namespace => "backoffice/" 407 end 408 409 assert_simply_restful_for :products, :controller => "backoffice/products" 410 end 411 end 412 387 413 protected 388 414 def with_restful_routing(*args) … … 403 429 def assert_simply_restful_for(controller_name, options = {}) 404 430 assert_restful_routes_for controller_name, options 405 assert_restful_named_routes_for controller_name, options431 assert_restful_named_routes_for controller_name, nil, options 406 432 end 407 433 … … 412 438 413 439 def assert_restful_routes_for(controller_name, options = {}) 414 (options[:options] ||= {})[:controller] = controller_name.to_s 440 options[:options] ||= {} 441 options[:options][:controller] = options[:controller] || controller_name.to_s 415 442 416 443 collection_path = "/#{options[:path_prefix]}#{controller_name}" … … 457 484 end 458 485 singular_name ||= controller_name.to_s.singularize 459 (options[:options] ||= {})[:controller] = controller_name.to_s 460 @controller = "#{controller_name.to_s.camelize}Controller".constantize.new 486 487 options[:options] ||= {} 488 options[:options][:controller] = options[:controller] || controller_name.to_s 489 490 @controller = "#{options[:options][:controller].camelize}Controller".constantize.new 461 491 @request = ActionController::TestRequest.new 462 492 @response = ActionController::TestResponse.new