I’ve created a custom type for my Enum so I could provide labels. I couldn’t find a function that returned the values and their labels. My as_select_options() is my short term solution. Is there an out of the box function?
`defmodule MyApp.AttendanceMode do
use Ash.Type.Enum, values: [hybrid: "Hybird", in_person: "In Person", online: "Online"]
def as_select_options() do
values()
|> Enum.map(fn key -> {description(key), key} end)
end
end
There isn’t a function to do what you want, but we should make one
I’d accept a PR for something along those lines. I’d like maybe a less web-y name, like labeled_values/0? Then you can do MyApp.AttendanceMode.labeled_values() in the section options. Please open a proposal issue or a PR
@spec labeled_values() :: [{:atom, String.t()}]
@doc """
Returns a list of the values and their descriptions.
"""
def labeled_values() do
values()
|> Enum.map(fn key -> {key, description(key)} end)
end
Option 2. In AshPhoenix, maybe on Form, it could be more web-y specific.
# @spec options_for_select(Ash.Type.Enum.t()) :: [{String.t(), :atom}]
@doc """
Returns a list of the descriptions, value pairs to pass to Phoenix.HTML.Form.options_for_select/2.
"""
def options_for_select(enum) do
enum.values()
|> Enum.map(fn key -> {enum.description(key), key} end)
end