提交 1a58ac60 编写于 作者: A Andrew White

Refactor to reduce number of loops

Only build the missing_keys array once we have detected that there
actually are missing keys by moving the check to be part of the block
that performs the path substitution.
上级 74722d66
...@@ -184,27 +184,18 @@ def call(t, args) ...@@ -184,27 +184,18 @@ def call(t, args)
def optimized_helper(args) def optimized_helper(args)
path = @string_route.dup path = @string_route.dup
klass = Journey::Router::Utils klass = Journey::Router::Utils
parameterized_args = args.map(&:to_param)
missing_keys = []
parameterized_args.each_with_index do |arg, index| @path_parts.zip(args) do |part, arg|
if arg.nil? || arg.empty? parameterized_arg = arg.to_param
missing_keys << @path_parts[index]
end
end
unless missing_keys.empty? if parameterized_arg.nil? || parameterized_arg.empty?
message = "No route matches #{Hash[@path_parts.zip(args)].inspect}" raise_generation_error(args)
message << " missing required keys: #{missing_keys.inspect}" end
raise ActionController::UrlGenerationError, message
end
@path_parts.zip(parameterized_args) do |part, arg|
# Replace each route parameter # Replace each route parameter
# e.g. :id for regular parameter or *path for globbing # e.g. :id for regular parameter or *path for globbing
# with ruby string interpolation code # with ruby string interpolation code
path.gsub!(/(\*|:)#{part}/, klass.escape_fragment(arg)) path.gsub!(/(\*|:)#{part}/, klass.escape_fragment(parameterized_arg))
end end
path path
end end
...@@ -212,6 +203,25 @@ def optimized_helper(args) ...@@ -212,6 +203,25 @@ def optimized_helper(args)
def optimize_routes_generation?(t) def optimize_routes_generation?(t)
t.send(:optimize_routes_generation?) t.send(:optimize_routes_generation?)
end end
def raise_generation_error(args)
parts, missing_keys = [], []
@path_parts.zip(args) do |part, arg|
parameterized_arg = arg.to_param
if parameterized_arg.nil? || parameterized_arg.empty?
missing_keys << part
end
parts << [part, arg]
end
message = "No route matches #{Hash[parts].inspect}"
message << " missing required keys: #{missing_keys.inspect}"
raise ActionController::UrlGenerationError, message
end
end end
def initialize(route, options) def initialize(route, options)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册