2016-08-06 13:15:47 -04:00
|
|
|
require "active_support/deprecation"
|
|
|
|
require "active_support/core_ext/string/strip" # for strip_heredoc
|
|
|
|
require "optparse"
|
2016-01-24 15:02:44 -05:00
|
|
|
|
2016-08-06 13:15:47 -04:00
|
|
|
desc "Print out all defined routes in match order, with names. Target specific controller with -c option, or grep routes using -g option"
|
2012-10-14 06:03:39 -04:00
|
|
|
task routes: :environment do
|
2010-08-28 21:53:18 -04:00
|
|
|
all_routes = Rails.application.routes.routes
|
2016-08-06 13:15:47 -04:00
|
|
|
require "action_dispatch/routing/inspector"
|
2012-12-30 09:04:09 -05:00
|
|
|
inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)
|
2016-08-06 13:15:47 -04:00
|
|
|
if ARGV.any?{ |argv| argv.start_with? "CONTROLLER" }
|
2016-01-24 15:02:44 -05:00
|
|
|
puts <<-eow.strip_heredoc
|
2016-02-02 18:16:18 -05:00
|
|
|
Passing `CONTROLLER` to `bin/rails routes` is deprecated and will be removed in Rails 5.1.
|
|
|
|
Please use `bin/rails routes -c controller_name` instead.
|
2016-01-24 15:02:44 -05:00
|
|
|
eow
|
|
|
|
end
|
|
|
|
|
|
|
|
routes_filter = nil
|
2016-08-06 13:15:47 -04:00
|
|
|
routes_filter = { controller: ENV["CONTROLLER"] } if ENV["CONTROLLER"]
|
2016-01-24 15:02:44 -05:00
|
|
|
|
|
|
|
OptionParser.new do |opts|
|
2016-02-02 18:16:18 -05:00
|
|
|
opts.banner = "Usage: rails routes [options]"
|
2016-04-24 00:40:58 -04:00
|
|
|
|
|
|
|
Rake.application.standard_rake_options.each { |args| opts.on(*args) }
|
|
|
|
|
2016-02-11 06:36:03 -05:00
|
|
|
opts.on("-c CONTROLLER") do |controller|
|
2016-01-24 15:02:44 -05:00
|
|
|
routes_filter = { controller: controller }
|
|
|
|
end
|
|
|
|
|
2016-02-11 06:36:03 -05:00
|
|
|
opts.on("-g PATTERN") do |pattern|
|
2016-01-24 15:02:44 -05:00
|
|
|
routes_filter = pattern
|
|
|
|
end
|
|
|
|
|
|
|
|
end.parse!(ARGV.reject { |x| x == "routes" })
|
|
|
|
|
|
|
|
puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, routes_filter)
|
|
|
|
|
|
|
|
exit 0 # ensure extra arguments aren't interpreted as Rake tasks
|
2010-08-29 13:28:10 -04:00
|
|
|
end
|