Class: Flatfoot::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/flatfoot.rb

Constant Summary collapse

DEFAULT_TARGET =
Dir.glob('app/views/**/*.html.erb').reject{ |file| file.match(/(_mailer)/) }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, options = {}) ⇒ Tracker

Returns a new instance of Tracker.



11
12
13
14
15
16
# File 'lib/flatfoot.rb', line 11

def initialize(store, options = {})
  @store = store
  @target = options[:target] || DEFAULT_TARGET
  @logged_views = []
  @roots = options.fetch(:roots){ "#{Rails.root.to_s}/" }.split(',')
end

Instance Attribute Details

#logged_viewsObject

Returns the value of attribute logged_views.



9
10
11
# File 'lib/flatfoot.rb', line 9

def logged_views
  @logged_views
end

#rootsObject

Returns the value of attribute roots.



9
10
11
# File 'lib/flatfoot.rb', line 9

def roots
  @roots
end

#storeObject

Returns the value of attribute store.



9
10
11
# File 'lib/flatfoot.rb', line 9

def store
  @store
end

#targetObject

Returns the value of attribute target.



9
10
11
# File 'lib/flatfoot.rb', line 9

def target
  @target
end

Instance Method Details

#reset_recordingsObject



62
63
64
# File 'lib/flatfoot.rb', line 62

def reset_recordings
  store.del(tracker_key)
end

#track_views(name, start, finish, id, payload) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/flatfoot.rb', line 18

def track_views(name, start, finish, id, payload)
  begin
    if file = payload[:identifier]
      unless logged_views.include?(file)
        logged_views << file
        store.sadd(tracker_key, 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
    ###
    if layout_file = payload[:layout]
      unless logged_views.include?(layout_file)
        logged_views << layout_file
        store.sadd(tracker_key, layout_file)
      end
    end
  rescue Errno::EAGAIN, Timeout::Error
    #we don't want to raise errors if flatfoot can't reach redis. This is a nice to have not a bring the system down
  end
end

#unused_viewsObject



55
56
57
58
59
60
# File 'lib/flatfoot.rb', line 55

def unused_views
  recently_used_views = used_views
  all_views = target.reject{ |view| recently_used_views.include?(view) }
  # since layouts don't include format we count them used if they match with ANY formats
  all_views = all_views.reject{ |view| view.match(/\/layouts\//) && recently_used_views.any?{|used_view| view.include?(used_view)} }
end

#used_viewsObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/flatfoot.rb', line 43

def used_views
  views = store.smembers(tracker_key)
  normalized_views = []
  views.each do |view|
    roots.each do |root|
      view = view.gsub(/#{root}/,'')
    end
    normalized_views << view
  end
  normalized_views
end