Build Status

Adds rake tasks to detect extraneous routes and unreachable controller actions Ruby on Rails applications. It's also able to provide suggestions on how to change routes.rb to avoid defining dead routes and can generate an annotated version of a route file with suggestions for each line added as comments.

Installation

Add this line to your application's Gemfile:

gem 'rails_routes_analyzer'

And then execute:

$ bundle

Usage

rake routes:dead

Without any additional options this will scan all the routes and check for a matching action for each. For single routes created with methods such as get' orpost' it will tell you when that line could be removed.

For multi-route calls like resource' andresources' it can also let you know if and how to set the :except or :only parameter to better match the actions a controller actually provides. When suggesting :only/:except by default the one that provides a shorter list will be used.

For complex cases where for example a routes are created in a loop for multiple controllers a suggestion will be provided for each iteration but only if that specific iteration created a dead route. Every such suggestion will identify the exact controller to which it applies.

rake routes:annotate_dead [ANNOTATE=path/to/routes.rb]

Will output an annotated version of config/routes.rb or any other routes file as provided in the ANNOTATE parameter.

Additional options:

  • ONLY_ONLY=1 - suggestions for resource routes will only generate "only:" regardless of how many elements are listed.
  • ONLY_EXCEPT=1 - suggestions for resource routes will only generate "except:" regardless of how many elements are listed.
  • VERBOSE=1 - more verbosity, currently this means listing which non-existing actions a given call provides routes for.
rake routes:missing

Additional options:

Lists all action methods for all controllers which have no route pointing to them. Uses the (maybe not so well known) ActionController#Base.action_methods method which usually returns a list of all public methods of the controller class excluding any special Rails provided methods. To make the output of ActionController#Base.action_methods it would be ideal to try to make all application-provided controller methods non-public if they are not meant to be callable as an action. Alternatively it's also possible (but less desirable) to override the action_methods call in any controller class to explicitly remove mis-characterised methods.

  • STRICT=1 - causes controller base class provided public methods to be considered as actions for a subclass controller and thus reported as errors if they lack routes. Enabling this can generate a lot of noise for applications that have public non-actions in a controller base class.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/bear-metal/rails_routes_analyzer.

License

The gem is available as open source under the terms of the MIT License.

Copyright (c) 2016 Bear Metal