Skip to content

Instantly share code, notes, and snippets.

@natematykiewicz
Last active June 9, 2022 19:47
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save natematykiewicz/521bb5c9ec1f02c69a282bee21af1031 to your computer and use it in GitHub Desktop.
Save natematykiewicz/521bb5c9ec1f02c69a282bee21af1031 to your computer and use it in GitHub Desktop.
Find routes that will raise a routing error when requested
desc 'Find routes that will raise a routing error when requested'
task unroutable_routes: :environment do
# A lot of this code was taken from how `rake routes` works
# https://github.com/rails/rails/blob/f95c0b7e96eb36bc3efc0c5beffbb9e84ea664e4/railties/lib/rails/commands/routes/routes_command.rb
require 'action_dispatch/routing/inspector'
unroutables = Rails.application.routes.routes.
map { |r| ActionDispatch::Routing::RouteWrapper.new(r) }.
reject { |r| r.internal? || r.engine? || r.path.starts_with?('/rails/') || !r.controller }.
reject do |r|
controller = "#{r.controller}_controller".classify.safe_constantize
controller && (
controller.method_defined?(r.action) ||
Rails.root.join('app/views', r.controller).glob("#{r.action}.*").any?
)
end.
map { |r| r.__getobj__ }
if unroutables.present?
inspector = ActionDispatch::Routing::RoutesInspector.new(unroutables)
formatter = if ENV['EXPANDED']
ActionDispatch::Routing::ConsoleFormatter::Expanded.new
else
ActionDispatch::Routing::ConsoleFormatter::Sheet.new
end
puts inspector.format(formatter)
end
puts "#{unroutables.size} unroutable routes"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment