diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2015-08-14 16:13:26 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2015-08-14 16:13:26 -0700 |
commit | 6c48d9392fe964640fe5721fcd27bb170613cc27 (patch) | |
tree | b3e2a8b36ecd6f2e0a37a4a01beca13d8214ed97 /actionpack | |
parent | 5ba6966766e67af4ae0028c4429acbd280a100a2 (diff) | |
download | rails-6c48d9392fe964640fe5721fcd27bb170613cc27.tar.gz rails-6c48d9392fe964640fe5721fcd27bb170613cc27.tar.bz2 rails-6c48d9392fe964640fe5721fcd27bb170613cc27.zip |
pass pass the mapping object down the add_route stack
then we can let the mapping object derive stuff that the Route object
needs.
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_dispatch/journey/routes.rb | 14 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/routing/mapper.rb | 65 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/routing/route_set.rb | 48 | ||||
-rw-r--r-- | actionpack/test/dispatch/mapper_test.rb | 2 |
4 files changed, 73 insertions, 56 deletions
diff --git a/actionpack/lib/action_dispatch/journey/routes.rb b/actionpack/lib/action_dispatch/journey/routes.rb index 5990964b57..eba43c3c36 100644 --- a/actionpack/lib/action_dispatch/journey/routes.rb +++ b/actionpack/lib/action_dispatch/journey/routes.rb @@ -62,9 +62,19 @@ module ActionDispatch end end - # Add a route to the routing table. + MyMapping = Struct.new(:application, :path, :conditions, :required_defaults, :defaults) + def add_route(app, path, conditions, required_defaults, defaults, name = nil) - route = Route.new(name, app, path, conditions, required_defaults, defaults) + add_route2(name, MyMapping.new(app, path, conditions, required_defaults, defaults)) + end + + def add_route2(name, mapping) + route = Route.new(name, + mapping.application, + mapping.path, + mapping.conditions, + mapping.required_defaults, + mapping.defaults) route.precedence = routes.length routes << route diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 70adb42df5..d80faf7423 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -59,17 +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 + 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) @@ -100,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) @@ -147,6 +149,57 @@ module ActionDispatch 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 @@ -1619,7 +1672,7 @@ 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) + mapping = Mapping.build(@scope, @set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, options) @set.add_route(mapping, ast, as, anchor) end diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index df3b2bbc25..23dd865ed0 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -525,57 +525,11 @@ module ActionDispatch "http://guides.rubyonrails.org/routing.html#restricting-the-routes-created" end - required_defaults = mapping.required_defaults - path = build_path(path_ast, mapping.requirements, anchor) - conditions = build_conditions(mapping.conditions) - - route = @set.add_route(mapping.application, path, conditions, required_defaults, mapping.defaults, name) + route = @set.add_route2(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 diff --git a/actionpack/test/dispatch/mapper_test.rb b/actionpack/test/dispatch/mapper_test.rb index 8c1b53041a..eca4ca8e0e 100644 --- a/actionpack/test/dispatch/mapper_test.rb +++ b/actionpack/test/dispatch/mapper_test.rb @@ -89,7 +89,7 @@ module ActionDispatch options = { } scope = Mapper::Scope.new({}) ast = Journey::Parser.parse '/store/:name(*rest)' - m = Mapper::Mapping.build(scope, FakeSet.new, ast, 'foo', 'bar', nil, [:get], nil, {}, options) + m = Mapper::Mapping.build(scope, FakeSet.new, ast, 'foo', 'bar', nil, [:get], nil, {}, true, options) assert_equal(/.+?/, m.requirements[:rest]) end |