Class: ActiveStorageValidations::AspectRatioValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
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

Constructor Details

#initialize(options) ⇒ AspectRatioValidator

Returns a new instance of AspectRatioValidator.



10
11
12
# File 'lib/active_storage_validations/aspect_ratio_validator.rb', line 10

def initialize(options)
  super(options)
end

Instance Method Details

#check_validity!Object

Raises:

  • (ArgumentError)


15
16
17
18
# File 'lib/active_storage_validations/aspect_ratio_validator.rb', line 15

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

#validate_each(record, attribute, _value) ⇒ Object

Rails 5



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

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