Sha256: 8c9cdcf3ca306a5db9fc812fceb1d6476429d1d502b0d0bbed90366ff192ab23

Contents?: true

Size: 783 Bytes

Versions: 89

Compression:

Stored size: 783 Bytes

Contents

module Spree
  module Validations
    ##
    # Validates a field based on the maximum length of the underlying DB field, if there is one.
    class DbMaximumLengthValidator < ActiveModel::Validator
      def initialize(options)
        super
        @field = options[:field].to_s
        raise ArgumentError, 'a field must be specified to the validator' if @field.blank?
      end

      def validate(record)
        warn '`Spree::Validations::DbMaximumLengthValidator` is deprecated. Use `DbMaximumLengthValidator` instead.'
        limit = record.class.columns_hash[@field].limit
        value = record[@field.to_sym]
        if value && limit && value.to_s.length > limit
          record.errors.add(@field.to_sym, :too_long, count: limit)
        end
      end
    end
  end
end

Version data entries

89 entries across 89 versions & 1 rubygems

Version Path
spree_core-4.1.3 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-4.1.2 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-4.1.1 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-4.1.0 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-4.1.0.rc3 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-4.1.0.rc2 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-4.1.0.rc1 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-3.4.6 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-3.7.10 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-4.0.3 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-3.7.9 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-4.0.2 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-3.7.8 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-4.0.1 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-3.7.7 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-4.0.0 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-4.0.0.rc3 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-3.7.6 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-3.7.5 app/models/spree/validations/db_maximum_length_validator.rb
spree_core-4.0.0.rc2 app/models/spree/validations/db_maximum_length_validator.rb