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
21
22
23
24
25
26
27
# File 'lib/activestorage/validator/blob.rb', line 4

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

  size_range   = resolve_option(record, options[:size_range])
  content_type = resolve_option(record, options[:content_type])
  extension    = resolve_option(record, options[:extension])

  Array(values).each do |value|
    validate_size(record, attribute, value, size_range) if size_range.present?

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

    unless valid_extension?(value.blob, extension)
      record.errors.add(
        attribute,
        :extension,
        filename: value.blob.filename.to_s,
        extension: Array(extension).map { |e| normalize_extension(e) }.join(', ')
      )
    end
  end
end