database_validation 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/database_validation/version.rb +1 -1
- data/lib/database_validation.rb +13 -1
- metadata +2 -2
data/lib/database_validation.rb
CHANGED
@@ -14,8 +14,20 @@ module DatabaseValidation
|
|
14
14
|
base.columns.each do |field|
|
15
15
|
next if field.name == base.primary_key # don't add validations here
|
16
16
|
allow_nil = field.null
|
17
|
-
|
17
|
+
|
18
18
|
base.send(:validates_presence_of, field.name.to_sym, :allow_nil => allow_nil) unless allow_nil
|
19
|
+
next if field.limit.nil?
|
20
|
+
|
21
|
+
if [:integer, :float].include?(field.type)
|
22
|
+
unsigned = field.sql_type.include?('unsigned')
|
23
|
+
maximum = unsigned ? 2 ** (8 * field.limit) : 2 ** (8 * field.limit) / 2
|
24
|
+
minimum = unsigned ? -(2 ** (8 * field.limit)) - 1 : -(2 ** (8 * field.limit) / 2) - 1
|
25
|
+
base.send(:validates_numericality_of, field.name.to_sym, :less_than => maximum, :greater_than => minimum, :allow_nil => allow_nil)
|
26
|
+
else
|
27
|
+
maximum = field.limit
|
28
|
+
base.send(:validates_length_of, field.name.to_sym, :maximum => maximum, :allow_nil => allow_nil)
|
29
|
+
end
|
30
|
+
|
19
31
|
end
|
20
32
|
ActiveRecord::Base.connection.indexes(base.table_name).each do |key|
|
21
33
|
col_syms = key.columns.map(&:to_sym)
|