Skip to content

Instantly share code, notes, and snippets.

@rahim
Created June 14, 2021 12:58
Show Gist options
  • Save rahim/8c21d3e84cf24fec041609b430099bc3 to your computer and use it in GitHub Desktop.
Save rahim/8c21d3e84cf24fec041609b430099bc3 to your computer and use it in GitHub Desktop.
Dump all routable Rails transactions
#! /usr/bin/env ruby
# This dumps all known transactions (including engines) in standard controller#action format to STDOUT
#
# Built against private Rails 5.2.x APIs so success with other Rails versions is likely to be hit and miss.
require_relative "../config/environment"
class CustomInspector < ActionDispatch::Routing::RoutesInspector
def format(formatter, filter = nil)
routes_to_display = filter_routes(normalize_filter(filter))
routes = collect_routes(routes_to_display)
if routes.none?
formatter.no_routes(collect_routes(@routes))
return formatter.result
end
formatter.header routes
formatter.section routes
@engines.each do |name, engine_routes|
formatter.section engine_routes
end
formatter.result.split("\n").map { |s| s.split.first }.sort.uniq.reject { |s| s.match(/Engine/) }
end
end
class CustomFormatter < ActionDispatch::Routing::ConsoleFormatter
def draw_section(routes)
routes.map { |r| r[:reqs] }
end
end
inspector = CustomInspector.new(Rails.application.routes.routes)
puts inspector.format(CustomFormatter.new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment