Class: AnnotateRb::ModelAnnotator::ForeignKeyAnnotation::ForeignKeyComponentBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(foreign_key, options) ⇒ ForeignKeyComponentBuilder

Returns a new instance of ForeignKeyComponentBuilder.



9
10
11
12
# File 'lib/annotate_rb/model_annotator/foreign_key_annotation/foreign_key_component_builder.rb', line 9

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

Instance Attribute Details

#foreign_keyObject (readonly)

Returns the value of attribute foreign_key.



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

def foreign_key
  @foreign_key
end

Instance Method Details

#constraints_infoObject



30
31
32
33
34
35
36
37
# File 'lib/annotate_rb/model_annotator/foreign_key_annotation/foreign_key_component_builder.rb', line 30

def constraints_info
  @constraints_info ||= begin
    constraints_info = ""
    constraints_info += "ON DELETE => #{foreign_key.on_delete} " if foreign_key.on_delete
    constraints_info += "ON UPDATE => #{foreign_key.on_update} " if foreign_key.on_update
    constraints_info.strip
  end
end

#formatted_nameObject



14
15
16
17
18
19
20
# File 'lib/annotate_rb/model_annotator/foreign_key_annotation/foreign_key_component_builder.rb', line 14

def formatted_name
  @formatted_name ||= if foreign_key.name.blank?
    foreign_key.column
  else
    @options[:show_complete_foreign_keys] ? foreign_key.name : foreign_key.name.gsub(/(?<=^fk_rails_)[0-9a-f]{10}$/, "...")
  end
end

#ref_infoObject



39
40
41
42
43
44
45
# File 'lib/annotate_rb/model_annotator/foreign_key_annotation/foreign_key_component_builder.rb', line 39

def ref_info
  if foreign_key.column.is_a?(Array) # Composite foreign key using multiple columns
    "#{stringified_columns} => #{foreign_key.to_table}#{stringified_primary_key}"
  else
    "#{foreign_key.column} => #{foreign_key.to_table}.#{foreign_key.primary_key}"
  end
end

#stringified_columnsObject



22
23
24
# File 'lib/annotate_rb/model_annotator/foreign_key_annotation/foreign_key_component_builder.rb', line 22

def stringified_columns
  @stringified_columns ||= stringify(foreign_key.column)
end

#stringified_primary_keyObject



26
27
28
# File 'lib/annotate_rb/model_annotator/foreign_key_annotation/foreign_key_component_builder.rb', line 26

def stringified_primary_key
  @stringified_primary_key ||= stringify(foreign_key.primary_key)
end