Sha256: d7ca823780016754367316af5b5235e744c7fe8ad3c28e39e02637b778db408e

Contents?: true

Size: 1.37 KB

Versions: 6

Compression:

Stored size: 1.37 KB

Contents

module ActiveRecord
  module Validations
    class BlobValidator < ::ActiveModel::EachValidator
      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

      private

        def valid_content_type?(blob)
          return true if options[:content_type].nil?

          case options[:content_type]
          when Regexp
            options[:content_type].match?(blob.content_type)
          when Array
            options[:content_type].include?(blob.content_type)
          when Symbol
            blob.public_send("#{options[:content_type]}?")
          else
            options[:content_type] == blob.content_type
          end
        end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
activestorage-validator-0.3.0 lib/activestorage/validator/blob.rb
activestorage-validator-0.2.2 lib/activestorage/validator/blob.rb
activestorage-validator-0.2.1 lib/activestorage/validator/blob.rb
activestorage-validator-0.2.0 lib/activestorage/validator/blob.rb
activestorage-validator-0.1.5 lib/activestorage/validator/blob.rb
activestorage-validator-0.1.4 lib/activestorage/validator/blob.rb