Class: ActiveStorageValidations::LimitValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Includes:
ASVActiveStorageable, ASVErrorable, ASVOptionable, ASVSymbolizable
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
  limit_min_not_reached
  limit_max_exceeded
].freeze

Instance Method Summary collapse

Methods included from ASVErrorable

#add_error, #initialize_error_options

Instance Method Details

#check_validity!Object



22
23
24
25
# File 'lib/active_storage_validations/limit_validator.rb', line 22

def check_validity!
  ensure_at_least_one_validator_option
  ensure_arguments_validity
end

#validate_each(record, attribute, _value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_storage_validations/limit_validator.rb', line 27

def validate_each(record, attribute, _value)
  files = attached_files(record, attribute).reject(&:blank?)
  flat_options = set_flat_options(record)
  count = files.count

  return if files_count_valid?(count, flat_options)

  errors_options = initialize_and_populate_error_options(options, flat_options, count)
  error_type = set_error_type(flat_options, count)
  add_error(record, attribute, error_type, **errors_options)
end