Class RailsERD::Domain::Relationship

  1. lib/rails_erd/domain/relationship/cardinality.rb
  2. lib/rails_erd/domain/relationship.rb
  3. show all
Parent: Object

Describes a relationship between two entities. A relationship is detected based on Active Record associations. One relationship may represent more than one association, however. Related associations are grouped together. Associations are related if they share the same foreign key, or the same join table in the case of many-to-many associations.

Constants

N = Cardinality::N

Attributes

destination [R] The destination entity. It corresponds to the model that has defined a belongs_to association with the other model.
domain [R] The domain in which this relationship is defined.
source [R] The source entity. It corresponds to the model that has defined a has_one or has_many association with the other model.

Public instance methods

associations ()

Returns all Active Record association objects that describe this relationship.

[show source]
# File lib/rails_erd/domain/relationship.rb, line 73
      def associations
        @forward_associations + @reverse_associations
      end
cardinality ()

Returns the cardinality of this relationship.

[show source]
# File lib/rails_erd/domain/relationship.rb, line 78
      def cardinality
        @cardinality ||= begin
          reverse_max = any_habtm?(associations) ? N : 1
          forward_range = associations_range(@forward_associations, N)
          reverse_range = associations_range(@reverse_associations, reverse_max)
          Cardinality.new(reverse_range, forward_range)
        end
      end
indirect? ()

Indicates if a relationship is indirect, that is, if it is defined through other relationships. Indirect relationships are created in Rails with has_many :through or has_one :through association macros.

[show source]
# File lib/rails_erd/domain/relationship.rb, line 91
      def indirect?
        !@forward_associations.empty? and @forward_associations.all?(&:through_reflection)
      end
many_to? ()

Indicates whether the source cardinality class of this relationship is equal to infinity. This is true for many-to-many relationships only.

[show source]
# File lib/rails_erd/domain/relationship.rb, line 129
      def many_to?
        cardinality.cardinality_class[0] != 1
      end
mutual? ()

Indicates whether or not the relationship is defined by two inverse associations (e.g. a has_many and a corresponding belongs_to association).

[show source]
# File lib/rails_erd/domain/relationship.rb, line 98
      def mutual?
        @forward_associations.any? and @reverse_associations.any?
      end
one_to? ()

Indicates whether the source cardinality class of this relationship is equal to one. This is true for one-to-one or one-to-many relationships only.

[show source]
# File lib/rails_erd/domain/relationship.rb, line 123
      def one_to?
        cardinality.cardinality_class[0] == 1
      end
recursive? ()

Indicates whether or not this relationship connects an entity with itself.

[show source]
# File lib/rails_erd/domain/relationship.rb, line 103
      def recursive?
        @source == @destination
      end
strength ()

The strength of a relationship is equal to the number of associations that describe it.

[show source]
# File lib/rails_erd/domain/relationship.rb, line 135
      def strength
        if source.generalized? then 1 else associations.size end
      end
to_many? ()

Indicates whether the destination cardinality class of this relationship is equal to infinity. This is true for one-to-many or many-to-many relationships only.

[show source]
# File lib/rails_erd/domain/relationship.rb, line 116
      def to_many?
        cardinality.cardinality_class[1] != 1
      end
to_one? ()

Indicates whether the destination cardinality class of this relationship is equal to one. This is true for one-to-one relationships only.

[show source]
# File lib/rails_erd/domain/relationship.rb, line 109
      def to_one?
        cardinality.cardinality_class[1] == 1
      end