Class: Traceroute
- Inherits:
-
Object
- Object
- Traceroute
- Defined in:
- lib/traceroute.rb
Defined Under Namespace
Classes: Railtie
Constant Summary collapse
- VERSION =
Gem.loaded_specs['traceroute'].version.to_s
- WILDCARD_ROUTES =
%r[^/?:controller\(?/:action].freeze
Instance Method Summary collapse
- #collect_routes(routes) ⇒ Object
- #defined_action_methods ⇒ Object
-
#initialize(app) ⇒ Traceroute
constructor
A new instance of Traceroute.
- #load_everything! ⇒ Object
- #routed_actions ⇒ Object
- #routes ⇒ Object
- #unreachable_action_methods ⇒ Object
- #unused_routes ⇒ Object
Constructor Details
#initialize(app) ⇒ Traceroute
Returns a new instance of Traceroute.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/traceroute.rb', line 15 def initialize(app) @app = app @ignored_unreachable_actions = [] @ignored_unused_routes = [/^\/cable$/] @ignored_unused_routes << %r{^#{@app.config.assets.prefix}} if @app.config.respond_to? :assets config_filename = %w(.traceroute.yaml .traceroute.yml .traceroute).detect {|f| File.exist?(f)} if config_filename && (config = YAML.load_file(config_filename)) (config['ignore_unreachable_actions'] || []).each do |ignored_action| @ignored_unreachable_actions << Regexp.new(ignored_action) end (config['ignore_unused_routes'] || []).each do |ignored_action| @ignored_unused_routes << Regexp.new(ignored_action) end end end |
Instance Method Details
#collect_routes(routes) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/traceroute.rb', line 81 def collect_routes(routes) routes = routes.each_with_object([]) do |r, tmp_routes| next if (ActionDispatch::Routing::Mapper::Constraints === r.app) && (%w[ActionDispatch::Routing::PathRedirect ActionDispatch::Routing::Redirect].include?(r.app.app.class.name)) if r.app.is_a?(ActionDispatch::Routing::Mapper::Constraints) && r.app.app.respond_to?(:routes) engine_routes = r.app.app.routes if engine_routes.is_a?(ActionDispatch::Routing::RouteSet) tmp_routes.concat collect_routes(engine_routes.routes) end else tmp_routes << r end end routes.reject! {|r| r.app.is_a?(ActionDispatch::Routing::Redirect)} routes end |
#defined_action_methods ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/traceroute.rb', line 53 def defined_action_methods @defined_action_methods ||= [ActionController::Base, (ActionController::API if defined?(ActionController::API))].compact.map do |klass| klass.descendants.map do |controller| controller.action_methods.reject {|a| (a =~ /\A(_conditional)?_callback_/) || (a == '_layout_from_proc')}.map do |action| "#{controller.controller_path}##{action}" end end.flatten end.flatten.reject {|r| @ignored_unreachable_actions.any? { |m| r.match(m) } } end |
#load_everything! ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/traceroute.rb', line 35 def load_everything! @app.eager_load! ::Rails::InfoController rescue NameError ::Rails::WelcomeController rescue NameError ::Rails::MailersController rescue NameError @app.reload_routes! Rails::Engine.subclasses.each(&:eager_load!) end |
#routed_actions ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/traceroute.rb', line 63 def routed_actions @routed_actions ||= routes.map do |r| if r.requirements[:controller].present? && r.requirements[:action].present? "#{r.requirements[:controller]}##{r.requirements[:action]}" elsif (String === r.path) && (WILDCARD_ROUTES =~ r.path) %Q["#{r.path}" ⚠️ This is a legacy wild controller route that's not recommended for RESTful applications.] elsif WILDCARD_ROUTES =~ r.path.spec.to_s %Q["#{r.path.spec}" ⚠️ This is a legacy wild controller route that's not recommended for RESTful applications.] else ((String === r.path) && r.path.to_s) || r.path.spec.to_s # unknown routes end end.compact.flatten.reject {|r| @ignored_unused_routes.any? { |m| r.match(m) } } end |
#routes ⇒ Object
77 78 79 |
# File 'lib/traceroute.rb', line 77 def routes collect_routes @app.routes.routes end |
#unreachable_action_methods ⇒ Object
49 50 51 |
# File 'lib/traceroute.rb', line 49 def unreachable_action_methods defined_action_methods - routed_actions end |
#unused_routes ⇒ Object
45 46 47 |
# File 'lib/traceroute.rb', line 45 def unused_routes routed_actions - defined_action_methods end |