Class: GeneratedSchemaValidations::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/generated_schema_validations/table.rb

Defined Under Namespace

Classes: Validation, ValidationWithoutEnum

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, &block) ⇒ Table

Returns a new instance of Table.



18
19
20
21
22
23
24
25
26
27
# File 'lib/generated_schema_validations/table.rb', line 18

def initialize(table_name, &block)
  @table_name = table_name
  @column_names = []
  @possible_belongs_to_not_null_columns = []
  @bad_indexes = []
  @unique_indexes = []
  @validations = []

  instance_eval(&block)
end

Instance Attribute Details

#table_nameObject (readonly)

Returns the value of attribute table_name.



16
17
18
# File 'lib/generated_schema_validations/table.rb', line 16

def table_name
  @table_name
end

Instance Method Details

#bigint(name, column_options = {}) ⇒ Object



75
76
77
78
79
# File 'lib/generated_schema_validations/table.rb', line 75

def bigint(name, column_options = {})
  null_validation(:bigint, name, column_options)

  validates_without_enum name, :numericality, allow_nil: true
end

#binary(name, column_options = {}) ⇒ Object



116
117
118
# File 'lib/generated_schema_validations/table.rb', line 116

def binary(name, column_options = {})
  null_validation(:binary, name, column_options)
end

#boolean(name, column_options = {}) ⇒ Object



112
113
114
# File 'lib/generated_schema_validations/table.rb', line 112

def boolean(name, column_options = {})
  null_validation(:boolean, name, column_options)
end

#date(name, column_options = {}) ⇒ Object



102
103
104
105
# File 'lib/generated_schema_validations/table.rb', line 102

def date(name, column_options = {})
  null_validation(:date, name, column_options)
  validates name, :date_in_db_range
end

#datetime(name, column_options = {}) ⇒ Object



97
98
99
100
# File 'lib/generated_schema_validations/table.rb', line 97

def datetime(name, column_options = {})
  null_validation(:datetime, name, column_options)
  validates name, :date_time_in_db_range
end

#decimal(name, column_options = {}) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/generated_schema_validations/table.rb', line 136

def decimal(name, column_options = {})
  null_validation(:decimal, name, column_options)
  return if column_options[:array]
  return if column_options[:precision].blank? || column_options[:scale].blank?

  limit = 10**(column_options[:precision] - (column_options[:scale] || 0))
  validates name, :numericality, allow_nil: true, greater_than: -limit, less_than: limit
end

#float(name, column_options = {}) ⇒ Object



145
146
147
148
149
150
# File 'lib/generated_schema_validations/table.rb', line 145

def float(name, column_options = {})
  null_validation(:float, name, column_options)
  return if column_options[:array]

  validates name, :numericality, allow_nil: true
end

#geographyObject



172
173
174
# File 'lib/generated_schema_validations/table.rb', line 172

def geography(*)
  # do nothing
end

#index(names, index_options = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/generated_schema_validations/table.rb', line 160

def index(names, index_options = {})
  names = [names] unless names.is_a?(Array)
  return unless index_options[:unique]
  return unless names.all? { |name| name.to_s.in?(@column_names) }

  if index_options[:where]
    @bad_indexes.push(names.map(&:to_s))
  else
    @unique_indexes.push(names.map(&:to_s))
  end
end

#integer(name, column_options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/generated_schema_validations/table.rb', line 81

def integer(name, column_options = {})
  null_validation(:integer, name, column_options)

  return if column_options[:array]

  integer_range = ::ActiveRecord::Type::Integer.new.send(:range)
  options = { allow_nil: true, only_integer: true, greater_than_or_equal_to: integer_range.begin }
  if integer_range.exclude_end?
    options[:less_than] = integer_range.end
  else
    options[:less_than_or_equal_to] = integer_range.end
  end

  validates_without_enum name, :numericality, options
end

#json(name, column_options = {}) ⇒ Object



124
125
126
# File 'lib/generated_schema_validations/table.rb', line 124

def json(name, column_options = {})
  null_validation(:json, name, column_options)
end

#jsonb(name, column_options = {}) ⇒ Object



128
129
130
# File 'lib/generated_schema_validations/table.rb', line 128

def jsonb(name, column_options = {})
  null_validation(:jsonb, name, column_options)
end

#null_validation(datatype, name, column_options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/generated_schema_validations/table.rb', line 56

def null_validation(datatype, name, column_options)
  @column_names.push(name.to_s)

  return if column_options[:null] != false

  @possible_belongs_to_not_null_columns.push(name.to_sym) if datatype.in?(%i[bigint integer uuid])
  if datatype == :boolean
    validates name, :inclusion, in: [true, false], message: :blank
  elsif datatype.in?(%i[json jsonb])
    validates name, :exclusion, in: [nil], message: :blank
  else
    validates name, :presence
  end
end

#string(name, column_options = {}) ⇒ Object



120
121
122
# File 'lib/generated_schema_validations/table.rb', line 120

def string(name, column_options = {})
  text(name, column_options)
end

#text(name, column_options = {}) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/generated_schema_validations/table.rb', line 152

def text(name, column_options = {})
  null_validation(:text, name, column_options)
  return if column_options[:array]
  return if column_options[:limit].blank?

  validates name, :length, allow_nil: true, maximum: column_options[:limit]
end

#time(name, column_options = {}) ⇒ Object



107
108
109
110
# File 'lib/generated_schema_validations/table.rb', line 107

def time(name, column_options = {})
  null_validation(:time, name, column_options)
  validates name, :time_in_db_range
end

#to_sObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/generated_schema_validations/table.rb', line 29

def to_s
  # sort everything
  @possible_belongs_to_not_null_columns.sort!
  @bad_indexes.sort!
  @unique_indexes.sort!
  @validations.sort_by!(&:to_s)

  string = "\n"
  string += "def dbv_#{table_name}_validations(enums: [])\n"
  if @possible_belongs_to_not_null_columns.present?
    string += "  belongs_to_presence_validations_for(#{@possible_belongs_to_not_null_columns.inspect})\n"
  end
  string += "  bad_uniqueness_validations_for(#{@bad_indexes.inspect})\n" if @bad_indexes.present?
  string += "  belongs_to_uniqueness_validations_for(#{@unique_indexes.inspect})\n" if @unique_indexes.present?
  string += "  uniqueness_validations_for(#{@unique_indexes.inspect})\n" if @unique_indexes.present?
  string += @validations.uniq.map { |v| "  #{v}\n" }.join
  "#{string}end\n"
end

#uuid(name, column_options = {}) ⇒ Object



71
72
73
# File 'lib/generated_schema_validations/table.rb', line 71

def uuid(name, column_options = {})
  null_validation(:uuid, name, column_options)
end

#validates(attribute, validator, options = {}) ⇒ Object



48
49
50
# File 'lib/generated_schema_validations/table.rb', line 48

def validates(attribute, validator, options = {})
  @validations.push(Validation.new(attribute, validator, options))
end

#validates_without_enum(attribute, validator, options = {}) ⇒ Object



52
53
54
# File 'lib/generated_schema_validations/table.rb', line 52

def validates_without_enum(attribute, validator, options = {})
  @validations.push(ValidationWithoutEnum.new(attribute, validator, options))
end

#xml(name, column_options = {}) ⇒ Object



132
133
134
# File 'lib/generated_schema_validations/table.rb', line 132

def xml(name, column_options = {})
  null_validation(:xml, name, column_options)
end