Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Find unused helpers in a Rails app (slow)
#!/usr/bin/env ruby
# Shotgun approach (read: slow and dirty hack) to help find unused helpers in a Rails application
puts "Loading all source files into memory :("
source = {}
Dir["app/**/**/*.*"].each do |f|
source[ f ] = File.readlines( f )
puts "#{source.size} files loaded into memory"
helpers = []
source.keys.grep(/app\/helpers/).each do |f|
code = source[ f ]
code.each do |line|
if line =~ /def ([^\(\s]+)/
helpers << $1.chomp
puts "Scanning for #{helpers.size} helpers"
# combine the source code
complete_code = source.values.flatten
# Find occurances
helpers.each do |name|
found = false
complete_code.each do |line|
next if line =~ /def #{name}/
found = true if line =~ /#{name}/
puts "No traces found of #{name}..." unless found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment