2018-06-27 12:20:23 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-24 04:28:24 -05:00
|
|
|
# DurationValidator
|
|
|
|
#
|
|
|
|
# Validate the format conforms with ChronicDuration
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# class ApplicationSetting < ActiveRecord::Base
|
|
|
|
# validates :default_artifacts_expire_in, presence: true, duration: true
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
class DurationValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
ChronicDuration.parse(value)
|
|
|
|
rescue ChronicDuration::DurationParseError
|
2018-12-05 07:09:43 -05:00
|
|
|
if options[:message]
|
|
|
|
record.errors.add(:base, options[:message])
|
|
|
|
else
|
|
|
|
record.errors.add(attribute, "is not a correct duration")
|
|
|
|
end
|
2017-02-24 04:28:24 -05:00
|
|
|
end
|
|
|
|
end
|