123456789_123456789_123456789_123456789_123456789_

Class: ActionDispatch::Routing::RoutesInspector

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: actionpack/lib/action_dispatch/routing/inspector.rb

Overview

This class is just used for displaying route information when someone executes bin/rails routes or looks at the RoutingError page. People should not use this class.

Class Method Summary

Instance Method Summary

Constructor Details

.new(routes) ⇒ RoutesInspector

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/inspector.rb', line 72

def initialize(routes)
  @engines = {}
  @routes = routes
end

Instance Method Details

#collect_engine_routes(route) (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/inspector.rb', line 141

def collect_engine_routes(route)
  name = route.endpoint
  return unless route.engine?
  return if @engines[name]

  routes = route.rack_app.routes
  if routes.is_a?(ActionDispatch::Routing::RouteSet)
    @engines[name] = collect_routes(routes.routes)
  end
end

#collect_routes(routes) (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/inspector.rb', line 127

def collect_routes(routes)
  routes.collect do |route|
    RouteWrapper.new(route)
  end.reject(&:internal?).collect do |route|
    collect_engine_routes(route)

    { name: route.name,
      verb: route.verb,
      path: route.path,
      reqs: route.reqs,
      source_location: route.source_location }
  end
end

#filter_routes(filter) (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/inspector.rb', line 116

def filter_routes(filter)
  if filter
    @routes.select do |route|
      route_wrapper = RouteWrapper.new(route)
      filter.any? { |filter_type, value| route_wrapper.matches_filter?(filter_type, value) }
    end
  else
    @routes
  end
end

#format(formatter, filter = {})

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/inspector.rb', line 77

def format(formatter, filter = {})
  routes_to_display = filter_routes(normalize_filter(filter))
  routes = collect_routes(routes_to_display)
  if routes.none?
    formatter.no_routes(collect_routes(@routes), filter)
    return formatter.result
  end

  formatter.header routes
  formatter.section routes

  @engines.each do |name, engine_routes|
    formatter.section_title "Routes for #{name}"
    formatter.section engine_routes
  end

  formatter.result
end

#normalize_filter(filter) (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/inspector.rb', line 97

def normalize_filter(filter)
  if filter[:controller]
    { controller: /#{filter[:controller].underscore.sub(/_?controller\z/, "")}/ }
  elsif filter[:grep]
    grep_pattern = Regexp.new(filter[:grep])
    path = URI::DEFAULT_PARSER.escape(filter[:grep])
    normalized_path = ("/" + path).squeeze("/")

    {
      controller: grep_pattern,
      action: grep_pattern,
      verb: grep_pattern,
      name: grep_pattern,
      path: grep_pattern,
      exact_path_match: normalized_path,
    }
  end
end