Changeset 6783
- Timestamp:
- 05/19/07 16:26:44 (2 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/resources.rb (modified) (1 diff)
- trunk/actionpack/lib/action_controller/routing.rb (modified) (5 diffs)
- trunk/actionpack/test/controller/resources_test.rb (modified) (1 diff)
- trunk/actionpack/test/controller/routing_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r6781 r6783 1 1 *SVN* 2 2 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 3 10 * Update to script.aculo.us 1.7.1_beta3. [Thomas Fuchs] 4 11 trunk/actionpack/lib/action_controller/resources.rb
r6748 r6783 339 339 entities.each { |entity| map_singleton_resource(entity, options.dup, &block) } 340 340 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 # end349 #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 for352 # 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 else357 with_options({ :path_prefix => name.to_s, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(options), &block)358 end359 end360 361 341 362 342 private trunk/actionpack/lib/action_controller/routing.rb
r6730 r6783 871 871 def divide_route_options(segments, options) 872 872 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 873 881 requirements = (options.delete(:requirements) || {}).dup 874 882 defaults = (options.delete(:defaults) || {}).dup … … 880 888 hash[key] = value 881 889 end 882 890 883 891 [defaults, requirements, conditions] 884 892 end … … 962 970 # Wrap the path with slashes 963 971 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] 965 975 966 976 segments = segments_for_route_path(path) … … 1011 1021 @set.add_named_route(name, path, options) 1012 1022 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 1013 1043 1014 1044 def method_missing(route_name, *args, &proc) … … 1194 1224 1195 1225 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) 1197 1228 end 1198 1229 trunk/actionpack/test/controller/resources_test.rb
r6748 r6783 428 428 end 429 429 end 430 430 431 431 protected 432 432 def with_restful_routing(*args) trunk/actionpack/test/controller/routing_test.rb
r6730 r6783 1692 1692 assert_equal "/people/list", url 1693 1693 end 1694 1694 1695 1695 def test_root_map 1696 1696 Object.const_set(:PeopleController, Class.new) … … 1706 1706 Object.send(:remove_const, :PeopleController) 1707 1707 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 1708 1750 1709 1751 def test_generate_finds_best_fit