Class: Coverband::Collectors::ViewTracker
- Inherits:
-
AbstractTracker
- Object
- AbstractTracker
- Coverband::Collectors::ViewTracker
- 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
Constant Summary collapse
- REPORT_ROUTE =
"views_tracker"
- TITLE =
"Views"
- VIEWS_PATTERNS =
%w[.erb$ .haml$ .slim$]
Instance Attribute Summary collapse
-
#roots ⇒ Object
readonly
Returns the value of attribute roots.
Attributes inherited from AbstractTracker
#ignore_patterns, #logger, #store, #target
Instance Method Summary collapse
- #all_keys ⇒ Object
- #clear_key!(filename) ⇒ Object
-
#initialize(options = {}) ⇒ ViewTracker
constructor
A new instance of ViewTracker.
- #railtie! ⇒ Object
-
#track_key(payload) ⇒ Object
This method is called on every render call, so we try to reduce method calls and ensure high performance.
- #unused_keys(used_views = nil) ⇒ Object
- #used_keys ⇒ Object
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( = {}) @project_directory = File.(Coverband.configuration.root) @roots = .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
#roots ⇒ Object (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_keys ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/coverband/collectors/view_tracker.rb', line 75 def all_keys all_views = [] target.each do |view| roots.each do |root| view = view.gsub(root, "") end all_views << view end all_views.uniq end |
#clear_key!(filename) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/coverband/collectors/view_tracker.rb', line 94 def clear_key!(filename) return unless filename filename = "#{@project_directory}/#{filename}" redis_store.hdel(tracker_key, filename) @logged_keys.delete(filename) end |
#railtie! ⇒ Object
32 33 34 35 36 |
# 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 end |
#track_key(payload) ⇒ Object
This method is called on every render call, so we try to reduce method calls and ensure high performance
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/coverband/collectors/view_tracker.rb', line 42 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 |
# 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 unused_views = unused_views.reject { |view| view.include?("/layouts/") && recently_used_views.any? { |used_view| view.include?(used_view) } } unused_views.reject { |view| @ignore_patterns.any? { |pattern| view.match?(pattern) } } end |
#used_keys ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/coverband/collectors/view_tracker.rb', line 63 def used_keys views = redis_store.hgetall(tracker_key) normalized_views = {} views.each_pair do |view, time| roots.each do |root| view = view.gsub(root, "") end normalized_views[view] = time end normalized_views end |