Skip to content

Commit 9f68d52

Browse files
committed
Merge pull request rails#8114 from guilleiguaran/use-symbols-in-scope
Allow setting a symbol as path in scope on routes
2 parents 0134ca6 + 0d3a9e8 commit 9f68d52

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

actionpack/CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
## Rails 4.0.0 (unreleased) ##
22

3+
* Allow setting a symbol as path in scope on routes. This is now allowed:
4+
5+
scope :api do
6+
resources :users
7+
end
8+
9+
also is possible pass multiple symbols to scope to shorten multiple nested scopes:
10+
11+
scope :api do
12+
scope :v1 do
13+
resources :users
14+
end
15+
end
16+
17+
can be rewritten as:
18+
19+
scope :api, :v1 do
20+
resources :users
21+
end
22+
23+
*Guillermo Iguaran*
24+
325
* Fix error when using a non-hash query argument named "params" in `url_for`.
426

527
Before:

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ def scope(*args)
644644
options = args.extract_options!
645645
options = options.dup
646646

647-
options[:path] = args.first if args.first.is_a?(String)
647+
options[:path] = args.flatten.join('/') if args.any?
648648
recover = {}
649649

650650
options[:constraints] ||= {}

actionpack/test/dispatch/routing_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,14 @@ def self.call(params, request)
370370
scope :path => 'api' do
371371
resource :me
372372
get '/' => 'mes#index'
373+
scope :v2 do
374+
resource :me, as: 'v2_me'
375+
get '/' => 'mes#index'
376+
end
377+
378+
scope :v3, :admin do
379+
resource :me, as: 'v3_me'
380+
end
373381
end
374382

375383
get "(/:username)/followers" => "followers#index"
@@ -1467,6 +1475,18 @@ def test_path_scope
14671475
assert_equal 'mes#index', @response.body
14681476
end
14691477

1478+
def test_symbol_scope
1479+
get '/api/v2/me'
1480+
assert_equal 'mes#show', @response.body
1481+
assert_equal '/api/v2/me', v2_me_path
1482+
1483+
get '/api/v2'
1484+
assert_equal 'mes#index', @response.body
1485+
1486+
get '/api/v3/admin/me'
1487+
assert_equal 'mes#show', @response.body
1488+
end
1489+
14701490
def test_url_generator_for_generic_route
14711491
get 'whatever/foo/bar'
14721492
assert_equal 'foo#bar', @response.body

0 commit comments

Comments
 (0)