From f3a420467fc81fcee59d53a745f1ca23de5a450c Mon Sep 17 00:00:00 2001 From: Paul Robert Lloyd Date: Tue, 15 Feb 2022 16:34:44 +0000 Subject: [PATCH] Rake task to export routes as CSV --- lib/tasks/routes_export.rake | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lib/tasks/routes_export.rake diff --git a/lib/tasks/routes_export.rake b/lib/tasks/routes_export.rake new file mode 100644 index 000000000..fb5f4a4c5 --- /dev/null +++ b/lib/tasks/routes_export.rake @@ -0,0 +1,35 @@ +namespace :route_formatter do + desc "Export routes as CSV" + task csv: :environment do |t| + class CSVFormatter + def initialize + @buffer= [] + end + + def result + @buffer.join("\n") + end + + def section_title(title) + end + + def section(routes) + routes.each do |r| + @buffer << [r[:name], r[:verb], r[:path], r[:reqs]].join(",") + end + end + + def header(routes) + @buffer << %w"Prefix Verb URI_Pattern Controller#Action".join(",") + end + + def no_routes + @buffer << "" + end + end + require "action_dispatch/routing/inspector" + all_routes = Rails.application.routes.routes + inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes) + puts inspector.format(CSVFormatter.new) + end +end