Module: MagicModelsGenerator::Validations

Defined in:
lib/magic_model_generator/validations.rb

Class Method Summary collapse

Class Method Details

.generate_validations(klass) ⇒ Object

Returns an array of strings describing validations for this class



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

def self.generate_validations(klass)

  logger = MagicModelsGenerator.logger

  # Code reworked from http://www.redhillconsulting.com.au/rails_plugins.html
  # Thanks Red Hill Consulting for using an MIT licence :o)

  validations = []
  klass.columns.
    reject { |column| column.name =~ /(?i)^(((created|updated)_(at|on))|position|type|id)$/ }.
    reject { |column| column.name =~ /_id$/ }.
    each do |column|
    
    # Active record seems to interpolate booleans anyway to either true, false or nil...
    if column.type == :boolean
      validations << "validates_inclusion_of :#{column.name}, :in => [true, false], :allow_nil => #{column.null}, :message => ActiveRecord::Errors.default_error_messages[:blank]"
      logger.debug validations.last
    elsif !column.null
      validations << "validates_presence_of :#{column.name}"
      logger.debug validations.last
    end

    if column.type == :integer
      validations << "validates_numericality_of :#{column.name}, :allow_nil => #{column.null.inspect}, :only_integer => true"
      logger.debug validations.last
    elsif column.number?
      validations << "validates_numericality_of :#{column.name}, :allow_nil => #{column.null.inspect}"
      logger.debug validations.last
    elsif column.text? && column.limit
      validations << "validates_length_of :#{column.name}, :allow_nil => #{column.null.inspect}, :maximum => #{column.limit}"
      logger.debug validations.last
    end
    
  end
  
  # Single-column UNIQUE indexes
  klass.get_unique_index_columns.each do |col|
    validations << "validates_uniqueness_of :#{col}"
    logger.debug validations.last
  end                  
  
  validations
end