Skip to content

Commit e533a32

Browse files
committed
Inline find_routes into recognize
(Separate from the previous commit so that the original refactor is clear in the diff) Now that recognize just delegates to find_routes, there is no longer a need for find_routes to exist separately.
1 parent 1243b34 commit e533a32

File tree

1 file changed

+58
-62
lines changed
  • actionpack/lib/action_dispatch/journey

1 file changed

+58
-62
lines changed

actionpack/lib/action_dispatch/journey/router.rb

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def eager_load!
2727
end
2828

2929
def serve(req)
30-
find_routes(req) do |route, parameters|
30+
recognize(req) do |route, parameters|
3131
req.path_parameters = parameters
3232
req.route = route
3333

@@ -39,8 +39,63 @@ def serve(req)
3939
[404, { Constants::X_CASCADE => "pass" }, ["Not Found"]]
4040
end
4141

42-
def recognize(rails_req, &block)
43-
find_routes(rails_req, &block)
42+
def recognize(req, &block)
43+
req_params = req.path_parameters
44+
path_info = req.path_info
45+
script_name = req.script_name
46+
47+
routes = filter_routes(path_info)
48+
49+
custom_routes.each { |r|
50+
routes << r if r.path.match?(path_info)
51+
}
52+
53+
if req.head?
54+
routes = match_head_routes(routes, req)
55+
else
56+
routes.select! { |r| r.matches?(req) }
57+
end
58+
59+
if routes.size > 1
60+
routes.sort! do |a, b|
61+
a.precedence <=> b.precedence
62+
end
63+
end
64+
65+
routes.each do |r|
66+
match_data = r.path.match(path_info)
67+
68+
path_parameters = req_params.merge r.defaults
69+
70+
index = 1
71+
match_data.names.each do |name|
72+
if val = match_data[index]
73+
val = if val.include?("%")
74+
CGI.unescapeURIComponent(val)
75+
else
76+
val
77+
end
78+
val.force_encoding(::Encoding::UTF_8)
79+
path_parameters[name.to_sym] = val
80+
end
81+
index += 1
82+
end
83+
84+
if r.path.anchored
85+
yield(r, path_parameters)
86+
else
87+
req.script_name = (script_name.to_s + match_data.to_s).chomp("/")
88+
req.path_info = match_data.post_match
89+
req.path_info = "/" + req.path_info unless req.path_info.start_with? "/"
90+
91+
yield(r, path_parameters)
92+
93+
req.script_name = script_name
94+
req.path_info = path_info
95+
end
96+
97+
req.path_parameters = req_params
98+
end
4499
end
45100

46101
def visualizer
@@ -74,65 +129,6 @@ def filter_routes(path)
74129
simulator.memos(path) { [] }
75130
end
76131

77-
def find_routes(req)
78-
req_params = req.path_parameters
79-
path_info = req.path_info
80-
script_name = req.script_name
81-
82-
routes = filter_routes(path_info)
83-
84-
custom_routes.each { |r|
85-
routes << r if r.path.match?(path_info)
86-
}
87-
88-
if req.head?
89-
routes = match_head_routes(routes, req)
90-
else
91-
routes.select! { |r| r.matches?(req) }
92-
end
93-
94-
if routes.size > 1
95-
routes.sort! do |a, b|
96-
a.precedence <=> b.precedence
97-
end
98-
end
99-
100-
routes.each do |r|
101-
match_data = r.path.match(path_info)
102-
103-
path_parameters = req_params.merge r.defaults
104-
105-
index = 1
106-
match_data.names.each do |name|
107-
if val = match_data[index]
108-
val = if val.include?("%")
109-
CGI.unescapeURIComponent(val)
110-
else
111-
val
112-
end
113-
val.force_encoding(::Encoding::UTF_8)
114-
path_parameters[name.to_sym] = val
115-
end
116-
index += 1
117-
end
118-
119-
if r.path.anchored
120-
yield(r, path_parameters)
121-
else
122-
req.script_name = (script_name.to_s + match_data.to_s).chomp("/")
123-
req.path_info = match_data.post_match
124-
req.path_info = "/" + req.path_info unless req.path_info.start_with? "/"
125-
126-
yield(r, path_parameters)
127-
128-
req.script_name = script_name
129-
req.path_info = path_info
130-
end
131-
132-
req.path_parameters = req_params
133-
end
134-
end
135-
136132
def match_head_routes(routes, req)
137133
head_routes = routes.select { |r| r.requires_matching_verb? && r.matches?(req) }
138134
return head_routes unless head_routes.empty?

0 commit comments

Comments
 (0)