Class: TimeFormatValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/active_model_validators_ex/time_format_validator.rb

Instance Method Summary collapse

Instance Method Details

#previous_timeObject



14
15
16
17
18
19
20
21
# File 'lib/active_model_validators_ex/time_format_validator.rb', line 14

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

#validate_each(record, attribute, value) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/active_model_validators_ex/time_format_validator.rb', line 2

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