Skip to content

Instantly share code, notes, and snippets.

@Georgy5
Last active March 7, 2024 11:10
Show Gist options
  • Save Georgy5/ae2a2c5434a9dfb899eadb5816a6da8c to your computer and use it in GitHub Desktop.
Save Georgy5/ae2a2c5434a9dfb899eadb5816a6da8c to your computer and use it in GitHub Desktop.
Find unused Rails routes
require_relative "config/environment"
# Note: As of Rails 7.1+ we can just use:
# `rails routes --unused`
Rails.application.routes.routes.map(&:requirements).each do |route|
next if route.blank?
next if route[:internal]
controller_name = "#{route[:controller].camelcase}Controller"
next if controller_name.constantize.new.respond_to?(route[:action])
implicit_render_view = Rails.root.join("app", "views", *route[:controller].split('::'), "#{route[:action]}.*")
next if Dir.glob(implicit_render_view).any?
puts "#{controller_name}##{route[:action]}"
rescue NameError, LoadError
puts "#{controller_name}##{route[:action]} - controller not found"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment