lib/rails_erd/domain/attribute.rb in rails-erd-1.3.0 vs lib/rails_erd/domain/attribute.rb in rails-erd-1.3.1
- old
+ new
@@ -8,11 +8,24 @@
class Attribute
TIMESTAMP_NAMES = %w{created_at created_on updated_at updated_on} # @private :nodoc:
class << self
def from_model(domain, model) # @private :nodoc:
- model.columns.collect { |column| new(domain, model, column) }.sort
+ attributes = model.columns.collect { |column| new(domain, model, column) }
+ attributes.sort! if RailsERD.options[:sort]
+
+ if RailsERD.options[:prepend_primary]
+ primary_key = ActiveRecord::Base.get_primary_key(model)
+ primary = attributes.detect{ |column| column.name == primary_key }
+
+ if primary
+ attributes.delete(primary)
+ attributes.unshift(primary)
+ end
+ end
+
+ attributes
end
end
extend Inspectable
inspection_attributes :name, :type
@@ -45,10 +58,14 @@
# <tt>NOT NULL</tt> database constraint.
def mandatory?
!column.null or @model.validators_on(name).map(&:kind).include?(:presence)
end
+ def unique?
+ @model.validators_on(name).map(&:kind).include?(:uniqueness)
+ end
+
# Returns +true+ if this attribute is the primary key of the entity.
def primary_key?
@model.primary_key.to_s == name.to_s
end
@@ -91,10 +108,13 @@
# <tt>:decimal, :precision => 5, :scale => 2/tt>:: decimal (5,2)
# <tt>:boolean, :null => false</tt>:: boolean *
def type_description
type.to_s.tap do |desc|
desc << " #{limit_description}" if limit_description
- desc << " ∗" if mandatory? # Add a hair space + low asterisk (Unicode characters).
+ 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
# Returns any non-standard limit for this attribute. If a column has no
# limit or uses a default database limit, this method returns +nil+.