Class: Coverband::Collectors::RouteTracker

Inherits:
AbstractTracker show all
Defined in:
lib/coverband/collectors/route_tracker.rb

Overview

This class tracks route usage via ActiveSupport::Notifications

Constant Summary collapse

REPORT_ROUTE =
"routes_tracker"
TITLE =
"Routes"

Instance Attribute Summary

Attributes inherited from AbstractTracker

#ignore_patterns, #logger, #store, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractTracker

#all_keys, #as_json, #clear_key!, #keys_to_record, #logged_keys, #reset_recordings, #route, #save_report, #title, #tracking_since, #used_keys

Constructor Details

#initialize(options = {}) ⇒ RouteTracker

Returns a new instance of RouteTracker.



15
16
17
18
19
20
21
# File 'lib/coverband/collectors/route_tracker.rb', line 15

def initialize(options = {})
  if Rails&.respond_to?(:version) && Gem::Version.new(Rails.version) >= Gem::Version.new("6.0.0") && Gem::Version.new(Rails.version) < Gem::Version.new("7.1.0")
    require_relative "../utils/rails6_ext"
  end

  super
end

Class Method Details

.supported_version?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/coverband/collectors/route_tracker.rb', line 54

def self.supported_version?
  defined?(Rails) && defined?(Rails::VERSION) && Rails::VERSION::STRING.split(".").first.to_i >= 6
end

Instance Method Details

#railtie!Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/coverband/collectors/route_tracker.rb', line 64

def railtie!
  ActiveSupport::Notifications.subscribe("start_processing.action_controller") do |name, start, finish, id, payload|
    Coverband.configuration.route_tracker.track_key(payload)
  end

  # NOTE: This event was instrumented in Aug 10th 2022, but didn't make the 7.0.4 release and should be in the next release
  # https://github.com/rails/rails/pull/43755
  # Automatic tracking of redirects isn't available before Rails 7.1.0 (currently tested against the 7.1.0.alpha)
  # We could consider back porting or patching a solution that works on previous Rails versions
  ActiveSupport::Notifications.subscribe("redirect.action_dispatch") do |name, start, finish, id, payload|
    Coverband.configuration.route_tracker.track_key(payload)
  end
end

#track_key(payload) ⇒ Object

This method is called on every routing call, so we try to reduce method calls and ensure high performance



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/coverband/collectors/route_tracker.rb', line 27

def track_key(payload)
  route = if payload.key?(:location)
    # For redirect.action_dispatch
    return unless Coverband.configuration.track_redirect_routes

    {
      controller: nil,
      action: nil,
      url_path: payload[:request].path,
      verb: payload[:request].method
    }
  else
    # For start_processing.action_controller
    {
      controller: payload[:params]["controller"],
      action: payload[:action],
      url_path: nil,
      verb: payload[:method]
    }
  end

  if newly_seen_key?(route)
    @logged_keys << route
    @keys_to_record << route if track_key?(route)
  end
end

#unused_keys(used_keys = nil) ⇒ Object



58
59
60
61
62
# File 'lib/coverband/collectors/route_tracker.rb', line 58

def unused_keys(used_keys = nil)
  recently_used_routes = (used_keys || self.used_keys).keys
  # NOTE: we match with or without path to handle paths with named params like `/user/:user_id` to used routes filling with all the variable named paths
  all_keys.reject { |r| recently_used_routes.include?(r.to_s) || recently_used_routes.include?(r.merge(url_path: nil).to_s) }
end