Skip to content

Commit

Permalink
reuse the ast we already made
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed May 29, 2014
1 parent 3a102a5 commit ffbe1b1
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ def initialize(set, scope, path, options)
@default_action = options[:action] || scope[:action] @default_action = options[:action] || scope[:action]


@path = normalize_path! @path, options[:format] @path = normalize_path! @path, options[:format]
path_params = path_params @path ast = path_ast @path
@options = normalize_options!(options, path_params) path_params = path_params ast
@options = normalize_options!(options, path_params, ast)
normalize_requirements!(path_params) normalize_requirements!(path_params)
normalize_conditions!(path_params) normalize_conditions!(path_params)
normalize_defaults! normalize_defaults!
Expand Down Expand Up @@ -107,13 +108,13 @@ def optional_format?(path, format)
format != false && !path.include?(':format') && !path.end_with?('/') format != false && !path.include?(':format') && !path.end_with?('/')
end end


def normalize_options!(options, path_params) def normalize_options!(options, path_params, path_ast)
wildcards = path_ast(path).grep(Journey::Nodes::Star).map(&:name)

# Add a constraint for wildcard route to make it non-greedy and match the # Add a constraint for wildcard route to make it non-greedy and match the
# optional format part of the route by default # optional format part of the route by default
if wildcards.any? && options[:format] != false if options[:format] != false
wildcards.each { |wc| options[wc.to_sym] ||= /.+?/ } path_ast.grep(Journey::Nodes::Star) do |node|
options[node.name.to_sym] ||= /.+?/
end
end end


if path_params.include?(:controller) if path_params.include?(:controller)
Expand Down Expand Up @@ -316,8 +317,8 @@ def constraints
end end
end end


def path_params(path) def path_params(ast)
path_ast(path).grep(Journey::Nodes::Symbol).map { |n| n.name.to_sym } ast.grep(Journey::Nodes::Symbol).map { |n| n.name.to_sym }
end end


def path_ast(path) def path_ast(path)
Expand Down

0 comments on commit ffbe1b1

Please sign in to comment.