Upload.Ecto (upload v0.3.0)

Allows Ecto to handle file uploads.

Link to this section Summary

Functions

Casts an upload in the params under the given key, uploads it, and assigns it to the field.

Casts a path in the params under a given key, uploads it, and assigns it to the field.

Add the Upload's key to the changeset for the given field.

Link to this section Functions

Link to this function

cast_upload(changeset, field, opts \\ [])

Specs

cast_upload(Ecto.Changeset.t(), atom(), list()) :: Ecto.Changeset.t()

Casts an upload in the params under the given key, uploads it, and assigns it to the field.

Any options passed to this function will be passed through to Upload.cast.

You can also provide an option :with, which will allow you to use a custom uploader. You can use this mechanism to add validation to your file uploads. See the documentation about Upload.Uploader for more information.

Example

def changeset(user, params \\ %{}) do
  user
  |> cast(params, [:name])
  |> Upload.Ecto.cast_upload(:logo)
end

def changeset(user, params \\ %{}) do
  user
  |> cast(params, [:name])
  |> Upload.Ecto.cast_upload(:logo, prefix: ["logos"])
end

def changeset(user, params \\ %{}) do
  user
  |> cast(params, [:name])
  |> Upload.Ecto.cast_upload(:logo, with: MyCustomUploader)
end
Link to this function

cast_upload_path(changeset, field, opts \\ [])

Specs

cast_upload_path(Ecto.Changeset.t(), atom(), list()) :: Ecto.Changeset.t()

Casts a path in the params under a given key, uploads it, and assigns it to the field.

This function accepts the same options that Upload.Ecto.cast_upload/2 accepts.

Link to this function

put_upload(changeset, field, upload, opts \\ [])

Specs

put_upload(Ecto.Changeset.t(), atom(), Upload.t(), list()) :: Ecto.Changeset.t()

Add the Upload's key to the changeset for the given field.

If the file hasn't been uploaded yet, it will be.