Skip to content

Instantly share code, notes, and snippets.

@DevGW
Last active April 5, 2023 14:45
Show Gist options
  • Save DevGW/2b1f6cb5b37c44c945300c340714ec9a to your computer and use it in GitHub Desktop.
Save DevGW/2b1f6cb5b37c44c945300c340714ec9a to your computer and use it in GitHub Desktop.
Ruby / Rails :: route_formatter.rake #rails
namespace :route_formatter do
desc "get route as csv format"
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, ENV['CONTROLLER'])
end
end
# bin/rails route_formatter:csv
# If you want to put it on the Clipboard on Mac,
# bin/rails route_formatter:csv | pbcopy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment