5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
|
# File 'lib/validates_by_schema.rb', line 5
def validates_by_schema options={}
return unless table_exists?
columns = self.columns.reject(&:primary)
columns.reject!{|c| ['updated_at', 'created_at'].include? c.name}
except = options.delete(:except).try(:map, &:to_s)
only = options.delete(:only).try(:map, &:to_s)
if only.present?
columns.select!{|c| only.include? c.name}
elsif except.present?
columns.reject!{|c| except.include? c.name}
end
columns.each do |c|
case c.type
when :integer
val_hash = {presence: !c.null, numericality: {
only_integer: true, allow_nil: c.null}}
if c.limit
max = (2 ** (8 * c.limit)) / 2
val_hash[:numericality][:less_than] = max
val_hash[:numericality][:greater_than] = -max
end
validates c.name, val_hash
when :decimal
maximum = 10.0**(c.precision-c.scale) - 10.0**(-c.scale)
validates c.name, presence: !c.null, numericality: {
allow_nil: c.null, greater_than_or_equal_to: -maximum,
less_than_or_equal_to: maximum }
when :float
validates c.name, presence: !c.null,
numericality: { allow_nil: c.null }
when :string, :text
validates c.name, presence: !c.null, length: { maximum: c.limit.to_i,
allow_nil: c.null}
when :boolean
validates c.name, inclusion: { in: [true, false],
allow_nil: c.null }
when :date, :datetime, :time
validates c.name, presence: !c.null
end
end
end
|