module RailsRoutesAnalyzer::RouteInterceptor

Plugs into ActionDispatch::Routing::Mapper::Mapping to help get detailed information on which route was generated, exactly where and if there is a matching controller action

Constants

ROUTE_METHOD_REGEX

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/rails_routes_analyzer/route_interceptor.rb, line 46
def initialize(*args)
  super.tap do
    record_route(@options[:controller], @options[:action], conditions[:request_method])
  end
end
route_data() click to toggle source
# File lib/rails_routes_analyzer/route_interceptor.rb, line 10
def self.route_data
  {}.tap do |result|
    route_log.each do |(location, _controller_name, action, _request_methods)|
      (result[location] ||= []) << action
    end
  end
end
route_log() click to toggle source
# File lib/rails_routes_analyzer/route_interceptor.rb, line 18
def self.route_log
  @route_log ||= []
end

Public Instance Methods

record_route(controller_name, action, request_methods) click to toggle source
# File lib/rails_routes_analyzer/route_interceptor.rb, line 65
def record_route(controller_name, action, request_methods)
  return unless controller_name && action

  location = routes_rb_location + [controller_name]

  if location[0].nil?
    puts "Failed to find call location for: #{controller_name}/#{action}"
  else
    record = [location, controller_name, action.to_sym, request_methods]

    RouteInterceptor.route_log << record
  end
end
routes_rb_location() click to toggle source

Finds the most interesting Rails.root file from the backtrace that called a method in mapper.rb

# File lib/rails_routes_analyzer/route_interceptor.rb, line 23
def routes_rb_location
  bt = caller
  base = 0
  loop do
    index = bt[base..-1].index { |l| l =~ ROUTE_METHOD_REGEX }
    return "" if index.nil?

    next_line = bt[base + index + 1]

    if next_line =~ %r{action_dispatch/routing/mapper.rb}
      base += index + 1
      next
    else
      file_location = next_line[%r{:?\A#{Rails.root}\/(.*:[0-9]+)}, 1] || next_line

      route_creation_method = bt[base + index][ROUTE_METHOD_REGEX, 1]

      return [file_location, route_creation_method]
    end
  end
end