Home > Software design >  Rails: find routes/methods which no longer match any controller
Rails: find routes/methods which no longer match any controller

Time:05-29

I have a massive monolithic Rails repo with a couple thousand routes. Over time, developers have either deleted a route which maps to an existing controller or have deleted a controller which still has a route mapped to it

Is there a smart way for me to systematically find these anomalies within my rails app to clean it up?

NOTE: Please assume that these mismatches do not present a user-facing issue but is merely for maintenance purposes to trim the number of bad routes

CodePudding user response:

There is no such command that will find you:

But here is a thing you can do:

Run command:

rails routes > routes.rb 

It will write all your route in an text file and they you can match the manually if you have the time.
But there is no harm if you have extra routes present in Rails application its common in most of the Rails applications.

CodePudding user response:

I found this gem which aims to solve my question somewhat

https://github.com/amatsuda/traceroute

CodePudding user response:

The traceroute gem looks very nice, but just in case it doesn't work as you expect, you could do this in rails console.

Get an array of all your controllers with their actions:

ApplicationController.descendants.map {|c| [c, c.action_methods]}

Get an array of all routes:

Rails.application.routes.routes.map &:defaults

Then you could iterate over the two arrays, selecting actions that appear in one array but not in the other one.

Before starting the console set config.eager_load = true in config/environments.development.rb. Without this you won't see all of your controllers.

  • Related