Class: SexyValidations::Validators::Age

Inherits:
Object
  • Object
show all
Defined in:
lib/sexy_validations/validators/age.rb

Class Method Summary collapse

Class Method Details

.validate(record, attribute, value, options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sexy_validations/validators/age.rb', line 5

def self.validate(record, attribute, value, options)
  return unless value

  unless options.is_a?(Hash)
    options = {
      :within => options,
    }
  end
  
  if value.is_a?(::Date) || value.is_a?(::Time)
    min = options[:within].min
    if value.calc_age < min
      record.errors.add(attribute, "zu jung (mindestes #{min} Jahre)")
    end

    max = options[:within].max
    if value.calc_age > max
      record.errors.add(attribute, "zu alt (maximal #{max} Jahre)")
    end
  else
    record.errors.add(attribute, "ungültig")        
  end          
end