Class: DbMaximumLengthValidator

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

Overview

Validates a field based on the maximum length of the underlying DB field, if there is one.

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
# File 'app/validators/db_maximum_length_validator.rb', line 4

def validate_each(record, attribute, value)
  limit = if defined?(Globalize)
            record.class.translation_class.columns_hash[attribute.to_s].limit
          else
            record.class.columns_hash[attribute.to_s].limit
          end
  value = record[attribute.to_sym]
  if value && limit && value.to_s.length > limit
    record.errors.add(attribute.to_sym, :too_long, count: limit)
  end
end