Skip to content

How to extend submodule .Type #48

@willjleong

Description

@willjleong

I've noticed .Type is generated by Waffle.Ecto.Definition's macros as a submodule of our custom uploader Definition. If I want to extend the Ecto.Type so I can add forms in Kaffy admin, I can't figure out a clean way to extend the module that is created using this code:

defmodule Module.concat(unquote(definition), "Type") do
  # After the 3.2 version Ecto has moved @behavior
  # inside the `__using__` macro
  if macro_exported?(Ecto.Type, :__using__, 1) do
    use Ecto.Type
  else
    # in order to support versions lower than 3.2
    @behaviour Ecto.Type
  end

  def type, do: Waffle.Ecto.Type.type()
  def cast(value), do: Waffle.Ecto.Type.cast(unquote(definition), value)
  def load(value), do: Waffle.Ecto.Type.load(unquote(definition), value)
  def dump(value), do: Waffle.Ecto.Type.dump(unquote(definition), value)
end

I'm also not too familiar with metaprogramming in elixir so maybe there's a trick to extend the generated .Type submodule to add the functions I want to add render_form/5 and render_index/3 to integrate a Waffle.Ecto type into Kaffy Admin.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions