lib/rails_erd/domain/relationship.rb in rails-erd-1.3.1 vs lib/rails_erd/domain/relationship.rb in rails-erd-1.4.0

- old
+ new

@@ -61,21 +61,14 @@ delegate :one_to_one?, :one_to_many?, :many_to_many?, :source_optional?, :destination_optional?, :to => :cardinality def initialize(domain, associations) # @private :nodoc: @domain = domain - @reverse_associations, @forward_associations = *unless any_habtm?(associations) - associations.partition(&:belongs_to?) - else - # Many-to-many associations don't have a clearly defined direction. - # We sort by name and use the first model as the source. - source = associations.map(&:active_record).sort_by(&:name).first - associations.partition { |association| association.active_record != source } - end + @reverse_associations, @forward_associations = partition_associations(associations) assoc = @forward_associations.first || @reverse_associations.first - @source = @domain.entity_by_name(self.class.send(:association_owner, assoc)) + @source = @domain.entity_by_name(self.class.send(:association_owner, assoc)) @destination = @domain.entity_by_name(self.class.send(:association_target, assoc)) @source, @destination = @destination, @source if assoc.belongs_to? end # Returns all Active Record association objects that describe this @@ -149,9 +142,20 @@ def <=>(other) # @private :nodoc: (source.name <=> other.source.name).nonzero? or (destination.name <=> other.destination.name) end private + + def partition_associations(associations) + if any_habtm?(associations) + # Many-to-many associations don't have a clearly defined direction. + # We sort by name and use the first model as the source. + source = associations.map(&:active_record).sort_by(&:name).first + associations.partition { |association| association.active_record != source } + else + associations.partition(&:belongs_to?) + end + end def associations_range(associations, absolute_max) # The minimum of the range is the maximum value of each association # minimum. If there is none, it is zero by definition. The reasoning is # that from all associations, if only one has a required minimum, then