You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: document.rb
    path of file: ./repos/onebody/app/models
    
    the code of the file until where you have to start completion: require 'tempfile'
require 'stringio'

class Document < ApplicationRecord
  include Authority::Abi
    
  def previewable?
    return false if dont_preview
    image? || pdf?
  end

  def build_preview
    temp = Tempfile.new('preview')
    temp.close
    out_path = temp.path + '.png'
    begin
      MiniMagick::Tool::Convert.new do |convert|
        convert << file.path + '[0]'
        convert << out_path
      end
    rescue MiniMagick::Error
      Rails.logger.warn("WARNING: Could not build preview image for #{file_file_name}")
      self.preview = nil
    else
      self.preview = Rack::Test::UploadedFile.new(out_path, 'image/png', true)
      self.dont_preview = true
      save!
    end
  end