Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save the-spectator/33dfa35dd1cea6b40528bed9b1e693bf to your computer and use it in GitHub Desktop.
Save the-spectator/33dfa35dd1cea6b40528bed9b1e693bf 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