Class: ActiveStorageValidations::AspectRatioValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ActiveStorageValidations::AspectRatioValidator
- 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
- #check_validity! ⇒ Object
-
#initialize(options) ⇒ AspectRatioValidator
constructor
A new instance of AspectRatioValidator.
-
#validate_each(record, attribute, _value) ⇒ Object
Rails 5.
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() super() end |
Instance Method Details
#check_validity! ⇒ Object
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| .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.[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 |