Class: ActiveStorageValidations::AttachedValidator

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

Overview

:nodoc:

Constant Summary collapse

ERROR_TYPES =
%i[blank].freeze

Instance Method Summary collapse

Methods included from Errorable

#add_error, #initialize_error_options

Instance Method Details

#check_validity!Object



13
14
15
16
17
18
19
# File 'lib/active_storage_validations/attached_validator.rb', line 13

def check_validity!
  %i[allow_nil allow_blank].each do |not_authorized_option|
    if options.include?(not_authorized_option)
      raise ArgumentError, "You cannot pass the :#{not_authorized_option} option to the #{self.class.name.split('::').last.underscore}"
    end
  end
end

#validate_each(record, attribute, _value) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/active_storage_validations/attached_validator.rb', line 21

def validate_each(record, attribute, _value)
  return if record.send(attribute).attached? &&
            !Array.wrap(record.send(attribute)).all?(&:marked_for_destruction?)

  errors_options = initialize_error_options(options)
  add_error(record, attribute, ERROR_TYPES.first, **errors_options)
end