Class: ActiveStorageValidations::ProcessableImageValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Includes:
ErrorHandler, OptionProcUnfolding
Defined in:
lib/active_storage_validations/processable_image_validator.rb

Overview

:nodoc

Instance Method Summary collapse

Methods included from ErrorHandler

#add_error, #initialize_error_options

Methods included from OptionProcUnfolding

#unfold_procs

Instance Method Details

#validate_each(record, attribute, _value) ⇒ Object

Rails 5



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

def validate_each(record, attribute, _value)
  return true unless record.send(attribute).attached?

  errors_options = initialize_error_options(options)

  changes = record.attachment_changes[attribute.to_s]
  return true if changes.blank?

  files = Array.wrap(changes.is_a?(ActiveStorage::Attached::Changes::CreateMany) ? changes.attachables : changes.attachable)

  files.each do |file|
    add_error(record, attribute, :image_not_processable, **errors_options) unless Metadata.new(file).valid?
  end
end