Last active
March 7, 2024 11:10
-
-
Save Georgy5/ae2a2c5434a9dfb899eadb5816a6da8c to your computer and use it in GitHub Desktop.
Find unused Rails routes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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