class DBDiagram::Diagram::Graphviz

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 "YourApplication domain model".

Constants

CLUSTER_ATTRIBUTES

Default cluster attributes.

EDGE_ATTRIBUTES

Default edge attributes.

FONTS
GRAPH_ATTRIBUTES

Default graph attributes.

NODE_ATTRIBUTES

Default node attributes.

Attributes

graph[RW]

Private Instance Methods

draw_cluster_node(cluster, name, options) click to toggle source
# File lib/db_diagram/diagram/graphviz.rb, line 248
def draw_cluster_node(cluster, name, options)
  cluster.add_nodes escape_name(name), options
end
draw_edge(from, to, options) click to toggle source
# File lib/db_diagram/diagram/graphviz.rb, line 252
def draw_edge(from, to, options)
  graph.add_edges graph.search_node(escape_name(from)), graph.search_node(escape_name(to)), options if node_exists?(from) and node_exists?(to)
end
draw_node(name, options) click to toggle source
# File lib/db_diagram/diagram/graphviz.rb, line 244
def draw_node(name, options)
  graph.add_nodes escape_name(name), options
end
entity_options(entity, attributes) click to toggle source
# File lib/db_diagram/diagram/graphviz.rb, line 290
def entity_options(entity, attributes)
  label = options[:markup] ? "<#{read_template(:html).result(binding)}>" : "#{read_template(:record).result(binding)}"
  entity_style(entity, attributes).merge :label => label
end
escape_name(name) click to toggle source
# File lib/db_diagram/diagram/graphviz.rb, line 256
def escape_name(name)
  "m_#{name}"
end
filename() click to toggle source

Returns the file name that will be used when saving the diagram.

# File lib/db_diagram/diagram/graphviz.rb, line 277
def filename
  "#{options.filename.presence || "#{domain.name}_#{domain.current_migration_version}"}.#{options.filetype}"
end
filetype() click to toggle source

Returns the default file extension to be used when saving the diagram.

# File lib/db_diagram/diagram/graphviz.rb, line 282
def filetype
  if options.filetype.to_sym == :dot then
    :none
  else
    options.filetype.to_sym
  end
end
node_exists?(name) click to toggle source
# File lib/db_diagram/diagram/graphviz.rb, line 240
def node_exists?(name)
  !!graph.search_node(escape_name(name))
end
read_template(type) click to toggle source
# File lib/db_diagram/diagram/graphviz.rb, line 305
def read_template(type)
  template_text = File.read(File.expand_path("templates/#{NODE_LABEL_TEMPLATES[type]}", File.dirname(__FILE__)))
  if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
    ERB.new(template_text, trim_mode: "<>")
  else
    ERB.new(template_text, nil, "<>")
  end
end
relationship_options(relationship) click to toggle source
# File lib/db_diagram/diagram/graphviz.rb, line 295
def relationship_options(relationship)
  relationship_style(relationship).tap do |options|
    # Edges with a higher weight are optimized to be shorter and straighter.
    options[:weight] = relationship.strength

    # Indirect relationships should not influence node ranks.
    # options[:constraint] = false
  end
end
title() click to toggle source

Returns the title to be used for the graph.

# File lib/db_diagram/diagram/graphviz.rb, line 261
def title
  case options.title
  when false then
    nil
  when true
    if domain.name then
      "#{domain.name} domain model"
    else
      "Domain model"
    end
  else
    options.title
  end
end