Class: ActiveStorageValidations::AspectRatioValidator

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

Overview

:nodoc

Constant Summary collapse

AVAILABLE_CHECKS =
%i[with].freeze
NAMED_ASPECT_RATIOS =
%i[square portrait landscape].freeze
ASPECT_RATIO_REGEX =
/is_([1-9]\d*)_([1-9]\d*)/.freeze
ERROR_TYPES =
%i[
  image_metadata_missing
  aspect_ratio_not_square
  aspect_ratio_not_portrait
  aspect_ratio_not_landscape
  aspect_ratio_is_not
  aspect_ratio_unknown
].freeze
PRECISION =
3.freeze

Instance Method Summary collapse

Methods included from Errorable

#add_error, #initialize_error_options

Methods included from OptionProcUnfolding

#unfold_procs

Instance Method Details

#check_validity!Object



26
27
28
29
# File 'lib/active_storage_validations/aspect_ratio_validator.rb', line 26

def check_validity!
  ensure_at_least_one_validator_option
  ensure_aspect_ratio_validity
end

#validate_each(record, attribute, _value) ⇒ Object

Rails 5



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_storage_validations/aspect_ratio_validator.rb', line 48

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, file, )
    break
  end
end