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.
@@ -6,6 +6,6 @@ module DatabaseValidation
6
6
 
7
7
  private
8
8
 
9
- VERSION = [0, 0, 2]
9
+ VERSION = [0, 0, 3]
10
10
 
11
11
  end
@@ -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
- base.send(:validates_length_of, field.name.to_sym, :maximum => field.limit, :allow_nil => allow_nil) unless field.limit.nil?
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)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Crepezzi