PhxMediaLibrary.ModelRegistry (PhxMediaLibrary v0.6.1)

Copy Markdown View Source

Discovers and caches the Ecto schema module for a given mediable_type string.

This is the inverse of HasMedia.__media_type__/0 — given a string like "posts", it finds the module (e.g. MyApp.Post) that declared use PhxMediaLibrary.HasMedia and whose __media_type__/0 returns that string.

Lookup Strategy

  1. Check the explicit registry in application config (:model_registry)
  2. Scan all loaded modules that export __media_type__/0

Results are cached in :persistent_term for fast repeated lookups.

Explicit Registry

For production deployments where module scanning is undesirable, you can configure an explicit mapping:

config :phx_media_library,
  model_registry: %{
    "posts" => MyApp.Post,
    "users" => MyApp.User
  }

Summary

Functions

Finds the Ecto schema module that corresponds to the given mediable_type.

Returns the conversions defined on module for the given collection_name.

Functions

find_model_module(mediable_type)

@spec find_model_module(String.t()) :: {:ok, module()} | :error

Finds the Ecto schema module that corresponds to the given mediable_type.

Returns {:ok, module} on success, or :error if no module could be found.

Examples

iex> PhxMediaLibrary.ModelRegistry.find_model_module("posts")
{:ok, MyApp.Post}

iex> PhxMediaLibrary.ModelRegistry.find_model_module("unknown")
:error

get_model_conversions(module, collection_name)

@spec get_model_conversions(module(), atom()) :: [PhxMediaLibrary.Conversion.t()]

Returns the conversions defined on module for the given collection_name.

Tries get_media_conversions/1 first (which filters by collection), then falls back to media_conversions/0 (unfiltered list). Returns [] if the module doesn't define either function.

Examples

iex> PhxMediaLibrary.ModelRegistry.get_model_conversions(MyApp.Post, :images)
[%PhxMediaLibrary.Conversion{name: :thumb, ...}, ...]