Class: ActiveStorageValidations::AspectRatioValidator

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

Overview

:nodoc

Constant Summary collapse

AVAILABLE_CHECKS =
%i[with].freeze
PRECISION =
3

Instance Method Summary collapse

Methods included from OptionProcUnfolding

#unfold_procs

Instance Method Details

#check_validity!Object

Raises:

  • (ArgumentError)


12
13
14
15
# File 'lib/active_storage_validations/aspect_ratio_validator.rb', line 12

def check_validity!
  return true if AVAILABLE_CHECKS.any? { |argument| options.key?(argument) }
  raise ArgumentError, 'You must pass :with to the validator'
end

#validate_each(record, attribute, _value) ⇒ Object

Rails 5



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

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|
     = Metadata.new(file).
    next if is_valid?(record, attribute, )
    break
  end
end