Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
Created August 24, 2010 21:17
Show Gist options
  • Save pixeltrix/548344 to your computer and use it in GitHub Desktop.
Save pixeltrix/548344 to your computer and use it in GitHub Desktop.
From d00ce654a1442dcea7bf9585647d1b7015cc9cbe Mon Sep 17 00:00:00 2001
From: Andrew White <[email protected]>
Date: Tue, 24 Aug 2010 22:16:34 +0100
Subject: [PATCH] Use nested scope for routes defined at the :resources scope level
---
actionpack/lib/action_dispatch/routing/mapper.rb | 11 ++++++-----
actionpack/test/dispatch/routing_test.rb | 7 +++++++
2 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index 9aa34e7..9a92ed0 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -695,15 +695,14 @@ module ActionDispatch
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))
@@ -900,6 +899,8 @@ module ActionDispatch
end
name = case @scope[:scope_level]
+ when :nested
+ [member_name, prefix]
when :collection
[prefix, name_prefix, collection_name]
when :new
diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb
index 5b2547e..a4b8faf 100644
--- a/actionpack/test/dispatch/routing_test.rb
+++ b/actionpack/test/dispatch/routing_test.rb
@@ -231,6 +231,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
get "inactive", :on => :collection
post "deactivate", :on => :member
get "old", :on => :collection, :as => :stale
+ get "export"
end
namespace :api do
@@ -2091,6 +2092,12 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
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
--
1.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment