Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6594

Show
Ignore:
Timestamp:
04/27/07 16:58:36 (2 years ago)
Author:
david
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r6588 r6594  
    11*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 
    220 
    321* 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  
    1919       
    2020      def controller 
    21         @controller ||= (options[:controller] || plural).to_s 
     21        @controller ||= "#{options[:namespace]}#{(options[:controller] || plural).to_s}" 
    2222      end 
    2323       
     
    301301      entities.each { |entity| map_singleton_resource(entity, options.dup, &block) } 
    302302    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 
    303319 
    304320    private 
  • trunk/actionpack/test/controller/resources_test.rb

    r6593 r6594  
    1515class AccountController <  ResourcesController; end 
    1616class AdminController   <  ResourcesController; end 
     17 
     18module Backoffice 
     19  class ProductsController < ResourcesController; end 
     20end 
    1721 
    1822class ResourcesTest < Test::Unit::TestCase 
     
    385389  end 
    386390 
     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 
    387413  protected 
    388414    def with_restful_routing(*args) 
     
    403429    def assert_simply_restful_for(controller_name, options = {}) 
    404430      assert_restful_routes_for       controller_name, options 
    405       assert_restful_named_routes_for controller_name, options 
     431      assert_restful_named_routes_for controller_name, nil, options 
    406432    end 
    407433 
     
    412438 
    413439    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 
    415442 
    416443      collection_path            = "/#{options[:path_prefix]}#{controller_name}" 
     
    457484      end 
    458485      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 
    461491      @request    = ActionController::TestRequest.new 
    462492      @response   = ActionController::TestResponse.new