Class: Dsu::Validators::TimeValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/dsu/validators/time_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dsu/validators/time_validator.rb', line 7

def validate(record)
  time = record.time

  if time.nil?
    record.errors.add(:time, :blank)
    return
  end

  unless time.is_a?(Time)
    record.errors.add(:time, 'is the wrong object type. ' \
                             "\"Time\" was expected, but \"#{time.class}\" was received.")
    return
  end

  record.errors.add(:time, 'is not in localtime format.') unless time == time.in_time_zone
end