Skip to content

Instantly share code, notes, and snippets.

@katoy
Last active April 6, 2020 05:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katoy/f06965acc820b6c76b88 to your computer and use it in GitHub Desktop.
Save katoy/f06965acc820b6c76b88 to your computer and use it in GitHub Desktop.
Rails の routes 情報を csv 書式で出力する。 (rake routes:csv)
$ rake -T routes
rake routes # Print out all defined routes in match order, with names
rake routes:csv # Print out all defined routes in CSV format
$ rake routes:csv
Prefix, Verb, URI Pattern, Controller#Action
data, GET, /data(.:format), data#index
, POST, /data(.:format), data#create
new_datum, GET, /data/new(.:format), data#new
edit_datum, GET, /data/:id/edit(.:format), data#edit
datum, GET, /data/:id(.:format), data#show
, PATCH, /data/:id(.:format), data#update
, PUT, /data/:id(.:format), data#update
, DELETE, /data/:id(.:format), data#destroy
# See gems/actionpack-4.2.1/lib/action_dispatch/routing/inspector.rb
namespace :routes do
desc 'Print out all defined routes in CSV format.'
task :csv => :environment do
class CSVFormatter
def initialize
@buffer = []
end
def result
@buffer.join("\n")
end
def section_title(title)
@buffer << "\n#{title}:"
end
def section(routes)
routes.map do |r|
@buffer << "#{r[:name]},#{r[:verb]},#{r[:path]},#{r[:reqs]}"
end
end
def header(routes)
@buffer << 'Prefix,Verb,URI Pattern,Controller#Action'
end
def no_routes
@buffer << <<-MESSAGE.strip_heredoc
You don't have any routes defined!
Please add some routes in config/routes.rb.
For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html.
MESSAGE
end
end
all_routes = Rails.application.routes.routes
require 'action_dispatch/routing/inspector'
inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)
# puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, ENV['CONTROLLER'])
puts inspector.format(CSVFormatter.new, ENV['CONTROLLER'])
end
end
# See gems/actionpack-4.2.1/lib/action_dispatch/routing/inspector.rb
require 'csv'
namespace :routes do
desc 'Print out all defined routes in CSV format.'
task :csv => :environment do
class CSVFormatter
def initialize
@buffer = []
end
def result
CSV.generate do |csv|
@buffer.each do |record|
csv << record
end
end
end
def section_title(title)
@buffer << ["#{title}"]
end
def section(routes)
routes.map do |r|
z = r[:reqs].split('#')
controller = ''
method = ''
if z.size > 0
controller = z[0]
method = z[1]
else
end
# @buffer << ["#{r[:name]}", "#{r[:verb]}", "#{r[:path]}", "#{r[:reqs]}"]
@buffer << ["#{r[:name]}", "#{r[:verb]}", "#{r[:path]}", "#{controller}", "#{method}"]
end
end
def header(routes)
@buffer << ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action']
end
def no_routes
str = <<-MESSAGE.strip_heredoc
You don't have any routes defined!
Please add some routes in config/routes.rb.
For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html.
MESSAGE
@buffer << [str]
end
end
all_routes = Rails.application.routes.routes
require 'action_dispatch/routing/inspector'
inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)
# puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, ENV['CONTROLLER'])
puts inspector.format(CSVFormatter.new, ENV['CONTROLLER'])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment