Skip to content

accept value_fn for {:array, _} field#321

Open
onizuka8 wants to merge 1 commit into
aesmail:masterfrom
onizuka8:feat/allow_value_fn_for_array_field
Open

accept value_fn for {:array, _} field#321
onizuka8 wants to merge 1 commit into
aesmail:masterfrom
onizuka8:feat/allow_value_fn_for_array_field

Conversation

@onizuka8
Copy link
Copy Markdown

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

@ghenry
Copy link
Copy Markdown
Collaborator

ghenry commented Dec 11, 2024

Hi,

Thanks for your contribution!

Is this still required and works on the latest version?

Gavin.

@onizuka8 onizuka8 force-pushed the feat/allow_value_fn_for_array_field branch from 861d810 to 7172d43 Compare February 5, 2025 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants