Class: Diagrammer::RelationshipMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/diagrammer/relationship_mapper.rb

Overview

Turns ActiveRecord reflections into the table-to-table edges the renderer draws. Every edge is oriented from the table holding the foreign key to the table it references, so the renderer can anchor the line on the FK column.

Constant Summary collapse

ASSOCIATION_MACROS =
%i[belongs_to has_one has_many has_and_belongs_to_many].freeze
MACRO_STRENGTH =

Ordered weakest-to-strongest. When a belongs_to and its inverse collapse onto one edge, the strongest macro in the group decides the cardinality.

%i[belongs_to has_one has_many has_and_belongs_to_many].freeze

Instance Method Summary collapse

Constructor Details

#initialize(models:) ⇒ RelationshipMapper

Returns a new instance of RelationshipMapper.



16
17
18
19
# File 'lib/diagrammer/relationship_mapper.rb', line 16

def initialize(models:)
  @models = models
  @table_names = models.to_set(&:table_name)
end

Instance Method Details

#callObject



21
22
23
24
# File 'lib/diagrammer/relationship_mapper.rb', line 21

def call
  candidates = @models.flat_map { |model| model_relationships(model) }
  merge_reciprocals(candidates)
end