kaffy icon indicating copy to clipboard operation
kaffy copied to clipboard

accept value_fn for {:array, _} field

Open onizuka8 opened this issue 1 year ago • 1 comments

This change will allow to define a :value_fn for any {:array, _} field. I use it as work-around for many_to_many associations. What I do is adding the field in the Kaffy.MyModelAdmin.form_fields/1

def form_fields(schema) do 
    Kaffy.ResourceSchema.form_fields(schema)
    |> Keyword.put(:associated_entities, %{
      type: {:array, :integer},
      value_fn: fn model ->
        Repo.preload(model, [:other_entities])
        |> Map.get(:other_entities)
        |> Enum.map(& &1.id)
      end,
      values_fn: fn _, _ ->
        Repo.all(Models.Entity)
        |> Enum.map(&{&1.name, &1.id})
      end
    })
end

onizuka8 avatar Aug 29 '24 08:08 onizuka8

Hi,

Thanks for your contribution!

Is this still required and works on the latest version?

Gavin.

ghenry avatar Dec 11 '24 19:12 ghenry