2013-06-13 04:18:17 -04:00
|
|
|
module Validators; end
|
|
|
|
class Validators::StrippedLengthValidator < ActiveModel::EachValidator
|
|
|
|
def self.validate(record, attribute, value, range)
|
2013-02-15 20:58:33 -05:00
|
|
|
unless value.nil?
|
|
|
|
stripped_length = value.strip.length
|
2013-06-13 04:18:17 -04:00
|
|
|
record.errors.add attribute, (I18n.t('errors.messages.too_short', count: range.begin)) unless
|
2013-02-15 20:58:33 -05:00
|
|
|
stripped_length >= range.begin
|
2014-07-04 02:38:27 -04:00
|
|
|
record.errors.add attribute, (I18n.t('errors.messages.too_long_validation', max: range.end, length: stripped_length)) unless
|
2013-02-15 20:58:33 -05:00
|
|
|
stripped_length <= range.end
|
|
|
|
else
|
2013-06-13 04:18:17 -04:00
|
|
|
record.errors.add attribute, (I18n.t('errors.messages.blank'))
|
2013-02-15 20:58:33 -05:00
|
|
|
end
|
|
|
|
end
|
2013-06-13 04:18:17 -04:00
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
# the `in` parameter might be a lambda when the range is dynamic
|
|
|
|
range = options[:in].lambda? ? options[:in].call : options[:in]
|
|
|
|
self.class.validate(record, attribute, value, range)
|
|
|
|
end
|
2013-02-15 20:58:33 -05:00
|
|
|
end
|