Skip to content

Commit

Permalink
Use nested scope for routes defined at the :resources scope level (as…
Browse files Browse the repository at this point in the history
… in Rails 2.3)

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
pixeltrix authored and josevalim committed Aug 25, 2010
1 parent 3ac6646 commit 1847d33
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 6 additions & 5 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Expand Up @@ -695,15 +695,14 @@ def match(*args)
raise ArgumentError, "Unknown scope #{on.inspect} given to :on"
end

if @scope[:scope_level] == :resource
if @scope[:scope_level] == :resources
args.push(options)
return nested { match(*args) }
elsif @scope[:scope_level] == :resource
args.push(options)
return member { match(*args) }
end

if resource_scope?
raise ArgumentError, "can't define route directly in resource(s) scope"
end

action = args.first
path = path_for_action(action, options.delete(:path))

Expand Down Expand Up @@ -900,6 +899,8 @@ def name_for_action(action, as=nil)
end

name = case @scope[:scope_level]
when :nested
[member_name, prefix]
when :collection
[prefix, name_prefix, collection_name]
when :new
Expand Down
7 changes: 7 additions & 0 deletions actionpack/test/dispatch/routing_test.rb
Expand Up @@ -231,6 +231,7 @@ def self.matches?(request)
get "inactive", :on => :collection
post "deactivate", :on => :member
get "old", :on => :collection, :as => :stale
get "export"
end

namespace :api do
Expand Down Expand Up @@ -2091,6 +2092,12 @@ def test_custom_resource_actions_defined_using_string
assert_equal '/customers/1/invoices/aged/3', aged_customer_invoices_path(:customer_id => '1', :months => '3')
end

def test_route_defined_in_resources_scope_level
get '/customers/1/export'
assert_equal 'customers#export', @response.body
assert_equal '/customers/1/export', customer_export_path(:customer_id => '1')
end

private
def with_test_routes
yield
Expand Down

0 comments on commit 1847d33

Please sign in to comment.