Class: Spree::Validations::DbMaximumLengthValidator
- Inherits:
-
ActiveModel::Validator
- Object
- ActiveModel::Validator
- Spree::Validations::DbMaximumLengthValidator
- Defined in:
- app/models/spree/validations/db_maximum_length_validator.rb
Overview
Validates a field based on the maximum length of the underlying DB field, if there is one.
Instance Method Summary collapse
-
#initialize(options) ⇒ DbMaximumLengthValidator
constructor
A new instance of DbMaximumLengthValidator.
- #validate(record) ⇒ Object
Constructor Details
#initialize(options) ⇒ DbMaximumLengthValidator
Returns a new instance of DbMaximumLengthValidator.
8 9 10 11 12 |
# File 'app/models/spree/validations/db_maximum_length_validator.rb', line 8 def initialize() super @field = [:field].to_s raise ArgumentError.new("a field must be specified to the validator") if @field.blank? end |
Instance Method Details
#validate(record) ⇒ Object
14 15 16 17 18 19 20 |
# File 'app/models/spree/validations/db_maximum_length_validator.rb', line 14 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 |