Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit 28cd098d99c52486aecb72aab39105d8abcd52ad 1 parent 63e14a7
@drogus drogus authored
View
2  actionpack/lib/action_dispatch/routing/mapper.rb
@@ -16,7 +16,7 @@ def self.new(app, constraints, request = Rack::Request)
end
end
- attr_reader :app
+ attr_reader :app, :constraints
def initialize(app, constraints, request)
@app, @constraints, @request = app, constraints, request
View
11 railties/lib/rails/application/route_inspector.rb
@@ -23,7 +23,7 @@ def collect_routes(routes)
routes = routes.collect do |route|
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'
action = route_reqs[:action] || ':action'
@@ -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]}"
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
View
17 railties/test/application/route_inspect_test.rb
@@ -127,5 +127,22 @@ def test_rake_routes_shows_route_with_rack_app
output = @inspector.format @set.routes
assert_equal [" /foo/:id(.:format) #{RackApp.name} {:id=>/[A-Z]\\d{5}/}"], output
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
Please sign in to comment.
Something went wrong with that request. Please try again.