Class: Tuning::Validations::TimeValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/tuning/validations/time.rb

Constant Summary collapse

CHECKS =
{ before: :<, before_or_equal_to: :<=, after: :>, after_or_equal_to: :>= }

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tuning/validations/time.rb', line 8

def validate_each(record, attribute, value)
  if [Date, Time].any? { |klass| value.kind_of?(klass) }
    CHECKS.each do |name, operator|
      if options.has_key?(name)
        other = options[name]
        case other
        when Symbol
          other = record.send(other)
        when Proc
          other = other.call(record)
        end
        unless value.send(operator, other)
          record.errors.add attribute, name, time: I18n.l(other)
        end
      end
    end
  else
    record.errors.add attribute, :not_a_time
  end
end