我如何在 exmachina 工厂中使用 arc ecto 文件?

How do i use arc ecto files, with exmachina factories?

我有一个 Ecto 模式,它使用 arc uploader 作为字段类型。

  schema "files" do
    field :name, :string
    field :locked, :boolean, default: false
    field :mime_type, :string
    field :path, Splish.Assets.FilesUploader.Type
    field :size, :integer
    belongs_to :user, Splish.Accounts.User

    timestamps()
  end

我想为此添加一个 exmachina 工厂

  def file_factory do
    %File{
      name: "Gyldendal",
      locked: false,
      mime_type: "png",
      size: 200,
      # path: "test/support/image.png",
      user: insert(:user)
    }
  end

我该怎么做?

来自Arc Basic Usage

The upload definition file responds to Avatar.store/1 which accepts either:

  • A path to a local file
  • A path to a remote http or https file
  • A map with a filename and path keys (eg, a %Plug.Upload{})
  • A map with a filename and binary keys (eg, %{filename: "image.png", binary: <<255,255,255,...>>})
  • A two-tuple consisting of one of the above file formats as well as a scope object.

我会做:

@file_upload %Plug.Upload{
    content_type: "image/png",
    filename: "image.png",
    path: "test/support/image.png" }


 def file_factory do
  %File{
    name: "Gyldendal",
    locked: false,
    mime_type: "png",
    size: 200,
    path: @file_upload, # or with scope {@file_upload, %File{}}
    user: insert(:user)
  }
end