aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2015-08-14 14:41:48 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2015-08-14 14:41:48 -0700
commit5ba6966766e67af4ae0028c4429acbd280a100a2 (patch)
tree2d7234541441879bc441a6cc046c1498ab20a203
parent68dd5abf1459e22ca64295ca3b4d6bcf2a525849 (diff)
downloadrails-5ba6966766e67af4ae0028c4429acbd280a100a2.tar.gz
rails-5ba6966766e67af4ae0028c4429acbd280a100a2.tar.bz2
rails-5ba6966766e67af4ae0028c4429acbd280a100a2.zip
pass the mapping object to build_route
now that we aren't doing options manipulations, we can just pass the mapping object down and read values from it.
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb10
-rw-r--r--actionpack/lib/action_dispatch/routing/route_set.rb10
-rw-r--r--actionpack/test/dispatch/mapper_test.rb21
3 files changed, 15 insertions, 26 deletions
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index 093c5a1b04..70adb42df5 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -61,6 +61,7 @@ module ActionDispatch
attr_reader :requirements, :conditions, :defaults
attr_reader :to, :default_controller, :default_action
+ attr_reader :required_defaults
def self.build(scope, set, ast, controller, default_action, to, via, formatted, options_constraints, options)
options = scope[:options].merge(options) if scope[:options]
@@ -136,14 +137,14 @@ 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
private
@@ -1619,8 +1620,7 @@ to this:
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)
+ @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..df3b2bbc25 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,11 +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)
+ required_defaults = mapping.required_defaults
+ path = build_path(path_ast, mapping.requirements, anchor)
+ conditions = build_conditions(mapping.conditions)
- route = @set.add_route(app, path, conditions, required_defaults, defaults, name)
+ route = @set.add_route(mapping.application, path, conditions, required_defaults, mapping.defaults, name)
named_routes[name] = route if name
route
end
diff --git a/actionpack/test/dispatch/mapper_test.rb b/actionpack/test/dispatch/mapper_test.rb
index 289fb69b8b..8c1b53041a 100644
--- a/actionpack/test/dispatch/mapper_test.rb
+++ b/actionpack/test/dispatch/mapper_test.rb
@@ -4,20 +4,10 @@ module ActionDispatch
module Routing
class MapperTest < ActiveSupport::TestCase
class FakeSet < ActionDispatch::Routing::RouteSet
- def initialize
- @my_routes = []
- super
- end
-
def resources_path_names
{}
end
- def add_route(*args)
- @my_routes << args
- super
- end
-
def request_class
ActionDispatch::Request
end
@@ -27,11 +17,11 @@ module ActionDispatch
end
def defaults
- @my_routes.map { |x| x[4] }
+ routes.map(&:defaults)
end
def conditions
- @my_routes.map { |x| x[1] }
+ routes.map(&:constraints)
end
def requirements
@@ -92,7 +82,7 @@ module ActionDispatch
end
assert_equal({:omg=>:awesome, :controller=>"posts", :action=>"index"},
fakeset.defaults.first)
- assert_equal ["GET"], fakeset.conditions.first[:request_method]
+ assert_equal(/^GET$/, fakeset.conditions.first[:request_method])
end
def test_mapping_requirements
@@ -100,8 +90,7 @@ module ActionDispatch
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)
- _, _, requirements, _ = m.to_route
- assert_equal(/.+?/, requirements[:rest])
+ assert_equal(/.+?/, m.requirements[:rest])
end
def test_via_scope
@@ -110,7 +99,7 @@ module ActionDispatch
mapper.scope(via: :put) do
mapper.match '/', :to => 'posts#index', :as => :main
end
- assert_equal ["PUT"], fakeset.conditions.first[:request_method]
+ assert_equal(/^PUT$/, fakeset.conditions.first[:request_method])
end
def test_map_slash