Skip to content

Commit

Permalink
Cleanup resource scoping by passing down the parent resource object
Browse files Browse the repository at this point in the history
in the scope
  • Loading branch information
josh committed Dec 8, 2009
1 parent 5835447 commit e600b41
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit e600b41

Please sign in to comment.