Class: Attachs::Extensions::ActiveRecord::Validations::AttachmentSizeValidator
- Inherits:
-
AttachmentValidator
- Object
- ActiveModel::EachValidator
- AttachmentValidator
- Attachs::Extensions::ActiveRecord::Validations::AttachmentSizeValidator
- Defined in:
- lib/attachs/extensions/active_record/validations/attachment_size_validator.rb
Constant Summary collapse
- CHECKS =
{ less_than: :<, less_than_or_equal_to: :<=, greater_than: :>, greater_than_or_equal_to: :>= }
Instance Method Summary collapse
-
#initialize(options) ⇒ AttachmentSizeValidator
constructor
A new instance of AttachmentSizeValidator.
- #validate_one(record, attribute, attachment) ⇒ Object
Methods inherited from AttachmentValidator
Constructor Details
#initialize(options) ⇒ AttachmentSizeValidator
Returns a new instance of AttachmentSizeValidator.
10 11 12 13 14 15 16 |
# File 'lib/attachs/extensions/active_record/validations/attachment_size_validator.rb', line 10 def initialize() if range = ([:in] || [:within]) [:less_than_or_equal_to] = range.max [:greater_than_or_equal_to] = range.min end super end |
Instance Method Details
#validate_one(record, attribute, attachment) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/attachs/extensions/active_record/validations/attachment_size_validator.rb', line 18 def validate_one(record, attribute, ) unless .blank? CHECKS.each do |name, operator| if .has_key?(name) other = [name] case other when Symbol other = record.send(other) when Proc other = other.call(record) end unless .size.send(operator, other) record.errors.add attribute, :invalid .errors.add :size, name, count: humanize_size(other) end end end end end |