Class: GeneratedSchemaValidations::Table

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

Defined Under Namespace

Classes: Validation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, &block) ⇒ Table

Returns a new instance of Table.



12
13
14
15
16
17
18
19
20
21
# File 'lib/generated_schema_validations/table.rb', line 12

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.



10
11
12
# File 'lib/generated_schema_validations/table.rb', line 10

def table_name
  @table_name
end

Instance Method Details

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



57
58
59
60
61
# File 'lib/generated_schema_validations/table.rb', line 57

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

  validates name, :numericality, allow_nil: true
end

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



93
94
95
# File 'lib/generated_schema_validations/table.rb', line 93

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

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



89
90
91
# File 'lib/generated_schema_validations/table.rb', line 89

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

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



84
85
86
87
# File 'lib/generated_schema_validations/table.rb', line 84

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

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



79
80
81
82
# File 'lib/generated_schema_validations/table.rb', line 79

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

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



113
114
115
116
117
118
119
120
# File 'lib/generated_schema_validations/table.rb', line 113

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



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

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

  validates name, :numericality, allow_nil: true
end

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



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/generated_schema_validations/table.rb', line 137

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 defined?(Rails::Railtie) && (Rails.env.development? || Rails.env.test?) && 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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/generated_schema_validations/table.rb', line 63

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 name, :numericality, options
end

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



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

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

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



105
106
107
# File 'lib/generated_schema_validations/table.rb', line 105

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

#null_validation(datatype, name, column_options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/generated_schema_validations/table.rb', line 40

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
  else
    validates name, :presence
  end
end

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



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

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

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



129
130
131
132
133
134
135
# File 'lib/generated_schema_validations/table.rb', line 129

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

#to_sObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/generated_schema_validations/table.rb', line 23

def to_s
  string = "\n"
  string += "def dbv_#{table_name}_validations\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



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

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

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



36
37
38
# File 'lib/generated_schema_validations/table.rb', line 36

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

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



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

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