Skip to content

Commit cc26b6b

Browse files
committed
Routes specifying 'to:' must be a string that contains a "#" or a rack
application. Use of a symbol should be replaced with `action: symbol`. Use of a string without a "#" should be replaced with `controller: string`.
1 parent 9b09e60 commit cc26b6b

6 files changed

Lines changed: 149 additions & 27 deletions

File tree

actionpack/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Routes specifying 'to:' must be a string that contains a "#" or a rack
2+
application. Use of a symbol should be replaced with `action: symbol`.
3+
Use of a string without a "#" should be replaced with `controller: string`.
4+
15
* Deprecate all *_filter callbacks in favor of *_action callbacks.
26

37
*Rafael Mendonça França*

actionpack/aaron_path.dot

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
digraph parse_tree {
2+
size="8,5"
3+
node [shape = none];
4+
edge [dir = none];
5+
70293339236320 [label=""];
6+
70293339236820 [label=""];
7+
70293339237000 [label=""];
8+
70293339237340 [label=""];
9+
70293339239200 [label=""];
10+
70293339239520 [label="/"];
11+
70293339239300 [label="foo"];
12+
70293339237420 [label="()"];
13+
70293339237520 [label=""];
14+
70293339238380 [label=""];
15+
70293339238820 [label="/"];
16+
70293339238520 [label=":bar"];
17+
70293339237620 [label="()"];
18+
70293339237860 [label=":baz"];
19+
70293339237080 [label="/"];
20+
70293339236920 [label=":aaron"];
21+
70293339236360 [label="()"];
22+
70293339236520 [label=""];
23+
70293339236700 [label="."];
24+
70293339236560 [label=":format"];
25+
70293339236320 -> 70293339236820;
26+
70293339236320 -> 70293339236360;
27+
70293339236820 -> 70293339237000;
28+
70293339236820 -> 70293339236920;
29+
70293339237000 -> 70293339237340;
30+
70293339237000 -> 70293339237080;
31+
70293339237340 -> 70293339239200;
32+
70293339237340 -> 70293339237420;
33+
70293339239200 -> 70293339239520;
34+
70293339239200 -> 70293339239300;
35+
70293339237420 -> 70293339237520;
36+
70293339237520 -> 70293339238380;
37+
70293339237520 -> 70293339237620;
38+
70293339238380 -> 70293339238820;
39+
70293339238380 -> 70293339238520;
40+
70293339237620 -> 70293339237860;
41+
70293339236360 -> 70293339236520;
42+
70293339236520 -> 70293339236700;
43+
70293339236520 -> 70293339236560;
44+
}

actionpack/ebi_path.dot

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
digraph parse_tree {
2+
size="8,5"
3+
node [shape = none];
4+
edge [dir = none];
5+
70293330639600 [label=""];
6+
70293330640700 [label=""];
7+
70293330655800 [label=""];
8+
70293330649320 [label=""];
9+
70293330650540 [label=""];
10+
70293330650820 [label="/"];
11+
70293330650620 [label="foo"];
12+
70293330649420 [label="()"];
13+
70293330649540 [label=""];
14+
70293330650120 [label=""];
15+
70293330650340 [label="/"];
16+
70293330650180 [label=":bar"];
17+
70293330649600 [label="()"];
18+
70293330649720 [label=""];
19+
70293330649920 [label="/"];
20+
70293330649760 [label=":baz"];
21+
70293330655220 [label="/"];
22+
70293330657120 [label=":aaron"];
23+
70293330639740 [label="()"];
24+
70293330639980 [label=""];
25+
70293330640400 [label="."];
26+
70293330640100 [label=":format"];
27+
70293330639600 -> 70293330640700;
28+
70293330639600 -> 70293330639740;
29+
70293330640700 -> 70293330655800;
30+
70293330640700 -> 70293330657120;
31+
70293330655800 -> 70293330649320;
32+
70293330655800 -> 70293330655220;
33+
70293330649320 -> 70293330650540;
34+
70293330649320 -> 70293330649420;
35+
70293330650540 -> 70293330650820;
36+
70293330650540 -> 70293330650620;
37+
70293330649420 -> 70293330649540;
38+
70293330649540 -> 70293330650120;
39+
70293330649540 -> 70293330649600;
40+
70293330650120 -> 70293330650340;
41+
70293330650120 -> 70293330650180;
42+
70293330649600 -> 70293330649720;
43+
70293330649720 -> 70293330649920;
44+
70293330649720 -> 70293330649760;
45+
70293330639740 -> 70293330639980;
46+
70293330639980 -> 70293330640400;
47+
70293330639980 -> 70293330640100;
48+
}

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require 'active_support/inflector'
88
require 'action_dispatch/routing/redirection'
99
require 'action_dispatch/routing/endpoint'
10+
require 'active_support/deprecation'
1011

1112
module ActionDispatch
1213
module Routing
@@ -284,9 +285,13 @@ def check_part(name, part, path_params, hash)
284285

285286
def get_controller_and_action(controller, action, to, modyoule)
286287
case to
287-
when Symbol then action = to.to_s
288+
when Symbol
289+
ActiveSupport::Deprecation.warn "defining a route where `to` is a symbol is deprecated. Please change \"to: :#{to}\" to \"action: :#{to}\""
290+
action = to.to_s
288291
when /#/ then controller, action = to.split('#')
289-
when String then controller = to
292+
when String
293+
ActiveSupport::Deprecation.warn "defining a route where `to` is a controller without an action is deprecated. Please change \"to: :#{to}\" to \"controller: :#{to}\""
294+
controller = to
290295
end
291296

292297
if modyoule && !controller.is_a?(Regexp)
@@ -1458,7 +1463,20 @@ def match(path, *rest)
14581463
if rest.empty? && Hash === path
14591464
options = path
14601465
path, to = options.find { |name, _value| name.is_a?(String) }
1461-
options[:to] = to
1466+
1467+
case to
1468+
when Symbol
1469+
options[:action] = to
1470+
when String
1471+
if to =~ /#/
1472+
options[:to] = to
1473+
else
1474+
options[:controller] = to
1475+
end
1476+
else
1477+
options[:to] = to
1478+
end
1479+
14621480
options.delete(path)
14631481
paths = [path]
14641482
else

actionpack/test/dispatch/request/multipart_params_parsing_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def teardown
145145
test "does not raise EOFError on GET request with multipart content-type" do
146146
with_routing do |set|
147147
set.draw do
148-
get ':action', to: 'multipart_params_parsing_test/test'
148+
get ':action', controller: 'multipart_params_parsing_test/test'
149149
end
150150
headers = { "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x" }
151151
get "/parse", {}, headers
@@ -174,7 +174,7 @@ def parse_multipart(name)
174174
def with_test_routing
175175
with_routing do |set|
176176
set.draw do
177-
post ':action', :to => 'multipart_params_parsing_test/test'
177+
post ':action', :controller => 'multipart_params_parsing_test/test'
178178
end
179179
yield
180180
end

actionpack/test/dispatch/routing_test.rb

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,8 @@ def test_global
361361
draw do
362362
controller(:global) do
363363
get 'global/hide_notice'
364-
get 'global/export', :to => :export, :as => :export_request
365-
get '/export/:id/:file', :to => :export, :as => :export_download, :constraints => { :file => /.*/ }
364+
get 'global/export', :action => :export, :as => :export_request
365+
get '/export/:id/:file', :action => :export, :as => :export_download, :constraints => { :file => /.*/ }
366366
get 'global/:action'
367367
end
368368
end
@@ -730,8 +730,8 @@ def test_replies
730730
draw do
731731
resources :replies do
732732
member do
733-
put :answer, :to => :mark_as_answer
734-
delete :answer, :to => :unmark_as_answer
733+
put :answer, :action => :mark_as_answer
734+
delete :answer, :action => :unmark_as_answer
735735
end
736736
end
737737
end
@@ -1188,7 +1188,7 @@ def test_articles_with_id
11881188
controller :articles do
11891189
scope '/articles', :as => 'article' do
11901190
scope :path => '/:title', :title => /[a-z]+/, :as => :with_title do
1191-
get '/:id', :to => :with_id, :as => ""
1191+
get '/:id', :action => :with_id, :as => ""
11921192
end
11931193
end
11941194
end
@@ -1435,7 +1435,7 @@ def test_dynamically_generated_helpers_on_collection_do_not_clobber_resources_ur
14351435
def test_scoped_controller_with_namespace_and_action
14361436
draw do
14371437
namespace :account do
1438-
get ':action/callback', :action => /twitter|github/, :to => "callbacks", :as => :callback
1438+
get ':action/callback', :action => /twitter|github/, :controller => "callbacks", :as => :callback
14391439
end
14401440
end
14411441

@@ -1492,7 +1492,7 @@ def test_redirect_with_port
14921492
def test_normalize_namespaced_matches
14931493
draw do
14941494
namespace :account do
1495-
get 'description', :to => :description, :as => "description"
1495+
get 'description', :action => :description, :as => "description"
14961496
end
14971497
end
14981498

@@ -2154,7 +2154,7 @@ def test_custom_resource_routes_are_scoped
21542154
end
21552155
resources :invoices do
21562156
get "outstanding" => "invoices#outstanding", :on => :collection
2157-
get "overdue", :to => :overdue, :on => :collection
2157+
get "overdue", :action => :overdue, :on => :collection
21582158
get "print" => "invoices#print", :as => :print, :on => :member
21592159
post "preview" => "invoices#preview", :as => :preview, :on => :new
21602160
end
@@ -2996,7 +2996,9 @@ def test_controller_name_with_leading_slash_raise_error
29962996
end
29972997

29982998
assert_raise(ArgumentError) do
2999-
draw { controller("/feeds") { get '/feeds/:service', :to => :show } }
2999+
assert_deprecated do
3000+
draw { controller("/feeds") { get '/feeds/:service', :to => :show } }
3001+
end
30003002
end
30013003

30023004
assert_raise(ArgumentError) do
@@ -3284,20 +3286,24 @@ def test_mix_string_to_action
32843286
end
32853287

32863288
def test_mix_symbol_to_controller_action
3287-
draw do
3288-
get '/projects', controller: 'project_files',
3289-
action: 'index',
3290-
to: :show
3289+
assert_deprecated do
3290+
draw do
3291+
get '/projects', controller: 'project_files',
3292+
action: 'index',
3293+
to: :show
3294+
end
32913295
end
32923296
get '/projects'
32933297
assert_equal 'project_files#show', @response.body
32943298
end
32953299

3296-
def test_mix_string_to_controller_action
3297-
draw do
3298-
get '/projects', controller: 'project_files',
3299-
action: 'index',
3300-
to: 'show'
3300+
def test_mix_string_to_controller_action_no_hash
3301+
assert_deprecated do
3302+
draw do
3303+
get '/projects', controller: 'project_files',
3304+
action: 'index',
3305+
to: 'show'
3306+
end
33013307
end
33023308
get '/projects'
33033309
assert_equal 'show#index', @response.body
@@ -3567,16 +3573,18 @@ def draw(&block)
35673573
def test_missing_controller
35683574
ex = assert_raises(ArgumentError) {
35693575
draw do
3570-
get '/foo/bar', :to => :index
3576+
get '/foo/bar', :action => :index
35713577
end
35723578
}
35733579
assert_match(/Missing :controller/, ex.message)
35743580
end
35753581

35763582
def test_missing_action
35773583
ex = assert_raises(ArgumentError) {
3578-
draw do
3579-
get '/foo/bar', :to => 'foo'
3584+
assert_deprecated do
3585+
draw do
3586+
get '/foo/bar', :to => 'foo'
3587+
end
35803588
end
35813589
}
35823590
assert_match(/Missing :action/, ex.message)
@@ -4083,7 +4091,7 @@ def show
40834091
set.draw do
40844092
get "/bar/:id", :to => redirect("/foo/show/%{id}")
40854093
get "/foo/show(/:id)", :to => "test_invalid_urls/foo#show"
4086-
get "/foo(/:action(/:id))", :to => "test_invalid_urls/foo"
4094+
get "/foo(/:action(/:id))", :controller => "test_invalid_urls/foo"
40874095
get "/:controller(/:action(/:id))"
40884096
end
40894097

0 commit comments

Comments
 (0)