public
Created

  • Download Gist
association_grapher.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
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' )

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.