Class: ActiveStorageValidations::ProcessableImageValidator

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

Overview

:nodoc

Constant Summary collapse

ERROR_TYPES =
%i[
  image_not_processable
].freeze

Instance Method Summary collapse

Methods included from Errorable

#add_error, #initialize_error_options

Methods included from OptionProcUnfolding

#unfold_procs

Instance Method Details

#validate_each(record, attribute, _value) ⇒ Object

Rails 5



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_storage_validations/processable_image_validator.rb', line 35

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

  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|
    if !Metadata.new(file).valid?
      errors_options = initialize_error_options(options, file)
      add_error(record, attribute, ERROR_TYPES.first , **errors_options) unless Metadata.new(file).valid?
    end
  end
end