Class: AnnotateRb::ModelAnnotator::ForeignKeyAnnotation::AnnotationBuilder
- Inherits:
-
Object
- Object
- AnnotateRb::ModelAnnotator::ForeignKeyAnnotation::AnnotationBuilder
- Defined in:
- lib/annotate_rb/model_annotator/foreign_key_annotation/annotation_builder.rb
Instance Method Summary collapse
- #build ⇒ Object
-
#initialize(model, options) ⇒ AnnotationBuilder
constructor
A new instance of AnnotationBuilder.
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, ) @model = model @options = end |
Instance Method Details
#build ⇒ Object
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 |