Skip to content

Instantly share code, notes, and snippets.

@hosh
Created March 30, 2010 17:34
Show Gist options
  • Save hosh/349333 to your computer and use it in GitHub Desktop.
Save hosh/349333 to your computer and use it in GitHub Desktop.
# Package for inspecting descendents of ActiveRecord and ActionMailer in a simple tree form.
# Works with Rails 2.3.5
# INSTALL
# Drop this into lib/tasks
# USAGE
# rake stats:models
# rake stats:mailers
# Monkeypatch Class so we get direct descendents (children)
class Class
def children
result = []
ObjectSpace.each_object(Class) { |klass| result << klass if klass.superclass == self }
result
end
end
namespace :stats do
def force_load_application_classes
$rails_rake_task = false
Rails::Initializer.run(:load_application_classes) do |config|
config.cache_classes = true
end
$rails_rake_task = true
end
def sort_classes(klasses)
klasses.map { |k| [k.to_s, k] }.sort.map { |s| s[1] } # Hack to force sort on stringified Class name
end
def print_descendant_tree(klass, indent = 0)
puts (" " * indent) + klass.to_s
sort_classes(klass.children).each { |descendant| print_descendant_tree(descendant, indent + 2) }
end
desc 'Shows a tree of all ActiveRecord::Base derived models'
task :models => [ :environment ] do
force_load_application_classes
print_descendant_tree(ActiveRecord::Base)
end
desc 'Shows a tree of all ActionMailer::Base derived mailers'
task :mailers => [ :environment ] do
force_load_application_classes
print_descendant_tree(ActionMailer::Base)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment