PolyEcto

CIHex.pmDocumentation

A generic, configurable library for polymorphic associations in Ecto schemas. PolyEcto provides a clean, efficient way to create "belongs to any" and "has many polymorphic" relationships without sacrificing Ecto's familiar patterns.

See the detailed documentation for comprehensive usage examples and API reference.

Features

Installation

Add polyecto to your list of dependencies in mix.exs:

def deps do
  [
    {:polyecto, "~> 0.1.0"},
    {:ecto, "~> 3.10"}
  ]
end

Quick Start

  1. Create a config module:
defmodule MyApp.PolyEctoConfig do
  @behaviour PolyEcto.Config

  @registry %{
    "posts" => MyApp.Post,
    "comments" => MyApp.Comment
  }

  @reverse_registry Map.new(@registry, fn {k, v} -> {v, k} end)

  @impl true
  def repo, do: MyApp.Repo

  @impl true
  def get_schema(table_name), do: Map.get(@registry, table_name)

  @impl true
  def get_table(schema_module), do: Map.get(@reverse_registry, schema_module)
end
  1. Configure it in config/config.exs:
config :polyecto, :config, MyApp.PolyEctoConfig
  1. Use in your schemas:
defmodule MyApp.Comment do
  use Ecto.Schema
  import PolyEcto

  schema "comments" do
    polymorphic_belongs_to :commentable
    field :content, :text
    timestamps()
  end

  def changeset(comment, attrs) do
    comment
    |> cast(attrs, [:content])
    |> PolyEcto.cast_polymorphic(:commentable)
  end
end

Documentation

For detailed documentation including:

See the comprehensive README.

License

MIT