Module: RailsRoutesAnalyzer::RouteInterceptor
- Defined in:
- lib/rails_routes_analyzer/route_interceptor.rb
Overview
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
Constant Summary collapse
- ROUTE_METHOD_REGEX =
%r{action_dispatch/routing/mapper.rb:[0-9]+:in `(#{Regexp.union(*::RailsRoutesAnalyzer::ROUTE_METHODS)})'\z}
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(*args) ⇒ Object
Rails 5+.
- #record_route(controller_name, action, request_methods) ⇒ Object
-
#routes_rb_location ⇒ Object
Finds the most interesting Rails.root file from the backtrace that called a method in mapper.rb.
Class Method Details
.route_data ⇒ Object
10 11 12 13 14 15 16 |
# 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 ⇒ Object
18 19 20 |
# File 'lib/rails_routes_analyzer/route_interceptor.rb', line 18 def self.route_log @route_log ||= [] end |
Instance Method Details
#initialize(*args) ⇒ Object
Rails 5+
58 59 60 61 62 |
# File 'lib/rails_routes_analyzer/route_interceptor.rb', line 58 def initialize(*args) super.tap do record_route([:controller], [:action], conditions[:request_method]) end end |
#record_route(controller_name, action, request_methods) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# 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 ⇒ Object
Finds the most interesting Rails.root file from the backtrace that called a method in mapper.rb
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# 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 |