# File lib/rails_erd/domain/attribute.rb, line 73 def primary_key? @model.primary_key.to_s == name.to_s end
class RailsERD::Domain::Attribute
Describes an entity's attribute. Attributes correspond directly to database columns.
Public Class Methods
# File lib/rails_erd/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 } if primary attributes[primary], attributes[0] = attributes[0], attributes[primary] end attributes end
Public Instance Methods
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/rails_erd/domain/attribute.rb, line 57 def content? !primary_key? and !foreign_key? and !timestamp? and !inheritance? end
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/rails_erd/domain/attribute.rb, line 93 def false? false end
Returns true
if this attribute is used as a foreign key for any relationship.
# File lib/rails_erd/domain/attribute.rb, line 79 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
Returns true
if this attribute is used for single table inheritance. These attributes are typically named type
.
# File lib/rails_erd/domain/attribute.rb, line 87 def inheritance? @model.inheritance_column == name end
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/rails_erd/domain/attribute.rb, line 133 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
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/rails_erd/domain/attribute.rb, line 64 def mandatory? !column.null or @model.validators_on(name).map(&:kind).include?(:presence) end
The name of the attribute, equal to the column name.
# File lib/rails_erd/domain/attribute.rb, line 45 def name column.name end
Returns true
if this attribute is the primary key of the entity.
Returns any non-standard scale for this attribute (decimal types only).
# File lib/rails_erd/domain/attribute.rb, line 140 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
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/rails_erd/domain/attribute.rb, line 100 def timestamp? TIMESTAMP_NAMES.include? name end
The type of the attribute, equal to the Rails migration type. Can be any of :string
, :integer
, :boolean
, :text
, etc.
# File lib/rails_erd/domain/attribute.rb, line 51 def type column.type or column.sql_type.downcase.to_sym end
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/rails_erd/domain/attribute.rb, line 121 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
# File lib/rails_erd/domain/attribute.rb, line 68 def unique? @model.validators_on(name).map(&:kind).include?(:uniqueness) end
Private Instance Methods
# File lib/rails_erd/domain/attribute.rb, line 155 def native_type @model.connection.native_database_types[type] or {} end