aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/routing
diff options
context:
space:
mode:
authorDiego Carrion <dc.rec1@gmail.com>2010-06-07 18:08:48 -0500
committerDavid Heinemeier Hansson <david@loudthinking.com>2010-06-07 18:09:20 -0500
commit67a60ee314f53abcde78f8ecd2a1f7c9ef8264e1 (patch)
tree816f230f970160d02e496a4ae8dc7daece83de0b /actionpack/lib/action_dispatch/routing
parenta04060fb6fe006b1dbc224263dd6c39525733c6d (diff)
downloadrails-67a60ee314f53abcde78f8ecd2a1f7c9ef8264e1.tar.gz
rails-67a60ee314f53abcde78f8ecd2a1f7c9ef8264e1.tar.bz2
rails-67a60ee314f53abcde78f8ecd2a1f7c9ef8264e1.zip
Add shallow routes to the new router [Closes #3765]
Diffstat (limited to 'actionpack/lib/action_dispatch/routing')
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb29
1 files changed, 26 insertions, 3 deletions
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index ae4417b56c..b64c57f985 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -350,6 +350,10 @@ module ActionDispatch
scope(:constraints => constraints) { yield }
end
+ def shallow
+ scope(:shallow => true) { yield }
+ end
+
def defaults(defaults = {})
scope(:defaults => defaults) { yield }
end
@@ -374,12 +378,21 @@ module ActionDispatch
@scope_options ||= private_methods.grep(/^merge_(.+)_scope$/) { $1.to_sym }
end
+ def merge_shallow_scope(parent, child)
+ parent or child
+ end
+
def merge_path_scope(parent, child)
- Mapper.normalize_path("#{parent}/#{child}")
+ parent_path = (@scope[:shallow] and child.eql?(':id')) ? parent.split('/').last : parent
+ Mapper.normalize_path "#{parent_path}/#{child}"
end
def merge_name_prefix_scope(parent, child)
- parent ? "#{parent}_#{child}" : child
+ if @scope[:shallow]
+ child
+ else
+ parent ? "#{parent}_#{child}" : child
+ end
end
def merge_module_scope(parent, child)
@@ -514,6 +527,10 @@ module ActionDispatch
options["#{singular}_id".to_sym] = id_constraint if id_constraint?
options
end
+
+ def shallow?
+ options[:shallow]
+ end
end
class SingletonResource < Resource #:nodoc:
@@ -581,8 +598,12 @@ module ActionDispatch
resource = Resource.new(resources.pop, options)
- scope(:path => resource.path, :controller => resource.controller) do
+ scope(:path => resource.path, :controller => resource.controller, :shallow => resource.shallow?) do
with_scope_level(:resources, resource) do
+ if @scope[:shallow] && @scope[:name_prefix]
+ @scope[:path] = "/#{@scope[:name_prefix].pluralize}/:#{@scope[:name_prefix]}_id/#{resource.path}"
+ end
+
yield if block_given?
with_scope_level(:collection) do
@@ -596,6 +617,8 @@ module ActionDispatch
with_scope_level(:member) do
scope(':id') do
scope(resource.options) do
+ @scope[:name_prefix] = nil if @scope[:shallow]
+
get :show if resource.actions.include?(:show)
put :update if resource.actions.include?(:update)
delete :destroy if resource.actions.include?(:destroy)