-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathtemplates_tracker_rspec.rb
65 lines (53 loc) · 1.11 KB
/
templates_tracker_rspec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
53
54
55
56
57
58
59
60
61
62
63
64
65
module TemplatesTracker
class << self
attr_reader :mode
def start(mode)
@mode = mode
ActionView::Template.send(:prepend, TemplatesTracker::Ext)
end
def track(path)
return unless path.start_with?(root)
store[path] = 1
end
def print
if mode == "debug"
puts "\n======== Used Templates =========\n\n"
puts used.join("\n")
end
puts "\n======== Unused Templates =========\n\n"
puts unused.join("\n")
end
def used
store.keys
end
def unused
all - used
end
# FIXME: check other view paths (using config.paths)
# Currently, only takes into account the app's templates.
def all
Dir[root + "/app/views/**/*.erb"]
end
private
def store
@store ||= {}
end
def root
@root ||= Rails.root.to_s
end
end
module Ext
def initialize(*, **)
super
TemplatesTracker.track(@identifier)
end
end
end
if ENV["TT"]
TemplatesTracker.start(ENV["TT"])
RSpec.configure do |config|
config.after(:suite) do
TemplatesTracker.print
end
end
end