Attached.Ecto.Schema (Attached v0.1.1)

Copy Markdown View Source

Macros for declaring file attachments on Ecto schemas.

Usage

defmodule MyApp.Accounts.User do
  use Ecto.Schema
  use Attached.Ecto.Schema

  schema "users" do
    field :name, :string

    attached :avatar, variants: %{
      thumb: [resize_to_fill: {100, 100}],
      medium: [resize_to_limit: {400, 400}]
    }
  end
end

FK column naming

attached :avatar expects an avatar_attached_original_id FK column by default.

Override per field: attached :avatar, foreign_key: :avatar_original_id Override globally: config :attached, default_foreign_key_suffix: "_original_id"

Multiple attachments

For multi-file attachments, declare a plain Ecto join schema with its own belongs_to :original, Attached.Originals.Original association — that gives you a real schema for positions, captions, soft deletes, and ad-hoc queries without fighting a hidden join table.

Summary

Functions

Declares a single file attachment on the schema.

Functions

attached(name, opts \\ [])

(macro)

Declares a single file attachment on the schema.

Generates a belongs_to :{name}_attached_original association and expects a {name}_attached_original_id FK column by default (configurable).

Options

  • :foreign_key — override the FK column name (atom)
  • :variants — map of variant name to transformation options

Example

attached :avatar, variants: %{thumb: [resize_to_fill: {100, 100}]}