Skip to content

Instantly share code, notes, and snippets.

@tadast
Created May 6, 2013 19:46
Show Gist options
  • Save tadast/5527622 to your computer and use it in GitHub Desktop.
Save tadast/5527622 to your computer and use it in GitHub Desktop.
easy rails route scanning in Rails console
# Put this in your ~/.irbrc for easy rails route scanning
#
# Usage:
# > routes
# => prints all routes
# > routes /GET.*user/i
# => prints routes matching a given regex
# > routes "user"
# => matches strings as well
if defined? Rails
if Rails::VERSION::STRING >= "4"
# returns an array of routes in Rails 4
def formatted_routes
r = Rails.application.routes.routes
i = ActionDispatch::Routing::RoutesInspector.new(r)
f = ActionDispatch::Routing::ConsoleFormatter.new
i.format(f).split("\n")
end
else
require 'rails/application/route_inspector'
# returns an array of routes in Rails 3
def formatted_routes
inspector = Rails::Application::RouteInspector.new
inspector.format(Rails.application.routes.routes)
end
end
# filter and print out the routes
def routes(filter = nil)
Rails.application.reload_routes!
formatted = formatted_routes
if filter
formatted.select!{ |r| r.match(filter) }
end
puts formatted
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment