Skip to content

Instantly share code, notes, and snippets.

@adamsanderson
Created February 18, 2009 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamsanderson/66215 to your computer and use it in GitHub Desktop.
Save adamsanderson/66215 to your computer and use it in GitHub Desktop.
Quick and dirty dot file for ActiveRecord models
# Quick and dirty dot file for ActiveRecord models
namespace :dot do
task :load_models => :environment do
Dir.glob("#{RAILS_ROOT}/app/models/**/*rb").each{|p| require p}
end
desc 'Generates a dot file of the models'
task :models => :load_models do
models = Object.subclasses_of ActiveRecord::Base
open("#{RAILS_ROOT}/models.dot",'w') do |io|
io << "digraph Models{"
io << "node;"
models.each do |model|
name = model.to_s.gsub('::','')
io << "#{format_model name,model}\n"
model.reflect_on_all_associations.each do |a|
if a.macro.to_s[/has_/]
io << "#{name} -> #{a.class_name.gsub('::','')}\n"
end
end
end
io << "}"
end
end
def format_model(name, cls)
if cls.table_exists?
n = 0..cls.columns.length
col_cells = cls.columns.zip(n.to_a).map{|c,i| "<TR><TD align='left'>#{c.name}</TD><TD align='left'>#{c.type}</TD></TR>"}.join(' ')
%!#{name} [shape=plaintext label=<<TABLE bgcolor="white" border="1" cellborder="1" cellspacing="0"><TR><TD bgcolor="#cccccc" COLSPAN="2">#{name}</TD></TR>#{col_cells}</TABLE>>]!
else
"#{name} [style=\"dashed\"]"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment