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"
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.
21 22 23 24 25 26 27 |
# File 'lib/coverband/collectors/view_tracker.rb', line 21 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 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
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/coverband/collectors/view_tracker.rb', line 72 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
90 91 92 93 94 95 96 |
# File 'lib/coverband/collectors/view_tracker.rb', line 90 def clear_key!(filename) return unless filename filename = "#{@project_directory}/#{filename}" redis_store.hdel(tracker_key, filename) @logged_keys.delete(filename) end |
#railtie! ⇒ Object
29 30 31 32 33 |
# File 'lib/coverband/collectors/view_tracker.rb', line 29 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
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/coverband/collectors/view_tracker.rb', line 39 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
83 84 85 86 87 88 |
# File 'lib/coverband/collectors/view_tracker.rb', line 83 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.reject { |view| view.match(/\/layouts\//) && recently_used_views.any? { |used_view| view.include?(used_view) } } end |
#used_keys ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/coverband/collectors/view_tracker.rb', line 60 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 |