Permalink
Browse files

Cleanup resource scoping by passing down the parent resource object

in the scope
  • Loading branch information...
1 parent 5835447 commit e600b41c7f2029b1fb4b75b90acc3379acf95d2b @josh josh committed Dec 8, 2009
Showing with 20 additions and 11 deletions.
  1. +20 −11 actionpack/lib/action_dispatch/routing/mapper.rb
@@ -27,6 +27,14 @@ def member_name
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 @@ def resource(*resources, &block)
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 @@ def resource(*resources, &block)
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 @@ def resources(*resources, &block)
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 @@ def resources(*resources, &block)
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 @@ def match(*args)
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

0 comments on commit e600b41

Please sign in to comment.