Class: AnnotateRb::ModelAnnotator::ForeignKeyAnnotation::AnnotationBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/annotate_rb/model_annotator/foreign_key_annotation/annotation_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, options) ⇒ AnnotationBuilder

Returns a new instance of AnnotationBuilder.



7
8
9
10
# File 'lib/annotate_rb/model_annotator/foreign_key_annotation/annotation_builder.rb', line 7

def initialize(model, options)
  @model = model
  @options = options
end

Instance Method Details

#buildObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/annotate_rb/model_annotator/foreign_key_annotation/annotation_builder.rb', line 12

def build
  return Components::NilComponent.new if !@options[:show_foreign_keys]
  return Components::NilComponent.new unless @model.connection.respond_to?(:supports_foreign_keys?) &&
    @model.connection.supports_foreign_keys? && @model.connection.respond_to?(:foreign_keys)

  foreign_keys = @model.connection.foreign_keys(@model.table_name)
  return Components::NilComponent.new if foreign_keys.empty?

  fks = foreign_keys.map do |fk|
    ForeignKeyComponentBuilder.new(fk, @options)
  end

  max_size = fks.map(&:formatted_name).map(&:size).max + 1

  foreign_key_components = fks.sort_by { |fk| [fk.formatted_name, fk.stringified_columns] }.map do |fk|
    # fk is a ForeignKeyComponentBuilder

    ForeignKeyComponent.new(fk.formatted_name, fk.constraints_info, fk.ref_info, max_size)
  end

  _annotation = Annotation.new(foreign_key_components)
end