Class: Coverband::Collectors::ViewTracker

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

Overview

This class tracks view file usage via ActiveSupport::Notifications

This is a port of Flatfoot, a project I open sourced years ago, but am now rolling into Coverband github.com/livingsocial/flatfoot

Direct Known Subclasses

ViewTrackerService

Constant Summary collapse

REPORT_ROUTE =
"views_tracker"
TITLE =
"Views"
VIEWS_PATTERNS =
%w[.erb$ .haml$ .slim$]

Instance Attribute Summary collapse

Attributes inherited from AbstractTracker

#ignore_patterns, #logger, #store, #target

Instance Method Summary collapse

Methods inherited from AbstractTracker

#as_json, #keys_to_record, #logged_keys, #reset_recordings, #route, #save_report, supported_version?, #title, #tracking_since

Constructor Details

#initialize(options = {}) ⇒ ViewTracker

Returns a new instance of ViewTracker.



22
23
24
25
26
27
28
29
30
# File 'lib/coverband/collectors/view_tracker.rb', line 22

def initialize(options = {})
  @project_directory = File.expand_path(Coverband.configuration.root)
  @roots = options.fetch(:roots) { Coverband.configuration.all_root_patterns }
  @roots = @roots.split(",") if @roots.is_a?(String)

  super

  @ignore_patterns -= VIEWS_PATTERNS.map { |ignore_str| Regexp.new(ignore_str) }
end

Instance Attribute Details

#rootsObject (readonly)

Returns the value of attribute roots.



16
17
18
# File 'lib/coverband/collectors/view_tracker.rb', line 16

def roots
  @roots
end

Instance Method Details

#all_keysObject



82
83
84
# File 'lib/coverband/collectors/view_tracker.rb', line 82

def all_keys
  target.map { |view| normalize_path(view) }.uniq
end

#clear_key!(filename) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/coverband/collectors/view_tracker.rb', line 103

def clear_key!(filename)
  return unless filename
  return unless redis_store

  filename = "#{@project_directory}/#{filename}"
  redis_store.hdel(tracker_key, filename)
  @logged_keys.delete(filename)
end

#railtie!Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/coverband/collectors/view_tracker.rb', line 32

def railtie!
  ActiveSupport::Notifications.subscribe(/render_(template|partial|collection).action_view/) do |name, start, finish, id, payload|
    Coverband.configuration.view_tracker.track_key(payload) unless name.include?("!")
  end

  # ViewComponent >= 4.6.0 emits render.view_component with a view_identifier key
  # containing the template path. Requires config.view_component.instrumentation_enabled = true
  # in the host app.
  ActiveSupport::Notifications.subscribe("render.view_component") do |name, start, finish, id, payload|
    Coverband.configuration.view_tracker.track_key(identifier: payload[:view_identifier]) unless name.include?("!")
  end
end

#track_key(payload) ⇒ Object

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/coverband/collectors/view_tracker.rb', line 49

def track_key(payload)
  if (file = payload[:identifier])
    if newly_seen_key?(file)
      @logged_keys << file
      @keys_to_record << file if track_file?(file)
    end
  end

  ###
  # Annoyingly while you get full path for templates
  # notifications only pass part of the path for layouts dropping any format info
  # such as .html.erb or .js.erb
  # http://edgeguides.rubyonrails.org/active_support_instrumentation.html#render_partial-action_view
  ###
  return unless (layout_file = payload[:layout])
  return unless newly_seen_key?(layout_file)

  @logged_keys << layout_file
  @keys_to_record << layout_file if track_file?(layout_file, layout: true)
end

#unused_keys(used_views = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/coverband/collectors/view_tracker.rb', line 86

def unused_keys(used_views = nil)
  recently_used_views = used_keys.keys
  unused_views = all_keys - recently_used_views
  # since layouts don't include format we count them used if they match with ANY formats
  potential_layout_references = recently_used_views.reject { |v| v.end_with?(".erb", ".haml", ".slim") }

  layout_matcher = if potential_layout_references.any?
    Regexp.union(potential_layout_references)
  end

  unused_views.reject! do |view|
    (layout_matcher && view.include?("/layouts/") && view.match?(layout_matcher)) ||
      @ignore_patterns.any? { |pattern| view.match?(pattern) }
  end
  unused_views
end

#used_keysObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/coverband/collectors/view_tracker.rb', line 70

def used_keys
  return {} unless redis_store

  views = redis_store.hgetall(tracker_key)
  normalized_views = {}
  views.each_pair do |view, time|
    normalized_view = normalize_path(view)
    normalized_views[normalized_view] = time
  end
  normalized_views
end