Class: MermaidRailsErd::PolymorphicTargetsResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/mermaid_rails_erd/polymorphic_targets_resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_data_collector) ⇒ PolymorphicTargetsResolver

Returns a new instance of PolymorphicTargetsResolver.



9
10
11
# File 'lib/mermaid_rails_erd/polymorphic_targets_resolver.rb', line 9

def initialize(model_data_collector)
  @model_data_collector = model_data_collector
end

Instance Attribute Details

#model_data_collectorObject (readonly)

Returns the value of attribute model_data_collector.



7
8
9
# File 'lib/mermaid_rails_erd/polymorphic_targets_resolver.rb', line 7

def model_data_collector
  @model_data_collector
end

Instance Method Details

#resolve(name, from_table, rel_type) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mermaid_rails_erd/polymorphic_targets_resolver.rb', line 13

def resolve(name, from_table, rel_type)
  # Get all models that implement the polymorphic interface
  target_models = model_data_collector.polymorphic_targets_for(name)

  # Create relationships for each target model
  target_models.map do |target|
    fk_column = "#{name}_id"
    Relationship.new(
      from_table,
      target.table_name,
      fk_column,
      rel_type,
      nil, # Let the Relationship generate the label
      from_table, # fk_table
      fk_column, # fk_column
      target.table_name, # pk_table
      "id", # pk_column
      # Add (polymorphic) to the label
      true, # is_polymorphic
      "polymorphic", # extra_label
    )
  end
end