Sha256: a3271eb518b967d50b2c12980f50adfa90b10932d624341c063a9a72ccb20f16

Contents?: true

Size: 671 Bytes

Versions: 28

Compression:

Stored size: 671 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.new("a field must be specified to the validator") if @field.blank?
      end

      def validate(record)
        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

28 entries across 28 versions & 1 rubygems

Version Path
solidus_core-1.2.3 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.1.4 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.0.7 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.0.6 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.2.2 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.2.1 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.1.3 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.0.5 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.2.0 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.2.0.rc2 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.0.4 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.1.2 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.2.0.rc1 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.2.0.beta1 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.0.3 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.1.1 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.1.0 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.1.0.pre2 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.1.0.pre1 app/models/spree/validations/db_maximum_length_validator.rb
solidus_core-1.1.0.beta1 app/models/spree/validations/db_maximum_length_validator.rb