Created
August 7, 2018 06:31
-
-
Save lissdy/e8266f5800e4f965f2dfab30da3ced7c to your computer and use it in GitHub Desktop.
Export rails routes to csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]}" | |
@buffer << "#{r[:verb]} #{r[:path]}" | |
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 |
Really, very useful! Thank you!
Hey, do you know why I get the error NoMethodError: undefined method '[]' for nil:NilClass
?
Here is the full trace:
NoMethodError: undefined method '[]' for nil:NilClass
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actionpack-6.0.3.2/lib/action_dispatch/routing/inspector.rb:85:in 'normalize_filter'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actionpack-6.0.3.2/lib/action_dispatch/routing/inspector.rb:65:in 'format'C:/Users/Gabriel/Documents/Edukado/edukado-web/lib/tasks/routes_csv.rake:43:in 'block (2 levels) in <top (required)>'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/task.rb:281:in 'block in execute'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/task.rb:281:in 'each'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/task.rb:281:in 'execute'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/task.rb:219:in 'block in invoke_with_call_chain'
C:/Ruby26-x64/lib/ruby/2.6.0/monitor.rb:235:in 'mon_synchronize'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/task.rb:199:in 'invoke_with_call_chain'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/task.rb:188:in 'invoke'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:160:in 'invoke_task'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:116:in 'block (2 levels) in top_level'C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:116:in 'each'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:116:in 'block in top_level'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:125:in 'run_with_threads'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:110:in 'top_level'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:83:in 'block in run'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:186:in 'standard_exception_handling'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:80:in 'run'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/exe/rake:27:in '<top (required)>'
C:/Ruby26-x64/bin/rake:23:in 'load'
C:/Ruby26-x64/bin/rake:23:in '<main>'
Tasks: TOP => routes:csv
Hey, do you know why I get the error
NoMethodError: undefined method '[]' for nil:NilClass
?Here is the full trace:
NoMethodError: undefined method '[]' for nil:NilClass C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actionpack-6.0.3.2/lib/action_dispatch/routing/inspector.rb:85:in 'normalize_filter' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/actionpack-6.0.3.2/lib/action_dispatch/routing/inspector.rb:65:in 'format'C:/Users/Gabriel/Documents/Edukado/edukado-web/lib/tasks/routes_csv.rake:43:in 'block (2 levels) in <top (required)>' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/task.rb:281:in 'block in execute' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/task.rb:281:in 'each' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/task.rb:281:in 'execute' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/task.rb:219:in 'block in invoke_with_call_chain' C:/Ruby26-x64/lib/ruby/2.6.0/monitor.rb:235:in 'mon_synchronize' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/task.rb:199:in 'invoke_with_call_chain' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/task.rb:188:in 'invoke' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:160:in 'invoke_task' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:116:in 'block (2 levels) in top_level'C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:116:in 'each' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:116:in 'block in top_level' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:125:in 'run_with_threads' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:110:in 'top_level' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:83:in 'block in run' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:186:in 'standard_exception_handling' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/lib/rake/application.rb:80:in 'run' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rake-13.0.3/exe/rake:27:in '<top (required)>' C:/Ruby26-x64/bin/rake:23:in 'load' C:/Ruby26-x64/bin/rake:23:in '<main>' Tasks: TOP => routes:csv
You might want to remove ENV['CONTROLLER']
from line 43 if you don't have it set.
Very helpful- thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found it through Google, thanks, very useful!