diff options
Diffstat (limited to 'actionpack/lib/action_dispatch/routing')
-rw-r--r-- | actionpack/lib/action_dispatch/routing/mapper.rb | 81 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/routing/route_set.rb | 50 |
2 files changed, 67 insertions, 64 deletions
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 4411625e51..d80faf7423 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -59,16 +59,17 @@ module ActionDispatch class Mapping #:nodoc: ANCHOR_CHARACTERS_REGEX = %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z} - attr_reader :requirements, :conditions, :defaults + attr_reader :requirements, :defaults attr_reader :to, :default_controller, :default_action + attr_reader :required_defaults, :ast - def self.build(scope, set, ast, controller, default_action, to, via, formatted, options_constraints, options) + def self.build(scope, set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, options) options = scope[:options].merge(options) if scope[:options] defaults = (scope[:defaults] || {}).dup scope_constraints = scope[:constraints] || {} - new set, ast, defaults, controller, default_action, scope[:module], to, formatted, scope_constraints, scope[:blocks] || [], via, options_constraints, options + new set, ast, defaults, controller, default_action, scope[:module], to, formatted, scope_constraints, scope[:blocks] || [], via, options_constraints, anchor, options end def self.check_via(via) @@ -99,13 +100,15 @@ module ActionDispatch format != false && !path.include?(':format') && !path.end_with?('/') end - def initialize(set, ast, defaults, controller, default_action, modyoule, to, formatted, scope_constraints, blocks, via, options_constraints, options) + def initialize(set, ast, defaults, controller, default_action, modyoule, to, formatted, scope_constraints, blocks, via, options_constraints, anchor, options) @defaults = defaults @set = set @to = to @default_controller = controller @default_action = default_action + @ast = ast + @anchor = anchor path_params = ast.find_all(&:symbol?).map(&:to_sym) @@ -136,16 +139,67 @@ module ActionDispatch @conditions = Hash[conditions] @defaults = formats[:defaults].merge(@defaults).merge(normalize_defaults(options)) - @conditions[:required_defaults] = (split_options[:required_defaults] || []).map(&:first) + @required_defaults = (split_options[:required_defaults] || []).map(&:first) unless via == [:all] @conditions[:request_method] = via.map { |m| m.to_s.dasherize.upcase } end end - def to_route - [ app(@blocks), conditions, requirements, defaults ] + def application + app(@blocks) end + def path + build_path @ast, requirements, @anchor + end + + def conditions + build_conditions @conditions, @set.request_class + end + + def build_conditions(current_conditions, request_class) + conditions = current_conditions.dup + + # Rack-Mount requires that :request_method be a regular expression. + # :request_method represents the HTTP verb that matches this route. + # + # Here we munge values before they get sent on to rack-mount. + verbs = conditions[:request_method] || [] + unless verbs.empty? + conditions[:request_method] = %r[^#{verbs.join('|')}$] + end + + conditions.keep_if do |k, _| + request_class.public_method_defined?(k) + end + end + private :build_conditions + + def build_path(ast, requirements, anchor) + pattern = Journey::Path::Pattern.new(ast, requirements, SEPARATORS, anchor) + + builder = Journey::GTG::Builder.new ast + + # Get all the symbol nodes followed by literals that are not the + # dummy node. + symbols = ast.grep(Journey::Nodes::Symbol).find_all { |n| + builder.followpos(n).first.literal? + } + + # Get all the symbol nodes preceded by literals. + symbols.concat ast.find_all(&:literal?).map { |n| + builder.followpos(n).first + }.find_all(&:symbol?) + + symbols.each { |x| + x.regexp = /(?:#{Regexp.union(x.regexp, '-')})+/ + } + + pattern + end + private :build_path + + private def add_wildcard_options(options, formatted, path_ast) # Add a constraint for wildcard route to make it non-greedy and match the @@ -1553,7 +1607,8 @@ to this: route_options[:as] = _path _path = option_path end - process_path(route_options, controller, _path, _path, to, via, formatted, anchor, options_constraints) + to = get_to_from_path(_path, to, route_options[:action]) + decomposed_match(_path, controller, route_options, _path, to, via, formatted, anchor, options_constraints) end path_types.fetch(Symbol, []).each do |action| @@ -1575,11 +1630,6 @@ to this: end end - def process_path(options, controller, path, option_path, to, via, formatted, ancho, options_constraintsr) - to = get_to_from_path(path, to, options[:action]) - decomposed_match(path, controller, options, option_path, to, via, formatted, ancho, options_constraintsr) - end - def using_match_shorthand?(path) path =~ %r{^/?[-\w]+/[-\w/]+$} end @@ -1622,9 +1672,8 @@ to this: path = Mapping.normalize_path URI.parser.escape(path), formatted ast = Journey::Parser.parse path - mapping = Mapping.build(@scope, @set, ast, controller, default_action, to, via, formatted, options_constraints, options) - app, conditions, requirements, defaults = mapping.to_route - @set.add_route(app, conditions, ast, requirements, defaults, as, anchor) + mapping = Mapping.build(@scope, @set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, options) + @set.add_route(mapping, ast, as, anchor) end def root(path, options={}) diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 04d8013768..4f698c84ab 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -514,7 +514,7 @@ module ActionDispatch routes.empty? end - def add_route(app, conditions, path_ast, requirements, defaults, name, anchor) + def add_route(mapping, path_ast, name, anchor) raise ArgumentError, "Invalid route name: '#{name}'" unless name.blank? || name.to_s.match(/^[_a-z]\w*$/i) if name && named_routes[name] @@ -525,57 +525,11 @@ module ActionDispatch "http://guides.rubyonrails.org/routing.html#restricting-the-routes-created" end - required_defaults = conditions.delete :required_defaults - path = build_path(path_ast, requirements, anchor) - conditions = build_conditions(conditions) - - route = @set.add_route(app, path, conditions, required_defaults, defaults, name) + route = @set.add_route(name, mapping) named_routes[name] = route if name route end - def build_path(ast, requirements, anchor) - pattern = Journey::Path::Pattern.new(ast, requirements, SEPARATORS, anchor) - - builder = Journey::GTG::Builder.new ast - - # Get all the symbol nodes followed by literals that are not the - # dummy node. - symbols = ast.grep(Journey::Nodes::Symbol).find_all { |n| - builder.followpos(n).first.literal? - } - - # Get all the symbol nodes preceded by literals. - symbols.concat ast.find_all(&:literal?).map { |n| - builder.followpos(n).first - }.find_all(&:symbol?) - - symbols.each { |x| - x.regexp = /(?:#{Regexp.union(x.regexp, '-')})+/ - } - - pattern - end - private :build_path - - def build_conditions(current_conditions) - conditions = current_conditions.dup - - # Rack-Mount requires that :request_method be a regular expression. - # :request_method represents the HTTP verb that matches this route. - # - # Here we munge values before they get sent on to rack-mount. - verbs = conditions[:request_method] || [] - unless verbs.empty? - conditions[:request_method] = %r[^#{verbs.join('|')}$] - end - - conditions.keep_if do |k, _| - request_class.public_method_defined?(k) - end - end - private :build_conditions - class Generator PARAMETERIZE = lambda do |name, value| if name == :controller |