Skip to content

Instantly share code, notes, and snippets.

@IanVaughan
Created August 27, 2020 11:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanVaughan/dce87f99927a3c703a34b70930a3171d to your computer and use it in GitHub Desktop.
Save IanVaughan/dce87f99927a3c703a34b70930a3171d to your computer and use it in GitHub Desktop.
Find unused rails routes
# frozen_string_literal: true
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"
next unless 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
puts unused_routes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment