Class: ActiveModel::Validations::DatetimeValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/validators/validates_datetime.rb

Instance Method Summary collapse

Instance Method Details

#date?(value) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/validators/validates_datetime.rb', line 6

def date?(value)
  value.is_a?(Date) || value.is_a?(Time)
end

#validate_each(record, attribute, value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/validators/validates_datetime.rb', line 10

def validate_each(record, attribute, value)
  return if value.blank? && options[:allow_blank]
  return if value.nil? && options[:allow_nil]

  unless date?(value)
    record.errors.add(
      attribute,
      :invalid_date,
      message: options[:message],
      value: value
    )
  end

  return unless date?(value)

  validate_after_option(record, attribute, value)
  validate_before_option(record, attribute, value)
end