-
-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a function to get Enum descriptions and values. #1724
Comments
I'll have some time soon to take a stab at adding this feature. If you don't mind, I have a few questions to make sure the implementation meets the needs properly. What would you like to happen if the description's are nil?defmodule MyApp.Types.Status do
use Ash.Type.Enum, values: [:open, :closed] # <- No description provided so `description/1` returns nil
end If you have more than one nil description, you'll only get one of them returned when calling Is the description for the enum what you want or the string representation of the value?The description is meant to be as the name suggests, a description of what the enum value is and likely not something you'd put in a select field. I think this might be more useful for select fields if we did something similar to @doc """
Returns a list of the values by their string representation for use in UI select fields
"""
@spec labeled_values() :: [{String.t(), :atom}]
def labeled_values() do
Enum.map(values(), fn value -> {humanize(value), value} end) # <- [{"Open", :open}, {"Closed", :closed}]
end
defp humanize(value) when is_atom(value) do
value
|> to_string()
|> humanize()
end
defp humanize(value) when is_binary(value) do
value
|> String.replace(~r([^A-Za-z]), " ")
|> String.capitalize()
end Where could this be used in
|
hmm...alright, I think maybe you are right and it should go in |
The specific need I asked about in my Elixir Forum Post was for a function that supported using Enum descriptions and values for select input options on Phoenix Forms.
Is your feature request related to a problem? Please describe.
Creating a custom Enum invites the opportunity to provide labels or descriptions for the values. There's presently no function that returns the labels and descriptions. It seems a pretty natural fit to leverage the descriptions as labels in Phoenix forms.
Describe the solution you'd like
Add a function to Ash.Type.Enum that returns the labels and descriptions.
Describe alternatives you've considered
Express the feature either with a change to resource syntax, or with a change to the resource interface
On Ash.Type.Enum
Or in AshPhoenix...
The text was updated successfully, but these errors were encountered: