Sha256: 74b42223b4e5bdb9e82d0f231d134fcb576f6d15b90622f3890c81f4ca841461

Contents?: true

Size: 652 Bytes

Versions: 2

Compression:

Stored size: 652 Bytes

Contents

class TimeFormatValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    return if options[:allow_nil] && value.nil?

    parsed_time = value.is_a?(Time) ? value : Time.parse(value.to_s)

    if !previous_time.nil? and parsed_time < previous_time
      record.errors[attribute] << "invalid value, #{value} must be after #{previous_time}"
    end
  rescue StandardError => e
    record.errors[attribute] << "invalid value, #{value} not valid for #{attribute}"
  end

  def previous_time
    case options[:after].class.name
    when 'Proc'
      options[:after].call
    when 'Time'
      options[:after]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_model_validators_ex-0.3.0 lib/active_model_validators_ex/time_format_validator.rb
active_model_validators_ex-0.0.3 lib/active_model_validators_ex/time_format_validator.rb