Class: ActiveStorageValidations::DurationValidator

Inherits:
BaseComparisonValidator show all
Includes:
ASVAnalyzable, ASVAttachable
Defined in:
lib/active_storage_validations/duration_validator.rb

Constant Summary collapse

ERROR_TYPES =
%i[
  duration_not_less_than
  duration_not_less_than_or_equal_to
  duration_not_greater_than
  duration_not_greater_than_or_equal_to
  duration_not_between
].freeze
METADATA_KEYS =
%i[duration].freeze

Constants included from ASVAnalyzable

ASVAnalyzable::DEFAULT_IMAGE_PROCESSOR

Constants inherited from BaseComparisonValidator

BaseComparisonValidator::AVAILABLE_CHECKS

Instance Method Summary collapse

Methods inherited from BaseComparisonValidator

#check_validity!, #initialize

Methods included from ASVErrorable

#add_error, #initialize_error_options

Constructor Details

This class inherits a constructor from ActiveStorageValidations::BaseComparisonValidator

Instance Method Details

#validate_each(record, attribute, _value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/active_storage_validations/duration_validator.rb', line 19

def validate_each(record, attribute, _value)
  return if no_attachments?(record, attribute)

  flat_options = set_flat_options(record)

  attachables_and_blobs(record, attribute).each do |attachable, blob|
    duration = begin
      (blob, attachable, METADATA_KEYS)&.fetch(:duration, nil)
    rescue ActiveStorage::FileNotFoundError
      add_attachment_missing_error(record, attribute, attachable)
      next
    end

    if duration.to_i <= 0
      (record, attribute, attachable)
      next
    end

    is_valid?(duration, flat_options) || populate_error_options_and_add_error(record, attribute, attachable, flat_options, duration)
  end
end