refactor: DRY - use Mv.Constants.custom_field_prefix() instead of string literals
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
fabfe64468
commit
cf6a108049
4 changed files with 28 additions and 8 deletions
|
|
@ -18,5 +18,17 @@ defmodule Mv.Constants do
|
|||
:postal_code
|
||||
]
|
||||
|
||||
@custom_field_prefix "custom_field_"
|
||||
|
||||
def member_fields, do: @member_fields
|
||||
|
||||
@doc """
|
||||
Returns the prefix used for custom field keys in field visibility maps.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Mv.Constants.custom_field_prefix()
|
||||
"custom_field_"
|
||||
"""
|
||||
def custom_field_prefix, do: @custom_field_prefix
|
||||
end
|
||||
|
|
|
|||
|
|
@ -131,17 +131,21 @@ defmodule MvWeb.Components.FieldVisibilityDropdownComponent do
|
|||
defp extract_member_field_keys(nil), do: []
|
||||
|
||||
defp extract_member_field_keys(fields) do
|
||||
prefix = Mv.Constants.custom_field_prefix()
|
||||
|
||||
Enum.filter(fields, fn field ->
|
||||
is_atom(field) ||
|
||||
(is_binary(field) && not String.starts_with?(field, "custom_field_"))
|
||||
(is_binary(field) && not String.starts_with?(field, prefix))
|
||||
end)
|
||||
end
|
||||
|
||||
defp extract_custom_field_keys(nil), do: []
|
||||
|
||||
defp extract_custom_field_keys(fields) do
|
||||
prefix = Mv.Constants.custom_field_prefix()
|
||||
|
||||
Enum.filter(fields, fn field ->
|
||||
is_binary(field) && String.starts_with?(field, "custom_field_")
|
||||
is_binary(field) && String.starts_with?(field, prefix)
|
||||
end)
|
||||
end
|
||||
|
||||
|
|
@ -157,7 +161,7 @@ defmodule MvWeb.Components.FieldVisibilityDropdownComponent do
|
|||
end
|
||||
|
||||
defp format_custom_field_label(field_string, custom_fields) do
|
||||
id = String.trim_leading(field_string, "custom_field_")
|
||||
id = String.trim_leading(field_string, Mv.Constants.custom_field_prefix())
|
||||
find_custom_field_name(id, field_string, custom_fields)
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ defmodule MvWeb.MemberLive.Index do
|
|||
alias MvWeb.MemberLive.Index.FieldVisibility
|
||||
|
||||
# Prefix used in sort field names for custom fields (e.g., "custom_field_<id>")
|
||||
@custom_field_prefix "custom_field_"
|
||||
@custom_field_prefix Mv.Constants.custom_field_prefix()
|
||||
|
||||
# Member fields that are loaded for the overview
|
||||
# Uses constants from Mv.Constants to ensure consistency
|
||||
|
|
@ -796,7 +796,7 @@ defmodule MvWeb.MemberLive.Index do
|
|||
# Format: "custom_field_<id>" -> <id>
|
||||
defp extract_custom_field_ids(visible_custom_fields) do
|
||||
Enum.map(visible_custom_fields, fn field_string ->
|
||||
case String.split(field_string, "custom_field_") do
|
||||
case String.split(field_string, @custom_field_prefix) do
|
||||
["", id] -> id
|
||||
_ -> nil
|
||||
end
|
||||
|
|
|
|||
|
|
@ -155,9 +155,11 @@ defmodule MvWeb.MemberLive.Index.FieldVisibility do
|
|||
"""
|
||||
@spec get_visible_custom_fields(%{String.t() => boolean()}) :: [String.t()]
|
||||
def get_visible_custom_fields(field_selection) when is_map(field_selection) do
|
||||
prefix = Mv.Constants.custom_field_prefix()
|
||||
|
||||
field_selection
|
||||
|> Enum.filter(fn {field_string, visible} ->
|
||||
visible && String.starts_with?(field_string, "custom_field_")
|
||||
visible && String.starts_with?(field_string, prefix)
|
||||
end)
|
||||
|> Enum.map(fn {field_string, _visible} -> field_string end)
|
||||
end
|
||||
|
|
@ -188,8 +190,10 @@ defmodule MvWeb.MemberLive.Index.FieldVisibility do
|
|||
|
||||
# Gets custom field visibility (all custom fields with show_in_overview=true are visible)
|
||||
defp get_custom_field_visibility(custom_fields) do
|
||||
prefix = Mv.Constants.custom_field_prefix()
|
||||
|
||||
Enum.reduce(custom_fields, %{}, fn custom_field, acc ->
|
||||
field_string = "custom_field_#{custom_field.id}"
|
||||
field_string = "#{prefix}#{custom_field.id}"
|
||||
visible = Map.get(custom_field, :show_in_overview, true)
|
||||
Map.put(acc, field_string, visible)
|
||||
end)
|
||||
|
|
@ -218,7 +222,7 @@ defmodule MvWeb.MemberLive.Index.FieldVisibility do
|
|||
|
||||
# Converts field string to atom (for member fields) or keeps as string (for custom fields)
|
||||
defp to_field_identifier(field_string) when is_binary(field_string) do
|
||||
if String.starts_with?(field_string, "custom_field_") do
|
||||
if String.starts_with?(field_string, Mv.Constants.custom_field_prefix()) do
|
||||
field_string
|
||||
else
|
||||
try do
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue