Image from unsplash.com

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,

# config/routes.rb
Rails.application.routes.draw do
  resources :users
end

Writing above code will create the following routes -

HTTP Verb Path Controller#Action Used for
GET /users users#index Display a list of all users
GET /users/new users#new Return an HTML form for creating a new user
POST /users users#create Create a new user
GET /users/:id users#show Display a specific user
GET /users/:id/edit users#edit Return an HTML form for editing a user
PATCH/PUT /users/:id users#update Update a specific user
DELETE /users/:id users#destroy Delete a specific user

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 -

class UsersController < ApplicationController
  def new
  end

  def create
  end

  def show
  end
end

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 -

Rails.application.eager_load!
unused_routes = {}

# Iterating over all non-empty routes from RouteSet
Rails.application.routes.routes.map(&:requirements).reject(&:empty?).each do |route|
  name = route[:controller].camelcase
  next if name.start_with?("Rails")

  controller = "#{name}Controller"

  if Object.const_defined?(controller) && !controller.constantize.new.respond_to?(route[:action])
    # Get route for which associated action is not present and add it in final results
    unless Dir.glob(Rails.root.join("app", "views", name.downcase, "#{route[:action]}.*")).any?
      unused_routes[controller] = [] if unused_routes[controller].nil?
      unused_routes[controller] << route[:action]
    end
  end
end

puts unused_routes
# {"HomeController"=>["edit", "update", "update", "destroy"]}

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 -