Class: MysqlLengthOfStringValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Includes:
Logging
Defined in:
lib/string_length_conformable/mysql_length_of_string_validator.rb

Instance Method Summary collapse

Methods included from Logging

#log

Instance Method Details

#validate(record) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/string_length_conformable/mysql_length_of_string_validator.rb', line 8

def validate(record)
  record.attribute_names.each do |attr_name|
    next if record.instance_eval(attr_name).nil?

    next unless record.column_for_attribute(attr_name).type == :string

    symbol_attr = attr_name.parameterize.underscore.to_sym

    next if record.class.validators_on(symbol_attr).select { |v| v.is_a? ActiveRecord::Validations::LengthValidator }.any?

    permited_length = record.class.columns_hash[attr_name].limit
    new_length = record.instance_eval(attr_name).length

    if new_length > permited_length.to_i
      record.errors.add attr_name, "is too long, #{permited_length.to_i} characters is the maximum allowed"
      log
    end
  end
end