From bf59717b43fc7c186f9acebac27ba9ac7d59dd93 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Sat, 19 Jun 2010 07:23:57 +0100 Subject: Custom resource routes should be scoped [#3765] Signed-off-by: Jeremy Kemper --- actionpack/lib/action_dispatch/routing/mapper.rb | 43 ++++++++++++++++++++---- 1 file changed, 36 insertions(+), 7 deletions(-) (limited to 'actionpack/lib/action_dispatch/routing/mapper.rb') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 95e56566a3..0018b6485b 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -647,7 +647,9 @@ module ActionDispatch with_scope_level(:new) do scope(*parent_resource.new_scope) do - yield + scope(action_path(:new)) do + yield + end end end end @@ -723,7 +725,17 @@ module ActionDispatch options = options_for_action(args.first, options) with_exclusive_scope do - return match(path, options) + return super(path, options) + end + elsif resource_method_scope? + path = path_for_custom_action + options[:as] = name_for_action(options[:as]) if options[:as] + args.push(options) + + with_exclusive_scope do + scope(path) do + return super + end end end @@ -737,7 +749,7 @@ module ActionDispatch def root(options={}) if @scope[:scope_level] == :resources - with_scope_level(:collection) do + with_scope_level(:nested) do scope(parent_resource.path, :name_prefix => parent_resource.collection_name) do super(options) end @@ -780,12 +792,18 @@ module ActionDispatch [:resource, :resources].include?(@scope[:scope_level]) end + def resource_method_scope? + [:collection, :member, :new].include?(@scope[:scope_level]) + end + def with_exclusive_scope begin old_name_prefix, old_path = @scope[:name_prefix], @scope[:path] @scope[:name_prefix], @scope[:path] = nil, nil - yield + with_scope_level(:exclusive) do + yield + end ensure @scope[:name_prefix], @scope[:path] = old_name_prefix, old_path end @@ -844,10 +862,8 @@ module ActionDispatch end else case @scope[:scope_level] - when :collection + when :collection, :new "#{@scope[:path]}/#{action_path(action)}(.:format)" - when :new - "#{@scope[:path]}/#{action_path(:new)}/#{action_path(action)}(.:format)" else if parent_resource.shallow? "#{@scope[:module]}/#{parent_resource.path}/:id/#{action_path(action)}(.:format)" @@ -858,6 +874,19 @@ module ActionDispatch end end + def path_for_custom_action + case @scope[:scope_level] + when :collection, :new + @scope[:path] + else + if parent_resource.shallow? + "#{@scope[:module]}/#{parent_resource.path}/:id" + else + @scope[:path] + end + end + end + def action_path(name, path_names = nil) path_names ||= @scope[:path_names] path_names[name.to_sym] || name.to_s -- cgit v1.2.3