Class: DateValidator

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

Constant Summary collapse

CHECKS =
{
  # These keys make the most sense for dates
  after: :>,
  after_or_equal_to: :>=,
  equal_to: :==,
  before: :<,
  before_or_equal_to: :<=,

  # For compatibility with numericality
  #greater_than: :>,
  #greater_than_or_equal_to: :>=,
  #less_than: :<,
  #less_than_or_equal_to: :<=,
}.freeze

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, raw_value) ⇒ Object



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
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/validators/date_validator.rb', line 18

def validate_each record, attribute, raw_value
  # TODO: Do we need to do anything with timezones? Figure it out (rails has
  # ActiveSupport::TimeWithZone)...
  value = if raw_value.is_a? Fixnum
            time.at(raw_value).to_date
          elsif raw_value.respond_to? :to_date
            raw_value.to_date
          else
            false
          end

          #elsif raw_value.is_a?(Symbol) || raw_value.is_a?(String)
          #  # Assume another (date) attribute
          #  record.send(raw_value).to_date

  unless value
    record.errors.add attribute, I18n.t('rails_validations.date.invalid')
    return
  end

  options.slice(*CHECKS.keys).each do |option, raw_option_value|
    option_value = if raw_option_value.respond_to? :call
                     raw_option_value.call record
                   elsif raw_option_value.is_a? Symbol
                     record.send raw_option_value
                   elsif raw_option_value.is_a? Fixnum
                     time.at(raw_option_value).to_date
                   elsif raw_option_value.respond_to? :to_date
                     raw_option_value.to_date
                   else
                     raise ArgumentError
                   end

    unless option_value
      record.errors.add attribute, I18n.t('rails_validations.date.invalid')
      return
    end

    unless value.send CHECKS[option], option_value
      record.errors.add attribute, I18n.t("rails_validations.date.#{option}", date: I18n.l(option_value))
    end
  end
end