class RailsERD::Domain::Entity
Entities represent your Active Record models. Entities may be connected to other entities.
Attributes
The domain in which this entity resides.
The Active Record model that this entity corresponds to.
The name of this entity. Equal to the class name of the corresponding model (for concrete entities) or given name (for abstract entities).
Private Class Methods
# File lib/rails_erd/domain/entity.rb, line 17 def abstract_from_models(domain, models) models.collect(&:reflect_on_all_associations).flatten.collect { |association| association.options[:as].to_s.classify if association.options[:as] }.flatten.compact.uniq.collect { |name| new(domain, name) } end
# File lib/rails_erd/domain/entity.rb, line 13 def concrete_from_models(domain, models) models.collect { |model| new(domain, model.name, model) } end
Public Instance Methods
Returns an array of attributes for this entity.
# File lib/rails_erd/domain/entity.rb, line 42 def attributes @attributes ||= generalized? ? [] : Attribute.from_model(domain, model) end
Returns all child entities, if this is a generalized entity.
# File lib/rails_erd/domain/entity.rb, line 91 def children @children ||= domain.specializations_by_entity_name(name).map(&:specialized) end
Returns true
if this entity has any relationships with other models, false
otherwise.
# File lib/rails_erd/domain/entity.rb, line 54 def connected? relationships.any? end
Returns true
if this entity has no relationships with any other models, false
otherwise. Opposite of connected?
.
# File lib/rails_erd/domain/entity.rb, line 60 def disconnected? relationships.none? end
Returns true
if this entity is a generalization, which does not correspond with a database table. Generalized entities are either models that are defined as abstract_class
or they are constructed from polymorphic interfaces. Any has_one
or has_many
association that defines a polymorphic interface with :as => :name
will lead to a generalized entity to be created.
# File lib/rails_erd/domain/entity.rb, line 70 def generalized? !model or !!model.abstract_class? end
# File lib/rails_erd/domain/entity.rb, line 95 def namespace $1 if name.match(/(.*)::.*/) end
Returns an array of all relationships that this entity has with other entities in the domain model.
# File lib/rails_erd/domain/entity.rb, line 48 def relationships domain.relationships_by_entity_name(name) end
Returns true
if this entity descends from another entity, and is represented in the same table as its parent. In Rails this concept is referred to as single-table inheritance. In entity-relationship diagrams it is called specialization.
# File lib/rails_erd/domain/entity.rb, line 78 def specialized? !!model and !model.descends_from_active_record? end
Returns true
if this entity does not correspond directly with a database table (if and only if the entity is specialized or generalized).
# File lib/rails_erd/domain/entity.rb, line 85 def virtual? generalized? or specialized? end