Class: DurationValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/duration_validator.rb

Overview

DurationValidator

Validate the format conforms with ChronicDuration

Example:

class ApplicationSetting < ActiveRecord::Base
  validates :default_artifacts_expire_in, presence: true, duration: true
end

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object


14
15
16
17
18
19
20
21
22
# File 'app/validators/duration_validator.rb', line 14

def validate_each(record, attribute, value)
  ChronicDuration.parse(value)
rescue ChronicDuration::DurationParseError
  if options[:message]
    record.errors.add(:base, options[:message])
  else
    record.errors.add(attribute, "is not a correct duration")
  end
end