Class: ActiveStorageValidations::LimitValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Includes:
Errorable, OptionProcUnfolding, Symbolizable
Defined in:
lib/active_storage_validations/limit_validator.rb

Overview

:nodoc:

Constant Summary collapse

AVAILABLE_CHECKS =
%i[max min].freeze
ERROR_TYPES =
%i[
  limit_out_of_range
].freeze

Instance Method Summary collapse

Methods included from Errorable

#add_error, #initialize_error_options

Methods included from OptionProcUnfolding

#unfold_procs

Instance Method Details

#check_validity!Object



17
18
19
20
# File 'lib/active_storage_validations/limit_validator.rb', line 17

def check_validity!
  ensure_at_least_one_validator_option
  ensure_arguments_validity
end

#validate_each(record, attribute, _) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_storage_validations/limit_validator.rb', line 22

def validate_each(record, attribute, _)
  files = Array.wrap(record.send(attribute)).reject { |file| file.blank? }.compact.uniq
  flat_options = unfold_procs(record, self.options, AVAILABLE_CHECKS)

  return true if files_count_valid?(files.count, flat_options)

  errors_options = initialize_error_options(options)
  errors_options[:min] = flat_options[:min]
  errors_options[:max] = flat_options[:max]
  add_error(record, attribute, ERROR_TYPES.first, **errors_options)
end