Brian Kessler
Brian Kessler

Reputation: 2327

In Elixir/Phoenix, what is the correct/best way to iterate through an EctoEnum.Postgres for a select input?

We have the following enum:

defmodule PricingEngine.Pricing.ProductCategoryEnum do
  use EctoEnum.Postgres,
      type: :product_category,
      enums: [
        :shoes,
        :apparel,
        :accessories
      ]
end

In a form.html.eex template, we would like to make a selection corresponding to this enum.

Currently, we have the following code:

<%= label f, :product_category %>
<%= select f, :product_category, PricingEngine.Pricing.ProductCategoryEnum.__enums__ %>
<%= error_tag f, :product_category %>

This works, but __enums__ suggests to me that this should be treated as a private property and not consumed in our code.

Is there a better way to do this?

Upvotes: 0

Views: 503

Answers (2)

Brian Kessler
Brian Kessler

Reputation: 2327

The rest of the team also decided __enums_ looks like it shouldn't be used. Our couch suggested we should extract the list, like:

defmodule PricingEngine.Pricing.ProductCategoryEnum do
  @options [
        :shoes,
        :apparel,
        :accessories
      ]
  use EctoEnum.Postgres,
      type: :product_category,
      enums: @options

   def values, do: @options
end

Upvotes: 0

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

__enums__/0 is a perfectly valid function; private functions are made real private with defp declaration instead of def.

This is a matter of the author thinking that way they look better.

Also, perfectly legit __info__/1 is named the same.

Upvotes: 3

Related Questions