module DatesNowValidator

Public Instance Methods

check_dates(errors, from, to) click to toggle source

Сверит даты между собой. Сверит даты с текущей датой.

@param errors @param from [nil || String || ::ActiveSupport::TimeWithZone || Time] @param to [аналогично]

# File lib/c80_shared/dates_now_validator.rb, line 10
def check_dates(errors, from, to)
  errors.add(:from, :invalid_date) unless is_date?(from)
  errors.add(:to,   :invalid_date) unless is_date?(to)

  # если введена хотя бы одна некорректная дата - дальше не проверяем
  return if errors[:from].any? || errors[:to].any?

  from = Time.parse(from) rescue from
  to   = Time.parse(to)   rescue to
  now  = Time.zone.now.beginning_of_day

  conditions = [
    from.beginning_of_day < now,
    to.beginning_of_day   < now,
    from > to
  ]

  if conditions.any?
    errors.add(:from, :invalid_date)
    errors.add(:to, :invalid_date)
  end

end

Private Instance Methods

is_date?(date) click to toggle source
# File lib/c80_shared/dates_now_validator.rb, line 36
def is_date?(date)
  return true if date.is_a? ::ActiveSupport::TimeWithZone
  !!Time.parse(date) rescue false
end