Skip to content

Options to use table names and native column types #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/rails_erd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def default_options
:only_recursion_depth, nil,
:prepend_primary, false,
:cluster, false,
:table_names, false,
:native_types, false
]
end

Expand Down
10 changes: 10 additions & 0 deletions lib/rails_erd/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@
desc "Control how edges are represented. See http://www.graphviz.org/doc/info/attrs.html#d:splines for values."
end

option :table_names do
long "--table_names=BOOLEAN"
desc "Label each entity with the table name instead of the ActiveRecord class name."
end

option :native_types do
long "--native_types=BOOLEAN"
desc "Display the db-native types instead of the Rails migration ones."
end

separator ""
separator "Output options:"

Expand Down
2 changes: 1 addition & 1 deletion lib/rails_erd/diagram/templates/node.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% if options.orientation == :horizontal %>{<% 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="<%= FONTS[:bold] %>" point-size="11"><%= entity.name %></font></td></tr>
<tr><td align="center" valign="bottom" width="<%= NODE_WIDTH %>"><font face="<%= FONTS[:bold] %>" point-size="11"><%= entity.label %></font></td></tr>
</table>
<% if attributes.any? %>
|
Expand Down
4 changes: 2 additions & 2 deletions lib/rails_erd/diagram/templates/node.record.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% if options.orientation == :horizontal %>{<% end %><%= entity.name %><% if attributes.any? %>
<% if options.orientation == :horizontal %>{<% end %><%= entity.label %><% if attributes.any? %>
|<% attributes.each do |attribute| %><%=
attribute %> (<%= attribute.type_description %>)
<% end %><% end %><% if options.orientation == :horizontal %>}<% end %>
<% end %><% end %><% if options.orientation == :horizontal %>}<% end %>
3 changes: 2 additions & 1 deletion lib/rails_erd/domain/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def name
# The type of the attribute, equal to the Rails migration type. Can be any
# of +:string+, +:integer+, +:boolean+, +:text+, etc.
def type
column.type or column.sql_type.downcase.to_sym
!RailsERD.options[:native_types] and column.type or column.sql_type.downcase.to_sym
end

# Returns +true+ if this attribute is a content column, that is, if it
Expand Down Expand Up @@ -120,6 +120,7 @@ def to_s # @private :nodoc:
# <tt>:boolean, :null => false</tt>:: boolean *
def type_description
type.to_s.dup.tap do |desc|
desc << "[]" if column.array?
desc << " #{limit_description}" if limit_description
desc << " ∗" if mandatory? && !primary_key? # Add a hair space + low asterisk (Unicode characters)
desc << " U" if unique? && !primary_key? && !foreign_key? # Add U if unique but non-key
Expand Down
4 changes: 4 additions & 0 deletions lib/rails_erd/domain/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def initialize(domain, name, model = nil) # @private :nodoc:
@domain, @name, @model = domain, name, model
end

def label
RailsERD.options[:table_names] ? model.table_name : name
end

# Returns an array of attributes for this entity.
def attributes
@attributes ||= generalized? ? [] : Attribute.from_model(domain, model)
Expand Down