Class: ActiveStorageValidations::AttachedValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Includes:
ASVActiveStorageable, ASVErrorable, ASVSymbolizable
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 ASVErrorable

#add_error, #initialize_error_options

Instance Method Details

#check_validity!Object



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

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.to_sym} validator"
    end
  end
end

#validate_each(record, attribute, _value) ⇒ Object



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

def validate_each(record, attribute, _value)
  return if attachments_present?(record, attribute) &&
            will_have_attachments_after_save?(record, attribute)

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