Last active
September 1, 2020 16:29
-
-
Save presidentbeef/9c0b32b3d7f46e2cd970ca0ae8972c9b to your computer and use it in GitHub Desktop.
Access Rails routes programmatically
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
# Save this file as `rr.rb` | |
# | |
# Run `./bin/rails c` to start Rails console. | |
# | |
# Then in Rails console run: | |
# require './rr'; routes = RouteReader.routes | |
# | |
# `routes` will be an Array of Hashes. Each entry is a route. | |
class RouteReader | |
attr_reader :routes | |
def self.routes | |
rr = self.new | |
ActionDispatch::Routing::RoutesInspector.new(Rails.application.routes.routes).format(rr) | |
rr.routes | |
end | |
def initialize | |
@routes = [] | |
end | |
def section(routes) | |
@routes.concat(routes) | |
end | |
def result; end | |
def section_title(title); end | |
def header(routes); end | |
def no_routes(routes); end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment