lib/rails_erd/relationship.rb in rails-erd-0.1.0 vs lib/rails_erd/relationship.rb in rails-erd-0.1.1
- old
+ new
@@ -1,6 +1,10 @@
module RailsERD
+ # Describes a relationship between two entities. A relationship is detected
+ # based on Active Record associations. One relationship may represent more
+ # than one association, however. Associations that share the same foreign
+ # key are grouped together.
class Relationship
class << self
def from_associations(domain, associations) #:nodoc:
assoc_groups = associations.group_by { |assoc| association_identity(assoc) }
assoc_groups.collect { |_, assoc_group| Relationship.new(domain, assoc_group.to_a) }
@@ -12,20 +16,19 @@
identifier = assoc.options[:join_table] || assoc.primary_key_name.to_s
Set[identifier, assoc.active_record, assoc.klass]
end
end
- # Returns the domain in which this relationship is defined.
+ # The domain in which this relationship is defined.
attr_reader :domain
- # Returns the source entity. The source entity corresponds to the model
- # that has defined a +has_one+ or +has_many+ association with the other
- # model.
+ # The source entity. It corresponds to the model that has defined a
+ # +has_one+ or +has_many+ association with the other model.
attr_reader :source
- # Returns the destination entity. The destination entity corresponds to the
- # model that has defined a +belongs_to+ association with the other model.
+ # The destination entity. It corresponds to the model that has defined
+ # a +belongs_to+ association with the other model.
attr_reader :destination
def initialize(domain, associations) #:nodoc:
@domain = domain
@reverse_associations, @forward_associations = *associations.partition(&:belongs_to?)
@@ -33,10 +36,10 @@
assoc = @forward_associations.first || @reverse_associations.first
@source, @destination = @domain.entity_for(assoc.active_record), @domain.entity_for(assoc.klass)
@source, @destination = @destination, @source if assoc.belongs_to?
end
- # Returns all +ActiveRecord+ association objects that describe this
+ # Returns all Active Record association objects that describe this
# relationship.
def associations
@forward_associations + @reverse_associations
end