Sha256: e88fb5c6769a02b3ab6c540c87b4c863dd046e965e52af7167c18ebd0ed3e359

Contents?: true

Size: 660 Bytes

Versions: 1

Compression:

Stored size: 660 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)

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

    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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_model_validators_ex-0.0.2 lib/active_model_validators_ex/time_format_validator.rb