Rake task to generate model diagrams in a rails project

namespace :doc do
  desc "document db: (annotate, schema.rb, dot)"
  task :db=>%w(annotate_models db:schema:dump doc:dot)

  desc "Use railroad to generate graphviz dot files"
  task :dot do
    #tack on stuff at the top of the page
    FORMAT=%q{sed 's/graph\\[/  graph[page="8.5,11",\
            size="7.5,10",center=1];\
    node[shape=Mrecord,width="1.2",fillcolor="#cccccc",\
            style="filled", penwidth="2"]\
    edge [ minlen="1.2", penwidth="2"]\
    graph[/'}
  
    now=`rake db:version`.chomp.split(' ')[-1]
    models="doc/models#{now}.dot"
    #hide magic hides created_at, updated_at
   puts `railroad -liM --hide-magic|#{FORMAT}>#{models}`
  
    [models].each do |filename|
      filetype='pdf'
      outfile=filename.gsub('.dot',".#{filetype}")
      #create viewable file (graphviz must be installed)
      puts `dot -T#{filetype} #{filename} -o#{outfile}`

      #open the file using mac graphviz app
      puts `open #{filename} -a graphviz`
    end
  end
end

Rake task to generate model diagrams in a rails project


namespace :doc do
  desc "document db: (annotate, schema.rb, dot)"
  task :db=>%w(annotate_models db:schema:dump doc:dot)

  desc "Use railroad to generate graphviz dot files"
  task :dot do
    #tack on stuff at the top of the page
    FORMAT=%q{sed 's/graph\\[/  graph[page="8.5,11",\
            size="7.5,10",center=1];\
    node[shape=Mrecord,width="1.2",fillcolor="#cccccc",\
            style="filled", penwidth="2"]\
    edge [ minlen="1.2", penwidth="2"]\
    graph[/'}
  
    now=`rake db:version`.chomp.split(' ')[-1]
    models="doc/models#{now}.dot"
    #hide magic hides created_at, updated_at
   puts `railroad -liM --hide-magic|#{FORMAT}>#{models}`
  
    [models].each do |filename|
      filetype='pdf'
      outfile=filename.gsub('.dot',".#{filetype}")
      #create viewable file (graphviz must be installed)
      puts `dot -T#{filetype} #{filename} -o#{outfile}`

      #open the file using mac graphviz app
      puts `open #{filename} -a graphviz`
    end
  end
end