Class: ActiveStorageValidations::LimitValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ActiveStorageValidations::LimitValidator
- Includes:
- OptionProcUnfolding
- 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, flat_options) ⇒ Boolean
- #validate_each(record, attribute, _) ⇒ Object
Methods included from OptionProcUnfolding
Instance Method Details
#check_validity! ⇒ Object
9 10 11 12 |
# File 'lib/active_storage_validations/limit_validator.rb', line 9 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, flat_options) ⇒ Boolean
23 24 25 26 27 28 29 30 31 |
# File 'lib/active_storage_validations/limit_validator.rb', line 23 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, _) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/active_storage_validations/limit_validator.rb', line 14 def validate_each(record, attribute, _) files = Array.wrap(record.send(attribute)).compact.uniq = unfold_procs(record, self., AVAILABLE_CHECKS) = { min: [:min], max: [:max] } return true if files_count_valid?(files.count, ) record.errors.add(attribute, [:message].presence || :limit_out_of_range, **) end |