Created
October 6, 2016 17:52
-
-
Save froger/e5bcc119a23744bd440f4432d0384339 to your computer and use it in GitHub Desktop.
Reflexion on rails routes
This file contains hidden or 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
### | |
# Rails : 5.0.0 | |
# Ruby : 2.3.0 | |
# | |
# Reflexion on rails routes. Can be usefull to display our applications | |
# routes in a dev mode. (For example in a development-only namespace /dev/). | |
## | |
# Give informations about the routes that use the given verbs (get or post). | |
# | |
def inspect_routes(verbs = [:get]) | |
# Convert the routes as array, taking only routes. | |
routes = Rails.application.routes.routes.map { |route| route } | |
# We don't won't duplicate paths in our result | |
routes.uniq! { |route| route.path.spec.to_s } | |
# Exlude the routes we don't want | |
routes.select! do |route| | |
path = route.path.spec.to_s.gsub(/\(\.:format\)/, '') | |
verbs.include?(route.verb.downcase.to_sym) && | |
!path.start_with?('/rails/') && | |
!path.start_with?('/assets/') | |
end | |
# Create an informative hash with the collected routes | |
routes.collect do |route| | |
controller_name = route.defaults[:controller].tr('/', '_') | |
action_name = route.defaults[:action] | |
{ | |
verb: route.verb.upcase, | |
controller: controller_name, | |
action: action_name, | |
path: route.path.spec.to_s.gsub(/\(\.:format\)/, '') | |
} | |
end | |
end | |
inspect_routes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment