Class: DurationValidator

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

Overview

Note:

Although this method is available to you, you should use HasDuration::ActiveRecordExtension::has_duration which also adds serialization.

The DurationValidator checks that a given field has an ActiveSupport::Duration or nil If you also need presence checks you should use ActiveSupport’s built in presence validator.

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



9
10
11
12
13
# File 'lib/has_duration.rb', line 9

def validate_each(record, attribute, value)
  unless value.is_a?(ActiveSupport::Duration) || value.nil?
    record.errors[attribute] << 'must be an ActiveSupport::Duration (1.month, 2.years, etc)'
  end
end