Class: ActiveStorageValidations::LimitValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ActiveStorageValidations::LimitValidator
- Defined in:
- lib/active_storage_validations/limit_validator.rb
Overview
:nodoc:
Constant Summary collapse
- AVAILABLE_CHECKS =
i[max min].freeze
Instance Method Summary collapse
- #check_validity! ⇒ Object
- #files_count_valid?(count) ⇒ Boolean
- #validate_each(record, attribute, _value) ⇒ Object
Instance Method Details
#check_validity! ⇒ Object
7 8 9 10 11 |
# File 'lib/active_storage_validations/limit_validator.rb', line 7 def check_validity! return true if AVAILABLE_CHECKS.any? { |argument| .key?(argument) } raise ArgumentError, 'You must pass either :max or :min to the validator' end |
#files_count_valid?(count) ⇒ Boolean
27 28 29 30 31 32 33 34 35 |
# File 'lib/active_storage_validations/limit_validator.rb', line 27 def files_count_valid?(count) if [:max].present? && [:min].present? count >= [:min] && count <= [:max] elsif [:max].present? count <= [:max] elsif [:min].present? count >= [:min] end end |
#validate_each(record, attribute, _value) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/active_storage_validations/limit_validator.rb', line 13 def validate_each(record, attribute, _value) return true unless record.send(attribute).attached? files = Array.wrap(record.send(attribute)) = {} [:min] = [:min] [:max] = [:max] return true if files_count_valid?(files.count) record.errors.add(attribute, [:message].presence || :limit_out_of_range, ) end |