Skip to content

Commit

Permalink
Correctly display rack apps with dynamic constraints in RoutesInspector
Browse files Browse the repository at this point in the history
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
drogus committed Dec 26, 2011
1 parent 4cf40d0 commit d7cfb63
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/routing/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion railties/lib/rails/application/route_inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
17 changes: 17 additions & 0 deletions railties/test/application/route_inspect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d7cfb63

Please sign in to comment.