LoginSignup
36
40

More than 5 years have passed since last update.

rails アプリから rails-erdでER図を出力するための準備

Last updated at Posted at 2014-07-26

はじめに

railsアプリからERDのpdfを出力できるという噂のrails-erd
とりあえず

Gemfile
(略)
rails-Graphviz
rails-erd
(略)

でbundle installして、ERD作成コマンドであるrake erdを実行した。

zsh
%rake erd

怒られた。

zsh
%rake erd
Loading application environment...
Loading code in search of Active Record models...
Generating Entity-Relationship Diagram for 5 models...
rake aborted!
Saving diagram failed!
Verify that Graphviz is installed and in your path, or use filetype=dot.
/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram/graphviz.rb:198:in `rescue in block in <class:Graphviz>'
/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram/graphviz.rb:190:in `block in <class:Graphviz>'
/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram.rb:142:in `instance_eval'
/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram.rb:142:in `save'
/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram.rb:120:in `create'
/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram.rb:74:in `create'
/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/tasks.rake:41:in `block (2 levels) in <top (required)>'
Tasks: TOP => erd => erd:generate
(See full trace by running task with --trace)

Graphvizがインストールされてるか確かめろ、もしくはファイルタイプをどっとにしろ、と。Graphviz というソフトを事前にインストールしておかねばならかなったのだ。

準備1.Graphvizのインストール

Graphvizのページをみる。
http://www.graphviz.org/Download_macos.php

Note: For some reason, the Graphviz app no longer works in the 2.38 and 2.39 packages for Mountain Lion (and Maverick). If you just need the command-line tools and libraries, these still work and are installed in /usr/local as usual. If you want the app, you can get it from homebrew or macports, or use the 2.36 version below.

最新の2.38はやつはMountain Lion (and Maverick)で動かねえよと。下の2.36 pkgかhomebrewとかmackportsでいれてね、とのこと。

とりあえずここを参考に
http://www.graphviz.org/Download..php

Mac OS X
MacPorts provides both stable and development versions of Graphviz and the Mac GUI Graphviz.app. These can be obtained via the ports "graphviz", "graphviz-devel", "graphviz-gui" and "graphviz-gui-devel".
Homebrew has a graphviz port (so just brew install graphviz, we hope).

"we hope"という表現に若干の不安を覚えつつ brew install graphvizでインストールした。無事インストールはできた。

準備2.rails-erdで使用するフォントの変更

Graphvizいれたからもう大丈夫だろう!再度rake erdに挑戦したらまた怒られた。

%rake erd
Loading application environment...
Loading code in search of Active Record models...
Generating Entity-Relationship Diagram for 5 models...
rake aborted!
Saving diagram failed!
Graphviz produced errors. Verify it has support for filetype=pdf, or use filetype=dot.
Original error: 2014-07-26 12:32:48.605 dot[5891:d07] CoreText performance note: Client called CTFontCreateWithName() using name "Arial Italic" and got font with PostScript name "Arial-ItalicMT". For best performance, only use PostScript names when calling this API.
/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram/graphviz.rb:195:in `rescue in block in <class:Graphviz>'
/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram/graphviz.rb:190:in `block in <class:Graphviz>'
/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram.rb:142:in `instance_eval'
/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram.rb:142:in `save'
/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram.rb:120:in `create'
/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram.rb:74:in `create'
/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/tasks.rake:41:in `block (2 levels) in <top (required)>'
Tasks: TOP => erd => erd:generate
(See full trace by running task with --trace)

メッセージから伺うに、フォントが悪いのかな?と思い調べる。
https://github.com/voormedia/rails-erd/issues/61 のhayduke19us(commented on 11 Dec 2013)は「fontnameをPostscriptフォントなやつにしろ」といっている。とりあえず指摘されている2箇所を確認。

rails_erd/diagram/graphviz.rb

/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram/graphviz.rb

module RailsERD
  class Diagram
    # Create Graphviz-based diagrams based on the domain model. For easy
    # command line graph generation, you can use:
    #
    #   % rake erd
    #
    # === Options
    #
    # The following options are supported:
    #
    # filename:: The file basename of the generated diagram. Defaults to +ERD+,
    #            or any other extension based on the file type.
    # filetype:: The file type of the generated diagram. Defaults to +pdf+, which
    #            is the recommended format. Other formats may render significantly
    #            worse than a PDF file. The available formats depend on your installation
    #            of Graphviz.
    # notation:: The cardinality notation to be used. Can be +:simple+ or
    #            +:bachman+. Refer to README.rdoc or to the examples on the project
    #            homepage for more information and examples.
    # orientation:: The direction of the hierarchy of entities. Either +:horizontal+
    #               or +:vertical+. Defaults to +horizontal+. The orientation of the
    #               PDF that is generated depends on the amount of hierarchy
    #               in your models.
    # title:: The title to add at the top of the diagram. Defaults to
    #         <tt>"YourApplication domain model"</tt>.
    class Graphviz < Diagram
      NODE_LABEL_TEMPLATES = { :html => "node.html.erb", :record => "node.record.erb" } # @private :nodoc:

      NODE_WIDTH = 130 # @private :nodoc:

      # Default graph attributes.
      GRAPH_ATTRIBUTES = {
        :rankdir => :LR,
        :ranksep => 0.5,
        :nodesep => 0.4,
        :pad => "0.4,0.4",
        :margin => "0,0",
        :concentrate => true,
        :labelloc => :t,
        :fontsize => 13,
        :fontname => "Arial Bold"
      }

      # Default node attributes.
      NODE_ATTRIBUTES = {
        :shape => "Mrecord",
        :fontsize => 10,
        :fontname => "Arial",
        :margin => "0.07,0.05",
        :penwidth => 1.0
      }

      # Default edge attributes.
      EDGE_ATTRIBUTES = {
        :fontname => "Arial",
        :fontsize => 8,
        :dir => :both,

この辺かー。バックアップとって修正する。とりあえずフォントは"Courier"にした。
:fontname => "Courier"に変更。

rails_erd/diagram/templates/node.html.erb

ビューでもfontfaceでfont指定している箇所があるので、要修正。

/Library/Ruby/Gems/2.0.0/gems/rails-erd-1.1.0/lib/rails_erd/diagram/templates/node.html.erb
<% if options.orientation == :vertical %>{<% end %>
<table border="0" align="center" cellspacing="0.5" cellpadding="0" width="<%= NODE_WIDTH + 4 %>">
  <tr><td align="center" valign="bottom" width="<%= NODE_WIDTH %>"><font face="Arial Bold" point-size="11"><%= entity.name %></font></td></tr>
</table>
<% if attributes.any? %>
|
<table border="0" align="left" cellspacing="2" cellpadding="0" width="<%= NODE_WIDTH + 4 %>">
<%   attributes.each do |attribute| %>
  <tr><td align="left" width="<%= NODE_WIDTH %>" port="<%= attribute %>"><%= attribute %> <font face="Arial Italic" color="grey60"><%= attribute.type_description %></font></td></tr>
<%   end %>
</table>
<% else %>
<% end %>
<% if options.orientation == :vertical %>}<% end %>

font face="Courier"にした。

再挑戦=>成功!!

再挑戦

zsh
%rake erd
Loading application environment...
Loading code in search of Active Record models...
Generating Entity-Relationship Diagram for 5 models...
Done! Saved diagram to erd.pdf.

やったー!プロジェクトルート直下にpdfファイルができたよ。

%ls
Gemfile
README.rdoc
app
config
db           
lib          
public       
tmp
Gemfile.lock 
Rakefile     
bin          
config.ru    
erd.pdf      
log          
test         
vendor

こんな感じのができた(適当なモデルだから恥ずかしい)
スクリーンショット 2014-07-26 12.59.03.png

Courierだとなんとなくかわいい。えがったえがった。

36
40
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
40