public

Find unused helpers in a Rails app (slow)

  • Download Gist
find_unused_helpers.rb
Ruby
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
#!/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 )
end
 
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
end
end
end
 
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}/
end
 
puts "No traces found of #{name}..." unless found
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.