Uploader v0.1.0 Uploader.Ecto.UploadableFields behaviour View Source

Provides a macro for defining uploadable fields.

When calling uploadable_field/2, two fields are created:

  • a virtual field is created named by the given name prefixed by "uploaded_" and holds the Plug.Upload struct (representing the uploaded file).
  • a field with the given name is created holding the casted Plug.Upload struct (typically the struct is casted into a filename).

    The code below

    uploadable_field :image

    will generate

    field :image, :string
    field :uploaded_image, :any, virtual: true

Using this module (use) provides the caller module with the function get_uploadable_fields\0 listing all the uploadable fields and their options.

The following options may be given to uploadable fields:

  • :cast: a function that casts a Plug.Upload struct into the value to be stored in the database.
  • directory: the base directory containing the file uploads.
  • filename: a function that generates the filename based on the given struct.
  • on_file_exists: specifies the strategy to apply if the file path already exists. Its value may be:

    • :overwrite: overwrite the file if the file path already exists
    • :compare_hash: do not copy the file if the file path already exists; if the hashes of the files' content are not equal, return an error.
  • print: a function that prints the field (typically used be the view).
  • type: the field type.

Link to this section Summary

Link to this section Functions

Link to this macro

uploadable_field(field_name, opts \\ [])

View Source (macro)

Link to this section Callbacks

Link to this callback

get_uploadable_fields()

View Source
get_uploadable_fields() :: [atom()]