在 Elixir/Phoenix 中,correct/best 迭代 EctoEnum.Postgres 以获得 select 输入的方法是什么?
In Elixir/Phoenix, what is the correct/best way to iterate through an EctoEnum.Postgres for a select input?
我们有以下枚举:
defmodule PricingEngine.Pricing.ProductCategoryEnum do
use EctoEnum.Postgres,
type: :product_category,
enums: [
:shoes,
:apparel,
:accessories
]
end
在 form.html.eex 模板中,我们希望根据此枚举进行选择。
目前,我们有以下代码:
<%= label f, :product_category %>
<%= select f, :product_category, PricingEngine.Pricing.ProductCategoryEnum.__enums__ %>
<%= error_tag f, :product_category %>
这可行,但 __enums__
建议我将其视为私有 属性,而不是在我们的代码中使用。
有更好的方法吗?
__enums__/0
是一个完全有效的函数;私有函数通过 defp
声明而不是 def
.
实现真正的私有
这是作者认为这样更好看的问题。
此外,完全合法的 __info__/1
命名相同。
团队的其他成员也认为 __enums_ 看起来不应该使用它。
我们的沙发建议我们应该提取列表,例如:
defmodule PricingEngine.Pricing.ProductCategoryEnum do
@options [
:shoes,
:apparel,
:accessories
]
use EctoEnum.Postgres,
type: :product_category,
enums: @options
def values, do: @options
end
我们有以下枚举:
defmodule PricingEngine.Pricing.ProductCategoryEnum do
use EctoEnum.Postgres,
type: :product_category,
enums: [
:shoes,
:apparel,
:accessories
]
end
在 form.html.eex 模板中,我们希望根据此枚举进行选择。
目前,我们有以下代码:
<%= label f, :product_category %>
<%= select f, :product_category, PricingEngine.Pricing.ProductCategoryEnum.__enums__ %>
<%= error_tag f, :product_category %>
这可行,但 __enums__
建议我将其视为私有 属性,而不是在我们的代码中使用。
有更好的方法吗?
__enums__/0
是一个完全有效的函数;私有函数通过 defp
声明而不是 def
.
这是作者认为这样更好看的问题。
此外,完全合法的 __info__/1
命名相同。
团队的其他成员也认为 __enums_ 看起来不应该使用它。 我们的沙发建议我们应该提取列表,例如:
defmodule PricingEngine.Pricing.ProductCategoryEnum do
@options [
:shoes,
:apparel,
:accessories
]
use EctoEnum.Postgres,
type: :product_category,
enums: @options
def values, do: @options
end