aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG7
-rw-r--r--actionpack/lib/action_controller/resources.rb20
-rw-r--r--actionpack/lib/action_controller/routing.rb37
-rw-r--r--actionpack/test/controller/resources_test.rb2
-rw-r--r--actionpack/test/controller/routing_test.rb44
5 files changed, 85 insertions, 25 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 917874acca..567c8d278f 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,12 @@
*SVN*
+* Allow routes to be decalred under namespaces [Tobias Luetke]:
+
+ map.namespace :admin do |admin|
+ admin.root :controller => "products"
+ admin.feed 'feed.xml', :controller => 'products', :action => 'feed', :format => 'xml'
+ end
+
* Update to script.aculo.us 1.7.1_beta3. [Thomas Fuchs]
* observe_form always sends the serialized form. #5271 [manfred, normelton@gmail.com]
diff --git a/actionpack/lib/action_controller/resources.rb b/actionpack/lib/action_controller/resources.rb
index df0c64d91c..bb92684e5c 100644
--- a/actionpack/lib/action_controller/resources.rb
+++ b/actionpack/lib/action_controller/resources.rb
@@ -339,26 +339,6 @@ 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)
- if options[:namespace]
- with_options({:path_prefix => "#{options.delete(:path_prefix)}/#{name}", :name_prefix => "#{options.delete(:name_prefix)}#{name}_", :namespace => "#{options.delete(:namespace)}#{name}/" }.merge(options), &block)
- else
- with_options({ :path_prefix => name.to_s, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(options), &block)
- end
- end
-
-
private
def map_resource(entities, options = {}, &block)
resource = Resource.new(entities, options)
diff --git a/actionpack/lib/action_controller/routing.rb b/actionpack/lib/action_controller/routing.rb
index be5eb164a1..00d5050b7e 100644
--- a/actionpack/lib/action_controller/routing.rb
+++ b/actionpack/lib/action_controller/routing.rb
@@ -870,6 +870,14 @@ module ActionController
# and requirements.
def divide_route_options(segments, options)
options = options.dup
+
+ if options[:namespace]
+ options[:controller] = "#{options[:path_prefix]}/#{options[:controller]}"
+ options.delete(:path_prefix)
+ options.delete(:name_prefix)
+ options.delete(:namespace)
+ end
+
requirements = (options.delete(:requirements) || {}).dup
defaults = (options.delete(:defaults) || {}).dup
conditions = (options.delete(:conditions) || {}).dup
@@ -879,7 +887,7 @@ module ActionController
hash = (path_keys.include?(key) && ! value.is_a?(Regexp)) ? defaults : requirements
hash[key] = value
end
-
+
[defaults, requirements, conditions]
end
@@ -961,7 +969,9 @@ module ActionController
def build(path, options)
# Wrap the path with slashes
path = "/#{path}" unless path[0] == ?/
- path = "#{path}/" unless path[-1] == ?/
+ path = "#{path}/" unless path[-1] == ?/
+
+ path = "/#{options[:path_prefix]}#{path}" if options[:path_prefix]
segments = segments_for_route_path(path)
defaults, requirements, conditions = divide_route_options(segments, options)
@@ -1010,6 +1020,26 @@ module ActionController
def named_route(name, path, options = {})
@set.add_named_route(name, path, options)
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)
+ if options[:namespace]
+ with_options({:path_prefix => "#{options.delete(:path_prefix)}/#{name}", :name_prefix => "#{options.delete(:name_prefix)}#{name}_", :namespace => "#{options.delete(:namespace)}#{name}/" }.merge(options), &block)
+ else
+ with_options({:path_prefix => name, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(options), &block)
+ end
+ end
+
def method_missing(route_name, *args, &proc)
super unless args.length >= 1 && proc.nil?
@@ -1193,7 +1223,8 @@ module ActionController
end
def add_named_route(name, path, options = {})
- named_routes[name] = add_route(path, options)
+ name = options[:name_prefix] + name.to_s if options[:name_prefix]
+ named_routes[name.to_sym] = add_route(path, options)
end
def options_as_params(options)
diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb
index 1f61377ad9..979008f21b 100644
--- a/actionpack/test/controller/resources_test.rb
+++ b/actionpack/test/controller/resources_test.rb
@@ -427,7 +427,7 @@ class ResourcesTest < Test::Unit::TestCase
assert_simply_restful_for :products, :controller => "backoffice/products"
end
end
-
+
protected
def with_restful_routing(*args)
with_routing do |set|
diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb
index 0197bf10e2..2abccd7402 100644
--- a/actionpack/test/controller/routing_test.rb
+++ b/actionpack/test/controller/routing_test.rb
@@ -1691,7 +1691,7 @@ class RouteSetTest < Test::Unit::TestCase
url = set.generate(:controller => "people", :action => "list")
assert_equal "/people/list", url
end
-
+
def test_root_map
Object.const_set(:PeopleController, Class.new)
@@ -1705,6 +1705,48 @@ class RouteSetTest < Test::Unit::TestCase
ensure
Object.send(:remove_const, :PeopleController)
end
+
+
+ def test_namespace
+ Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
+
+ set.draw do |map|
+
+ map.namespace 'api' do |api|
+ api.route 'inventory', :controller => "products", :action => 'inventory'
+ end
+
+ end
+
+ request.path = "/api/inventory"
+ request.method = :get
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("api/products", request.path_parameters[:controller])
+ assert_equal("inventory", request.path_parameters[:action])
+ ensure
+ Object.send(:remove_const, :Api)
+ end
+
+
+ def test_namespaced_root_map
+ Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
+
+ set.draw do |map|
+
+ map.namespace 'api' do |api|
+ api.root :controller => "products"
+ end
+
+ end
+
+ request.path = "/api"
+ request.method = :get
+ assert_nothing_raised { set.recognize(request) }
+ assert_equal("api/products", request.path_parameters[:controller])
+ assert_equal("index", request.path_parameters[:action])
+ ensure
+ Object.send(:remove_const, :Api)
+ end
def test_generate_finds_best_fit
set.draw do |map|