Module: ActiveRecord::Validations::ClassMethods

Defined in:
lib/rails-schema-validations.rb

Overview

this will put it in the scope of a model

Instance Method Summary collapse

Instance Method Details

#validations_from_schema(options = {}) ⇒ Object



3
4
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
# File 'lib/rails-schema-validations.rb', line 3

def validations_from_schema options = {}
  except = [*options.delete(:except)].map {|c| c.to_s}
  fail "unexpected arguments" if options.size != 0

  self.columns.map do |col|
    next if col.primary or except.include? col.name

    case col.type
    when :integer
      # / 2 assumes signed!
      validates_numericality_of col.name, :only_integer => true, :allow_nil => col.null,
        :less_than => (2 ** (8 * col.limit)) / 2
    when :float
      # less_than would need to look at col.scale, col.float
      validates_numericality_of col.name, :allow_nil => col.null
    #when :time, :datetime
    when :string, :text
      if col.limit.to_i > 0 # Mysql enum type shows up as a string with a limit of 0
        validates_length_of col.name, :maximum => col.limit, :allow_nil => col.null
      end
    when :boolean
      validates_inclusion_of col.name, :in => [true, false], :allow_nil => col.null
    end
  end.compact + content_columns.map do |col|
    next if col.null or col.type == :boolean or except.include? col.name
    validates_presence_of col.name
  end
end