class DBDiagram::Domain::Attribute

Describes an entity's attribute. Attributes correspond directly to database columns.

Public Class Methods

prepend_primary(model, attributes) click to toggle source
# File lib/db_diagram/domain/attribute.rb, line 23
def prepend_primary(model, attributes)
  primary_key = ActiveRecord::Base.get_primary_key(model)
  primary = attributes.index { |column| column.name == primary_key }

  return attributes if primary || primary == 0

  [primary_key, *attributes[0...primary], *attributes[(primary+1)..-1]]
end

Public Instance Methods

content?() click to toggle source

Returns true if this attribute is a content column, that is, if it is not a primary key, foreign key, timestamp, or inheritance column.

# File lib/db_diagram/domain/attribute.rb, line 55
def content?
  !primary_key? and !foreign_key? and !timestamp? and !inheritance?
end
false?() click to toggle source

Method allows false to be set as an attributes option when making custom graphs. It rejects all attributes when called from Diagram#filtered_attributes method

# File lib/db_diagram/domain/attribute.rb, line 91
def false?
  false
end
foreign_key?() click to toggle source

Returns true if this attribute is used as a foreign key for any relationship.

# File lib/db_diagram/domain/attribute.rb, line 77
def foreign_key?
  @domain.relationships_by_entity_name(@model.name).map(&:associations).flatten.map { |associaton|
    associaton.send(Domain.foreign_key_method_name)
  }.include?(name)
end
inheritance?() click to toggle source

Returns true if this attribute is used for single table inheritance. These attributes are typically named type.

# File lib/db_diagram/domain/attribute.rb, line 85
def inheritance?
  @model.inheritance_column == name
end
limit() click to toggle source

Returns any non-standard limit for this attribute. If a column has no limit or uses a default database limit, this method returns nil.

# File lib/db_diagram/domain/attribute.rb, line 135
def limit
  return if native_type == 'geometry' || native_type == 'geography'
  return column.limit.to_i if column.limit != native_type[:limit] and column.limit.respond_to?(:to_i)
  column.precision.to_i if column.precision != native_type[:precision] and column.precision.respond_to?(:to_i)
end
mandatory?() click to toggle source

Returns true if this attribute is mandatory. Mandatory attributes either have a presence validation (validates_presence_of), or have a NOT NULL database constraint.

# File lib/db_diagram/domain/attribute.rb, line 62
def mandatory?
  !column.null or @model.validators_on(name).map(&:kind).include?(:presence)
end
name() click to toggle source

The name of the attribute, equal to the column name.

# File lib/db_diagram/domain/attribute.rb, line 43
def name
  column.name
end
primary_key?() click to toggle source

Returns true if this attribute is the primary key of the entity.

# File lib/db_diagram/domain/attribute.rb, line 71
def primary_key?
  @model.primary_key.to_s == name.to_s
end
scale() click to toggle source

Returns any non-standard scale for this attribute (decimal types only).

# File lib/db_diagram/domain/attribute.rb, line 142
def scale
  return column.scale.to_i if column.scale != native_type[:scale] and column.scale.respond_to?(:to_i)
  0 if column.precision
end
timestamp?() click to toggle source

Returns true if this attribute is one of the standard 'magic' Rails timestamp columns, being created_at, updated_at, created_on or updated_on.

# File lib/db_diagram/domain/attribute.rb, line 102
def timestamp?
  TIMESTAMP_NAMES.include? name
end
true?() click to toggle source
# File lib/db_diagram/domain/attribute.rb, line 95
def true?
  true
end
type() click to toggle source

The type of the attribute, equal to the Rails migration type. Can be any of :string, :integer, :boolean, :text, etc.

# File lib/db_diagram/domain/attribute.rb, line 49
def type
  column.sql_type.downcase.to_sym or column.type
end
type_description() click to toggle source

Returns a description of the attribute type. If the attribute has a non-standard limit or if it is mandatory, this information is included.

Example output:

:integer

integer

:string, :limit => 255

string

:string, :limit => 128

string (128)

<tt>:decimal, :precision => 5, :scale => 2/tt>

decimal (5,2)

:boolean, :null => false

boolean *

# File lib/db_diagram/domain/attribute.rb, line 123
def type_description
  type.to_s.tap do |desc|
    # 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
    desc << " PK" if primary_key?
    desc << " FK" if foreign_key?
  end
end
unique?() click to toggle source
# File lib/db_diagram/domain/attribute.rb, line 66
def unique?
  @model.validators_on(name).map(&:kind).include?(:uniqueness)
end

Private Instance Methods

native_type() click to toggle source
# File lib/db_diagram/domain/attribute.rb, line 157
def native_type
  @model.connection.native_database_types[type] or {}
end