Get unused routes of large Rails App š
After a few years of continuous development, your rails application becomes larger and itās good practice to do some cleanup. One of the most obvious cleanups is cleaning up unused routes.
As your rails application grows, at the time of adding a route for new action, developers sometimes add resources :objects
in config/routes.rb
file rather than adding a single member or collection route which creates unnecessary routes at runtime š
For example,
Writing above code will create the following routes -
But, it might be possible that we are not using all of the above methods, or we need only users#new
, users#create
and users#show
methods in our Users controller like -
So, itās the most obvious process to inspect your routes.rb
file from time to time and clean it if necessary.
Inspecting each route and its corresponding action took so much time and itās next to impossible thing when the application is so large š
Now, there are few ruby gems available in the market which can do it for you, but again adding additional gem requires extra load in your gemfile of your large application.
Another solution is to make a simple script which compares routes and associated actions available in controllers and list down all possible unused routes of our large application so that we can clean routes.rb
file and remove routes that are not needed.
Have a look at following a small ruby script -
For getting unused routes for your rails app, all you need to do is to copy above code in your rails console
or you can paste above code in single file get-unused-routes.rb
in the root directory of your Rails application and run it using the following command -
$> ruby get-unused-routes.rb
Using above small script weāre able to get all unused routes of our large rails application, we donāt use any Gem/Plugin to achieve this š„³
References -