Skip to content

Commit 3b4255e

Browse files
committed
Use keywords in routing mapper
Converts hashes to keywords in ActionDispatch::Routing::Mapper. This makes the mapper a little easier to reason about, and slightly faster.
1 parent dd33918 commit 3b4255e

File tree

6 files changed

+393
-202
lines changed

6 files changed

+393
-202
lines changed

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 323 additions & 165 deletions
Large diffs are not rendered by default.

actionpack/test/controller/resources_test.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_multiple_default_restful_routes
7676
def test_multiple_resources_with_options
7777
expected_options = { controller: "threads", action: "index" }
7878

79-
with_restful_routing :messages, :comments, expected_options.slice(:controller) do
79+
with_restful_routing :messages, :comments, controller: "threads" do
8080
assert_recognizes(expected_options, path: "comments")
8181
assert_recognizes(expected_options, path: "messages")
8282
end
@@ -1176,17 +1176,15 @@ def test_invalid_except_option_for_singleton_resource
11761176
end
11771177

11781178
private
1179-
def with_restful_routing(*args)
1180-
options = args.extract_options!
1179+
def with_restful_routing(*args, **options)
11811180
collection_methods = options.delete(:collection)
11821181
member_methods = options.delete(:member)
11831182
path_prefix = options.delete(:path_prefix)
1184-
args.push(options)
11851183

11861184
with_routing do |set|
11871185
set.draw do
11881186
scope(path_prefix || "") do
1189-
resources(*args) do
1187+
resources(*args, **options) do
11901188
if collection_methods
11911189
collection do
11921190
collection_methods.each do |name, method|

actionpack/test/dispatch/mapper_test.rb

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test_mapping_requirements
9393
options = {}
9494
scope = Mapper::Scope.new({})
9595
ast = Journey::Parser.parse "/store/:name(*rest)"
96-
m = Mapper::Mapping.build(scope, FakeSet.new, ast, "foo", "bar", nil, [:get], nil, {}, true, options)
96+
m = Mapper::Mapping.build(scope, FakeSet.new, ast, "foo", "bar", nil, [:get], nil, {}, true, nil, options)
9797
assert_equal(/.+?/m, m.requirements[:rest])
9898
end
9999

@@ -225,6 +225,63 @@ def test_scope_does_not_destructively_mutate_default_options
225225
end
226226
end
227227
end
228+
229+
def test_deprecated_hash
230+
fakeset = FakeSet.new
231+
mapper = Mapper.new fakeset
232+
233+
assert_deprecated(ActionDispatch.deprecator) do
234+
mapper.get "/foo", { to: "home#index" }
235+
end
236+
237+
assert_deprecated(ActionDispatch.deprecator) do
238+
mapper.post "/foo", { to: "home#index" }
239+
end
240+
241+
assert_deprecated(ActionDispatch.deprecator) do
242+
mapper.put "/foo", { to: "home#index" }
243+
end
244+
245+
assert_deprecated(ActionDispatch.deprecator) do
246+
mapper.patch "/foo", { to: "home#index" }
247+
end
248+
249+
assert_deprecated(ActionDispatch.deprecator) do
250+
mapper.delete "/foo", { to: "home#index" }
251+
end
252+
253+
assert_deprecated(ActionDispatch.deprecator) do
254+
mapper.options "/foo", { to: "home#index" }
255+
end
256+
257+
assert_deprecated(ActionDispatch.deprecator) do
258+
mapper.connect "/foo", { to: "home#index" }
259+
end
260+
261+
assert_deprecated(ActionDispatch.deprecator) do
262+
mapper.match "/foo", { to: "home#index", via: :get }
263+
end
264+
265+
assert_deprecated(ActionDispatch.deprecator) do
266+
mapper.mount(lambda { |env| [200, {}, [""]] }, { at: "/" })
267+
end
268+
269+
assert_deprecated(ActionDispatch.deprecator) do
270+
mapper.scope("/hello", { only: :get }) { }
271+
end
272+
273+
assert_deprecated(ActionDispatch.deprecator) do
274+
mapper.namespace(:admin, { module: "sekret" }) { }
275+
end
276+
277+
assert_deprecated(ActionDispatch.deprecator) do
278+
mapper.resource(:user, { only: :show }) { }
279+
end
280+
281+
assert_deprecated(ActionDispatch.deprecator) do
282+
mapper.resources(:users, { only: :show }) { }
283+
end
284+
end
228285
end
229286
end
230287
end

actionpack/test/dispatch/routing/concerns_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ class ReviewsController < ResourcesController; end
77
class RoutingConcernsTest < ActionDispatch::IntegrationTest
88
class Reviewable
99
def self.call(mapper, options = {})
10-
mapper.resources :reviews, options
10+
mapper.resources :reviews, **options
1111
end
1212
end
1313

1414
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
1515
app.draw do
1616
concern :commentable do |options|
17-
resources :comments, options
17+
resources :comments, **options
1818
end
1919

2020
concern :image_attachable do

actionpack/test/dispatch/routing_test.rb

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -988,13 +988,13 @@ def test_resources_for_uncountable_names
988988

989989
def test_resource_does_not_modify_passed_options
990990
options = { id: /.+?/, format: /json|xml/ }
991-
draw { resource :user, options }
991+
draw { resource :user, **options }
992992
assert_equal({ id: /.+?/, format: /json|xml/ }, options)
993993
end
994994

995995
def test_resources_does_not_modify_passed_options
996996
options = { id: /.+?/, format: /json|xml/ }
997-
draw { resources :users, options }
997+
draw { resources :users, **options }
998998
assert_equal({ id: /.+?/, format: /json|xml/ }, options)
999999
end
10001000

@@ -2107,32 +2107,10 @@ def test_resource_merges_options_from_scope
21072107
end
21082108
end
21092109

2110-
assert_equal "/account", account_path
21112110
assert_raise(NoMethodError) { new_account_path }
21122111

21132112
get "/account/new"
21142113
assert_equal 404, status
2115-
2116-
get "/account"
2117-
assert_equal 200, status
2118-
end
2119-
2120-
def test_resource_merges_options_from_scope_hash
2121-
draw do
2122-
scope_options = { only: :show }
2123-
scope scope_options do
2124-
resource :account
2125-
end
2126-
end
2127-
2128-
assert_equal "/account", account_path
2129-
assert_raise(NoMethodError) { new_account_path }
2130-
2131-
get "/account/new"
2132-
assert_equal 404, status
2133-
2134-
get "/account"
2135-
assert_equal 200, status
21362114
end
21372115

21382116
def test_resources_merges_options_from_scope

actionpack/test/journey/router_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def test_nil_path_parts_are_ignored
219219
def test_generate_slash
220220
params = [ [:controller, "tasks"],
221221
[:action, "show"] ]
222-
get "/", Hash[params]
222+
get "/", **Hash[params]
223223

224224
path, _ = _generate(nil, Hash[params], {})
225225
assert_equal "/", path
@@ -498,15 +498,15 @@ def _generate(route_name, options, recall)
498498
[uri.path, params]
499499
end
500500

501-
def get(*args)
501+
def get(...)
502502
ActionDispatch.deprecator.silence do
503-
mapper.get(*args)
503+
mapper.get(...)
504504
end
505505
end
506506

507-
def match(*args)
507+
def match(...)
508508
ActionDispatch.deprecator.silence do
509-
mapper.match(*args)
509+
mapper.match(...)
510510
end
511511
end
512512

0 commit comments

Comments
 (0)