Class: AnnotateRb::ModelAnnotator::ColumnAnnotation::AttributesBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/annotate_rb/model_annotator/column_annotation/attributes_builder.rb

Constant Summary collapse

NO_DEFAULT_COL_TYPES =

Don’t show default value for these column types

%w[json jsonb hstore].freeze

Instance Method Summary collapse

Constructor Details

#initialize(column, options, is_primary_key, column_indices, column_defaults) ⇒ AttributesBuilder

Returns a new instance of AttributesBuilder.



10
11
12
13
14
15
# File 'lib/annotate_rb/model_annotator/column_annotation/attributes_builder.rb', line 10

def initialize(column, options, is_primary_key, column_indices, column_defaults)
  @column = ColumnWrapper.new(column, column_defaults, options)
  @options = options
  @is_primary_key = is_primary_key
  @column_indices = column_indices
end

Instance Method Details

#buildObject

Get the list of attributes that should be included in the annotation for a given column.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/annotate_rb/model_annotator/column_annotation/attributes_builder.rb', line 19

def build
  column_type = @column.column_type_string
  attrs = []

  if !@column.raw_default.nil? && !hide_default?
    schema_default = "default(#{@column.default_string})"

    attrs << schema_default
  end

  if @column.unsigned?
    attrs << "unsigned"
  end

  if !@column.null
    attrs << "not null"
  end

  if @is_primary_key
    attrs << "primary key"
  end

  is_special_type = %w[spatial geometry geography].include?(column_type)
  is_decimal_type = column_type == "decimal"

  if !is_decimal_type && !is_special_type
    if @column.limit && !@options[:format_yard]
      if @column.limit.is_a?(Array)
        attrs << "(#{@column.limit.join(", ")})"
      end
    end
  end

  # Check out if we got an array column
  if @column.array?
    attrs << "is an Array"
  end

  # Check out if we got a geometric column
  # and print the type and SRID
  if @column.geometry_type?
    attrs << "#{@column.geometry_type}, #{@column.srid}"
  elsif @column.geometric_type? && @column.geometric_type.present?
    attrs << "#{@column.geometric_type.to_s.downcase}, #{@column.srid}"
  end

  # Check if the column has indices and print "indexed" if true
  # If the index includes another column, print it too.
  if @options[:simple_indexes]
    # Note: there used to be a klass.table_exists? call here, but removed it as it seemed unnecessary.

    sorted_column_indices&.each do |index|
      indexed_columns = index.columns.reject { |i| i == @column.name }

      index_text = if index.unique
        "uniquely indexed"
      else
        "indexed"
      end

      attrs << if indexed_columns.empty?
        index_text
      else
        "#{index_text} => [#{indexed_columns.join(", ")}]"
      end
    end
  end

  if column_type == "enum" && @options[:show_enums]
    enum_type_name = @column.sql_type
    if enum_type_name.present? && enum_type_name != "enum"
      attrs << "enum_type: #{enum_type_name}"
    end
  end

  # Check if the column is a virtual column and print the function
  if @options[:show_virtual_columns] && @column.virtual?
    # Any whitespace in the function gets reduced to a single space
    attrs << @column.default_function.gsub(/\s+/, " ").strip
  end

  attrs
end

#hide_default?Boolean

Historically, the old gem looked for the option being set to “skip” e.g. hide_default_column_types: “skip”

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
# File 'lib/annotate_rb/model_annotator/column_annotation/attributes_builder.rb', line 112

def hide_default?
  excludes =
    if @options[:hide_default_column_types].blank?
      NO_DEFAULT_COL_TYPES
    else
      @options[:hide_default_column_types].split(",")
    end

  excludes.include?(@column.column_type_string)
end

#sorted_column_indicesObject



103
104
105
106
107
108
# File 'lib/annotate_rb/model_annotator/column_annotation/attributes_builder.rb', line 103

def sorted_column_indices
  # Not sure why there were & safe accessors here, but keeping in for time being.
  sorted_indices = @column_indices&.sort_by(&:name)

  _sorted_indices = sorted_indices.reject { |ind| ind.columns.is_a?(String) }
end