Sha256: f9161efa2ac531c2229e545a6288006137e670661a4ed5bb096d207cb50382cc

Contents?: true

Size: 932 Bytes

Versions: 2

Compression:

Stored size: 932 Bytes

Contents

module ActiveRecord
  module MySQL
    module Strict
      class StrictLengthValidator < ActiveModel::Validations::LengthValidator
        def validate_each(record, attribute, value)
          value = record.send(:read_attribute, attribute)
          value_length = value.respond_to?(:length) ? value.length : value.to_s.length
          errors_options = options.except(*RESERVED_OPTIONS)

          CHECKS.each do |key, validity_check|
            next unless check_value = options[key]

            if !value.nil? || skip_nil_check?(key)
              next if value_length.send(validity_check, check_value)
            end

            errors_options[:count] = check_value

            default_message = options[MESSAGES[key]]
            errors_options[:message] ||= default_message if default_message

            record.errors.add(attribute, MESSAGES[key], errors_options)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord_mysql_strict-0.2 lib/active_record/mysql/strict/strict_length_validator.rb
activerecord_mysql_strict-0.1.1 lib/active_record/mysql/strict/strict_length_validator.rb