Finding unused code
in your Rails app
The problem
Views
find app/views -type f | wc -l
1060!!
Controller methods
ag 'def ' app/controllers | wc -l
1231!!
How?
Active Support Instrumentation
Finding views
render_template.action_view
render_partial.action_view
=>
{
identifier: "/Users/adam/projects/notifications/app/views/posts/index.html.erb",
layout: "layouts/application"
}
Collecting information
config/initializers/trace_views.rb
def metric_name(path)
path.sub(/A#{Rails.root}//, '')
end
%w(render_template.action_view render_partial.action_view).map do |event_name|
ActiveSupport::Notifications.subscribe(event_name) do |*data|
event = ActiveSupport::Notifications::Event.new(*data)
template_name = metric_name(event.payload[:identifier])
UsedView.find_or_create_by_template_name(template_name)
end
end
Processing information
Dir.glob("app/views/**/*.haml").each do |view|
puts view unless UsedView.find_by_template_name(view)
end
Another approach...
Coverband
Rack middleware to help measure production code line
of code usage
https://github.com/danmayer/coverband
Finding unused code in your Rails app

Finding unused code in your Rails app