Skip to content

Instantly share code, notes, and snippets.

@southpolesteve
Created February 28, 2013 22:02
Show Gist options
  • Save southpolesteve/5060497 to your computer and use it in GitHub Desktop.
Save southpolesteve/5060497 to your computer and use it in GitHub Desktop.
Rake taks for finding unused methods in your rails app. A bit hacky and slow, but seems to work for most cases!
namespace :cleaner do
task :find do
models = Dir.glob("app/models/**/*")
all_files = Dir.glob("app/**/*")
models.each do |model|
if File.file?(model)
File.open(model).lines.each do |line|
if line=~ /def ([a-z0-9._?!]*)/
method_name = line.match(/def ([a-z0-9._?!]*)/)[1]
method_name.sub!(/self./, '')
use_count = 0
all_files.each do |file|
if File.file?(file)
begin # Hack to handle binary files. Should probably do something else here
file_count = File.readlines(file).grep(Regexp.new(method_name)).size
rescue
end
if file_count && file_count > 0
use_count = use_count + file_count
end
end
end
if use_count == 1
puts "#{method_name} is a candidate for deletion"
end
end
end
end
end
end
end
@johana-star
Copy link

I want to refactor this more, but bedtime. Anyhow, here's a first take on revising this: https://gist.github.com/strand/5062975

@johana-star
Copy link

Another revision. Haven't run this code, so I might've messed something up here: https://gist.github.com/strand/5062975

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment