From e600b41c7f2029b1fb4b75b90acc3379acf95d2b Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 7 Dec 2009 19:47:47 -0600 Subject: Cleanup resource scoping by passing down the parent resource object in the scope --- actionpack/lib/action_dispatch/routing/mapper.rb | 31 +++++++++++++++--------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 7eb648cedf..9ca4e16802 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -27,6 +27,14 @@ module ActionDispatch def collection_name plural end + + def id_segment + ":#{singular}_id" + end + + def member_name_prefix + "#{member_name}_" + end end class SingletonResource < Resource #:nodoc: @@ -51,10 +59,8 @@ module ActionDispatch resource = SingletonResource.new(resources.pop) if @scope[:scope_level] == :resources - parent_resource = @scope[:scope_level_options][:name] - parent_named_prefix = @scope[:scope_level_options][:name_prefix] with_scope_level(:member) do - scope(":#{parent_resource}_id", :name_prefix => parent_named_prefix) do + scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name_prefix) do resource(resource.name, options, &block) end end @@ -63,7 +69,7 @@ module ActionDispatch controller(resource.controller) do namespace(resource.name) do - with_scope_level(:resource, :name => resource.singular, :name_prefix => "#{resource.member_name}_") do + with_scope_level(:resource, resource) do yield if block_given? get "", :to => :show, :as => resource.member_name @@ -91,10 +97,8 @@ module ActionDispatch resource = Resource.new(resources.pop) if @scope[:scope_level] == :resources - parent_resource = @scope[:scope_level_options][:name] - parent_named_prefix = @scope[:scope_level_options][:name_prefix] with_scope_level(:member) do - scope(":#{parent_resource}_id", :name_prefix => parent_named_prefix) do + scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name_prefix) do resources(resource.name, options, &block) end end @@ -103,7 +107,7 @@ module ActionDispatch controller(resource.controller) do namespace(resource.name) do - with_scope_level(:resources, :name => resource.singular, :name_prefix => "#{resource.member_name}_") do + with_scope_level(:resources, resource) do yield if block_given? collection do @@ -165,14 +169,19 @@ module ActionDispatch super end + protected + def parent_resource + @scope[:scope_level_resource] + end + private - def with_scope_level(kind, options = {}) + def with_scope_level(kind, resource = parent_resource) old, @scope[:scope_level] = @scope[:scope_level], kind - old_options, @scope[:scope_level_options] = @scope[:scope_level_options], options + old_resource, @scope[:scope_level_resource] = @scope[:scope_level_resource], resource yield ensure @scope[:scope_level] = old - @scope[:scope_level_options] = old_options + @scope[:scope_level_resource] = old_resource end end -- cgit v1.2.3