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

Changeset 6783

Show
Ignore:
Timestamp:
05/19/07 16:26:44 (2 years ago)
Author:
xal
Message:

Allow routes to be declared off namespaces

Files:

Legend:

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

    r6781 r6783  
    11*SVN* 
    22 
     3* Allow routes to be decalred under namespaces [Tobias Luetke]:  
     4   
     5  map.namespace :admin do |admin| 
     6    admin.root :controller => "products"        
     7    admin.feed 'feed.xml', :controller => 'products', :action => 'feed', :format => 'xml' 
     8  end 
     9   
    310* Update to script.aculo.us 1.7.1_beta3.  [Thomas Fuchs] 
    411 
  • trunk/actionpack/lib/action_controller/resources.rb

    r6748 r6783  
    339339      entities.each { |entity| map_singleton_resource(entity, options.dup, &block) } 
    340340    end 
    341  
    342     # Enables the use of resources in a module by setting the name_prefix, path_prefix, and namespace for the model. 
    343     # Example: 
    344     # 
    345     #   map.namespace(:admin) do |admin| 
    346     #     admin.resources :products, 
    347     #       :has_many => [ :tags, :images, :variants ] 
    348     #   end 
    349     # 
    350     # This will create admin_products_url pointing to "admin/products", which will look for an Admin::ProductsController. 
    351     # It'll also create admin_product_tags_url pointing to "admin/products/#{product_id}/tags", which will look for 
    352     # Admin::TagsController. 
    353     def namespace(name, options = {}, &block) 
    354       if options[:namespace] 
    355         with_options({:path_prefix => "#{options.delete(:path_prefix)}/#{name}", :name_prefix => "#{options.delete(:name_prefix)}#{name}_", :namespace => "#{options.delete(:namespace)}#{name}/" }.merge(options), &block) 
    356       else 
    357         with_options({ :path_prefix => name.to_s, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(options), &block) 
    358       end 
    359     end 
    360  
    361341 
    362342    private 
  • trunk/actionpack/lib/action_controller/routing.rb

    r6730 r6783  
    871871      def divide_route_options(segments, options) 
    872872        options = options.dup 
     873         
     874        if options[:namespace] 
     875          options[:controller] = "#{options[:path_prefix]}/#{options[:controller]}" 
     876          options.delete(:path_prefix) 
     877          options.delete(:name_prefix) 
     878          options.delete(:namespace) 
     879        end         
     880                 
    873881        requirements = (options.delete(:requirements) || {}).dup 
    874882        defaults     = (options.delete(:defaults)     || {}).dup 
     
    880888          hash[key] = value 
    881889        end 
    882      
     890             
    883891        [defaults, requirements, conditions] 
    884892      end 
     
    962970        # Wrap the path with slashes 
    963971        path = "/#{path}" unless path[0] == ?/ 
    964         path = "#{path}/" unless path[-1] == ?/ 
     972        path = "#{path}/" unless path[-1] == ?/     
     973         
     974        path = "/#{options[:path_prefix]}#{path}" if options[:path_prefix] 
    965975     
    966976        segments = segments_for_route_path(path) 
     
    10111021          @set.add_named_route(name, path, options) 
    10121022        end 
     1023         
     1024        # Enables the use of resources in a module by setting the name_prefix, path_prefix, and namespace for the model. 
     1025        # Example: 
     1026        # 
     1027        #   map.namespace(:admin) do |admin| 
     1028        #     admin.resources :products, 
     1029        #       :has_many => [ :tags, :images, :variants ] 
     1030        #   end 
     1031        # 
     1032        # This will create admin_products_url pointing to "admin/products", which will look for an Admin::ProductsController. 
     1033        # It'll also create admin_product_tags_url pointing to "admin/products/#{product_id}/tags", which will look for 
     1034        # Admin::TagsController. 
     1035        def namespace(name, options = {}, &block) 
     1036          if options[:namespace] 
     1037            with_options({:path_prefix => "#{options.delete(:path_prefix)}/#{name}", :name_prefix => "#{options.delete(:name_prefix)}#{name}_", :namespace => "#{options.delete(:namespace)}#{name}/" }.merge(options), &block) 
     1038          else 
     1039            with_options({:path_prefix => name, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(options), &block) 
     1040          end 
     1041        end 
     1042         
    10131043 
    10141044        def method_missing(route_name, *args, &proc) 
     
    11941224   
    11951225      def add_named_route(name, path, options = {}) 
    1196         named_routes[name] = add_route(path, options) 
     1226        name = options[:name_prefix] + name.to_s if options[:name_prefix] 
     1227        named_routes[name.to_sym] = add_route(path, options) 
    11971228      end 
    11981229   
  • trunk/actionpack/test/controller/resources_test.rb

    r6748 r6783  
    428428    end 
    429429  end 
    430  
     430   
    431431  protected 
    432432    def with_restful_routing(*args) 
  • trunk/actionpack/test/controller/routing_test.rb

    r6730 r6783  
    16921692    assert_equal "/people/list", url 
    16931693  end 
    1694  
     1694   
    16951695  def test_root_map 
    16961696    Object.const_set(:PeopleController, Class.new) 
     
    17061706    Object.send(:remove_const, :PeopleController) 
    17071707  end 
     1708   
     1709   
     1710  def test_namespace 
     1711    Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) }) 
     1712 
     1713    set.draw do |map|  
     1714       
     1715      map.namespace 'api' do |api| 
     1716        api.route 'inventory', :controller => "products", :action => 'inventory' 
     1717      end 
     1718       
     1719    end 
     1720 
     1721    request.path = "/api/inventory" 
     1722    request.method = :get 
     1723    assert_nothing_raised { set.recognize(request) } 
     1724    assert_equal("api/products", request.path_parameters[:controller]) 
     1725    assert_equal("inventory", request.path_parameters[:action]) 
     1726  ensure 
     1727    Object.send(:remove_const, :Api) 
     1728  end 
     1729   
     1730 
     1731  def test_namespaced_root_map 
     1732    Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) }) 
     1733 
     1734    set.draw do |map|  
     1735       
     1736      map.namespace 'api' do |api| 
     1737        api.root :controller => "products"        
     1738      end 
     1739       
     1740    end 
     1741 
     1742    request.path = "/api" 
     1743    request.method = :get 
     1744    assert_nothing_raised { set.recognize(request) } 
     1745    assert_equal("api/products", request.path_parameters[:controller]) 
     1746    assert_equal("index", request.path_parameters[:action]) 
     1747  ensure 
     1748    Object.send(:remove_const, :Api) 
     1749  end 
    17081750 
    17091751  def test_generate_finds_best_fit