Class: ActiveRecord::Base

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

Class Method Summary collapse

Class Method Details

.inherited(klass) ⇒ Object



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
# File 'lib/validates_schema/validates_schema.rb', line 5

def inherited(klass)
  super(klass)
  # puts klass
  klass.arel_table.columns.each do |c|
    detail = c.column
    next if detail.name == 'id'
    options = {}
    case detail.type
    when :string, :text, :binary
      options[:length] = { :maximum => detail.limit } if detail.limit.present?
    when :integer, :float, :decimal
      options[:numericality] = true
      options[:numericality] = {:only_integer => true} if detail.type == :integer
    end
    options[:presence] = true unless detail.null

    unless options.empty?
      # puts "validates :#{detail.name.to_sym}, #{options.inspect}"
      klass.class_eval do
        validates detail.name.to_sym, options || {}
      end
    end
    
  end
end