Class: SerialTranslator::SerialTranslatorLengthValidator
- Inherits:
-
ActiveModel::Validations::LengthValidator
- Object
- ActiveModel::Validations::LengthValidator
- SerialTranslator::SerialTranslatorLengthValidator
- Defined in:
- lib/serial_translator/serial_translator_length_validator.rb
Instance Method Summary collapse
- #kind ⇒ Object
- #validate_each(record, attribute, _value) ⇒ Object
-
#validate_translation(record, attribute, value) ⇒ Object
this is ActiveModel::Validations::LengthValidator#validate_each, only extended with more interpolation args in error_options.
Instance Method Details
#kind ⇒ Object
37 |
# File 'lib/serial_translator/serial_translator_length_validator.rb', line 37 def kind; :length end |
#validate_each(record, attribute, _value) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/serial_translator/serial_translator_length_validator.rb', line 3 def validate_each(record, attribute, _value) translations = record.__send__("#{attribute}_translations")&.values || [] translations = [nil] if translations.empty? translations.each do |value| next if ([:allow_blank] && value.to_s == '') || ([:allow_nil] && value.nil?) validate_translation(record, attribute, value) end end |
#validate_translation(record, attribute, value) ⇒ Object
this is ActiveModel::Validations::LengthValidator#validate_each, only extended with more interpolation args in error_options
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/serial_translator/serial_translator_length_validator.rb', line 16 def validate_translation(record, attribute, value) value_length = value.respond_to?(:length) ? value.length : value.to_s.length = .except(*RESERVED_OPTIONS) CHECKS.each do |key, validity_check| next unless check_value = [key] if !value.nil? || skip_nil_check?(key) next if value_length.send(validity_check, check_value) end .merge!(count: check_value, actual_length: value_length) key == :minimum && [:difference] = check_value - value_length key == :maximum && [:difference] = value_length - check_value = [MESSAGES[key]] [:message] ||= if record.errors.add(attribute, MESSAGES[key], **) end end |