class ContentTypeValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    return if !value.attached?

    Array.wrap(value).each do |attachment|
      if !attachment.content_type.in?(content_types)
        record.errors.add(attribute, :content_type, options)
      end
    end
  end

  private

  def content_types
    options.fetch(:in)
  end
end
