Class: ActiveStorageValidations::LimitValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ActiveStorageValidations::LimitValidator
show all
- Includes:
- ErrorHandler, OptionProcUnfolding
- Defined in:
- lib/active_storage_validations/limit_validator.rb
Overview
Constant Summary
collapse
- AVAILABLE_CHECKS =
%i[max min].freeze
Instance Method Summary
collapse
#add_error, #initialize_error_options
#unfold_procs
Instance Method Details
#check_validity! ⇒ Object
10
11
12
13
14
|
# File 'lib/active_storage_validations/limit_validator.rb', line 10
def check_validity!
unless AVAILABLE_CHECKS.any? { |argument| options.key?(argument) }
raise ArgumentError, 'You must pass either :max or :min to the validator'
end
end
|
#files_count_valid?(count, flat_options) ⇒ 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, flat_options)
if flat_options[:max].present? && flat_options[:min].present?
count >= flat_options[:min] && count <= flat_options[:max]
elsif flat_options[:max].present?
count <= flat_options[:max]
elsif flat_options[:min].present?
count >= flat_options[:min]
end
end
|
#validate_each(record, attribute, _) ⇒ Object
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/active_storage_validations/limit_validator.rb', line 16
def validate_each(record, attribute, _)
files = Array.wrap(record.send(attribute)).compact.uniq
flat_options = unfold_procs(record, self.options, AVAILABLE_CHECKS)
errors_options = initialize_error_options(options)
errors_options[:min] = flat_options[:min]
errors_options[:max] = flat_options[:max]
return true if files_count_valid?(files.count, flat_options)
add_error(record, attribute, :limit_out_of_range, **errors_options)
end
|