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

337 views Asked by At

I have a Ecto schema, that uses a arc uploader as a field type.

  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

I would like to add a exmachina factory for this

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

How would i go about that?

1

There are 1 answers

0
TheAnh On BEST ANSWER

From 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.

I would do:

@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