Class: ActiveRecord::Validations::BlobValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/activestorage/validator/blob.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, values) ⇒ Object

rubocop:disable Metrics/AbcSize



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/activestorage/validator/blob.rb', line 4

def validate_each(record, attribute, values) # rubocop:disable Metrics/AbcSize
  return unless values.attached?

  Array(values).each do |value|
    if options[:size_range].present?
      if options[:size_range].min > value.blob.byte_size
        record.errors.add(attribute, :min_size_error, min_size: ActiveSupport::NumberHelper.number_to_human_size(options[:size_range].min))
      elsif options[:size_range].max < value.blob.byte_size
        record.errors.add(attribute, :max_size_error, max_size: ActiveSupport::NumberHelper.number_to_human_size(options[:size_range].max))
      end
    end

    unless valid_content_type?(value.blob)
      record.errors.add(attribute, :content_type)
    end
  end
end