From 412eca18dabd1175f2cfd77e030adac6dc2e087f Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Thu, 7 Jan 2021 22:18:20 -0500 Subject: [PATCH] improvement: support arguments in form_data --- lib/ash_phoenix/changeset/form_data.ex | 28 ++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/ash_phoenix/changeset/form_data.ex b/lib/ash_phoenix/changeset/form_data.ex index 5042e53..dee0431 100644 --- a/lib/ash_phoenix/changeset/form_data.ex +++ b/lib/ash_phoenix/changeset/form_data.ex @@ -6,9 +6,31 @@ defimpl Phoenix.HTML.FormData, for: Ash.Changeset do # is much better than ours. Unsurprising, the current system # was simply tacked on based on the API error system. - def input_type(%{resource: resource}, _, field) do - type = Ash.Resource.attribute(resource, field) + def input_type(%{resource: resource, action: action}, _, field) do + attribute = Ash.Resource.attribute(resource, field) + if attribute do + type_to_form_type(attribute.type) + else + argument = get_argument(action, field) + + if argument do + type_to_form_type(argument.type) + else + :text_input + end + end + end + + defp get_argument(action, field) when is_atom(field) do + Enum.find(action.arguments, &(&1.name == field)) + end + + defp get_argument(action, field) when is_binary(field) do + Enum.find(action.arguments, &(to_string(&1.name) == field)) + end + + defp type_to_form_type(type) do case Ash.Type.ecto_type(type) do :integer -> :number_input :boolean -> :checkbox @@ -21,8 +43,6 @@ defimpl Phoenix.HTML.FormData, for: Ash.Changeset do end def input_validations(_changeset, _form, _field) do - # Ash.Changeset. - # [required: ] [] end