lib/rails_erd/attribute.rb in rails-erd-0.1.0 vs lib/rails_erd/attribute.rb in rails-erd-0.1.1
- old
+ new
@@ -1,7 +1,9 @@
# -*- encoding: utf-8
module RailsERD
+ # Describes an entity's attribute. Attributes correspond directly to
+ # database columns.
class Attribute
TIMESTAMP_NAMES = %w{created_at created_on updated_at updated_on} #:nodoc:
class << self
def from_model(domain, model) #:nodoc:
@@ -9,34 +11,46 @@
end
end
attr_reader :column #:nodoc:
- def initialize(domain, model, column)
+ def initialize(domain, model, column) #:nodoc:
@domain, @model, @column = domain, model, column
end
+ # The name of the attribute, equal to the column name.
def name
column.name
end
+ # The type of the attribute, equal to the Rails migration type. Can be any
+ # of +:string+, +:integer+, +:boolean+, +:text+, etc.
def type
column.type
end
+ # Returns +true+ if this attribute is mandatory. Mandatory attributes
+ # either have a presence validation (+validates_presence_of+), or have a
+ # <tt>NOT NULL</tt> database constraint.
def mandatory?
!column.null or @model.validators_on(name).map(&:kind).include?(:presence)
end
+ # Returns +true+ if this attribute is the primary key of the entity.
def primary_key?
@model.arel_table.primary_key == name
end
+ # Returns +true+ if this attribute is used as a foreign key for any
+ # relationship.
def foreign_key?
@domain.relationships_for(@model).map(&:associations).flatten.map(&:primary_key_name).include?(name)
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+.
def timestamp?
TIMESTAMP_NAMES.include? name
end
def <=>(other) #:nodoc:
@@ -49,9 +63,17 @@
def to_s #:nodoc:
name
end
+ # Returns a short description of the attribute type. If the attribute has
+ # a non-standard limit or if it is mandatory, this information is included.
+ #
+ # Example output:
+ # <tt>:integer</tt>:: int
+ # <tt>:string, :limit => 255</tt>:: str
+ # <tt>:string, :limit => 128</tt>:: str (128)
+ # <tt>:boolean, :null => false</tt>:: bool *
def type_description
case type
when :integer then "int"
when :float then "float"
when :decimal then "dec"