提交 28cd098d 编写于 作者: P Piotr Sarnacki

Correctly display rack apps with dynamic constraints in RoutesInspector

If you used dynamic constraint like that:

  scope :constraint => MyConstraint.new do
    mount RackApp => "/foo"
  end

routes were not displayed correctly when using `rake routes`.
This commit fixes it. If you want nice display of dynamic
constraints in `rake routes` output, please just override
to_s method in your constraint's class.
上级 63e14a79
...@@ -16,7 +16,7 @@ def self.new(app, constraints, request = Rack::Request) ...@@ -16,7 +16,7 @@ def self.new(app, constraints, request = Rack::Request)
end end
end end
attr_reader :app attr_reader :app, :constraints
def initialize(app, constraints, request) def initialize(app, constraints, request)
@app, @constraints, @request = app, constraints, request @app, @constraints, @request = app, constraints, request
......
...@@ -23,7 +23,7 @@ def collect_routes(routes) ...@@ -23,7 +23,7 @@ def collect_routes(routes)
routes = routes.collect do |route| routes = routes.collect do |route|
route_reqs = route.requirements route_reqs = route.requirements
rack_app = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/ rack_app = discover_rack_app(route.app)
controller = route_reqs[:controller] || ':controller' controller = route_reqs[:controller] || ':controller'
action = route_reqs[:action] || ':action' action = route_reqs[:action] || ':action'
...@@ -70,6 +70,15 @@ def formatted_routes(routes) ...@@ -70,6 +70,15 @@ def formatted_routes(routes)
"#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}" "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}"
end end
end end
def discover_rack_app(app)
class_name = app.class.name.to_s
if class_name == "ActionDispatch::Routing::Mapper::Constraints"
discover_rack_app(app.app)
elsif class_name !~ /^ActionDispatch::Routing/
app
end
end
end end
end end
end end
...@@ -127,5 +127,22 @@ def test_rake_routes_shows_route_with_rack_app ...@@ -127,5 +127,22 @@ def test_rake_routes_shows_route_with_rack_app
output = @inspector.format @set.routes output = @inspector.format @set.routes
assert_equal [" /foo/:id(.:format) #{RackApp.name} {:id=>/[A-Z]\\d{5}/}"], output assert_equal [" /foo/:id(.:format) #{RackApp.name} {:id=>/[A-Z]\\d{5}/}"], output
end end
def test_rake_routes_shows_route_with_rack_app_nested_with_dynamic_constraints
constraint = Class.new do
def to_s
"( my custom constraint )"
end
end
@set.draw do
scope :constraint => constraint.new do
mount RackApp => '/foo'
end
end
output = @inspector.format @set.routes
assert_equal [" /foo #{RackApp.name} {:constraint=>( my custom constraint )}"], output
end
end end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册