Skip to content

Instantly share code, notes, and snippets.

@vijaydev
Created October 28, 2011 20:51
Show Gist options
  • Save vijaydev/1323521 to your computer and use it in GitHub Desktop.
Save vijaydev/1323521 to your computer and use it in GitHub Desktop.
require 'graphviz'
def each_model
ObjectSpace.each_object(Class) do |klass|
yield klass if klass.ancestors.include?(ActiveRecord::Base) && klass != ActiveRecord::Base
end
end
graph_viz = GraphViz::new('Gemfile', {:concentrate => true, :normalize => true, :nodesep => 0.55})
graph_viz.edge[:fontname] = graph_viz.node[:fontname] = 'Arial, Helvetica, SansSerif'
graph_viz.edge[:fontsize] = 12
models = {}
each_model do |model|
name = model.to_s
models[name] = graph_viz.add_node(name, { :shape => 'box3d', :fontsize => 16, :style => 'filled', :fillcolor => '#B9B9D5' } )
end
each_model do |model|
model_name = model.to_s
model.reflect_on_all_associations.each do |assoc|
assoc_name = assoc.name.to_s.singularize.camelize
graph_viz.add_edge(models[model_name], models[assoc_name], { :weight => 2 }) unless models[assoc_name].nil?
end
end
graph_viz.output( :png => 'activerecord_associations.png' )
@vijaydev
Copy link
Author

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