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
|
:postal_code
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@custom_field_prefix "custom_field_"
|
||||||
|
|
||||||
def member_fields, do: @member_fields
|
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
|
end
|
||||||
|
|
|
||||||
|
|
@ -131,17 +131,21 @@ defmodule MvWeb.Components.FieldVisibilityDropdownComponent do
|
||||||
defp extract_member_field_keys(nil), do: []
|
defp extract_member_field_keys(nil), do: []
|
||||||
|
|
||||||
defp extract_member_field_keys(fields) do
|
defp extract_member_field_keys(fields) do
|
||||||
|
prefix = Mv.Constants.custom_field_prefix()
|
||||||
|
|
||||||
Enum.filter(fields, fn field ->
|
Enum.filter(fields, fn field ->
|
||||||
is_atom(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)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp extract_custom_field_keys(nil), do: []
|
defp extract_custom_field_keys(nil), do: []
|
||||||
|
|
||||||
defp extract_custom_field_keys(fields) do
|
defp extract_custom_field_keys(fields) do
|
||||||
|
prefix = Mv.Constants.custom_field_prefix()
|
||||||
|
|
||||||
Enum.filter(fields, fn field ->
|
Enum.filter(fields, fn field ->
|
||||||
is_binary(field) && String.starts_with?(field, "custom_field_")
|
is_binary(field) && String.starts_with?(field, prefix)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -157,7 +161,7 @@ defmodule MvWeb.Components.FieldVisibilityDropdownComponent do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_custom_field_label(field_string, custom_fields) do
|
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)
|
find_custom_field_name(id, field_string, custom_fields)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ defmodule MvWeb.MemberLive.Index do
|
||||||
alias MvWeb.MemberLive.Index.FieldVisibility
|
alias MvWeb.MemberLive.Index.FieldVisibility
|
||||||
|
|
||||||
# Prefix used in sort field names for custom fields (e.g., "custom_field_<id>")
|
# 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
|
# Member fields that are loaded for the overview
|
||||||
# Uses constants from Mv.Constants to ensure consistency
|
# Uses constants from Mv.Constants to ensure consistency
|
||||||
|
|
@ -796,7 +796,7 @@ defmodule MvWeb.MemberLive.Index do
|
||||||
# Format: "custom_field_<id>" -> <id>
|
# Format: "custom_field_<id>" -> <id>
|
||||||
defp extract_custom_field_ids(visible_custom_fields) do
|
defp extract_custom_field_ids(visible_custom_fields) do
|
||||||
Enum.map(visible_custom_fields, fn field_string ->
|
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
|
["", id] -> id
|
||||||
_ -> nil
|
_ -> nil
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -155,9 +155,11 @@ defmodule MvWeb.MemberLive.Index.FieldVisibility do
|
||||||
"""
|
"""
|
||||||
@spec get_visible_custom_fields(%{String.t() => boolean()}) :: [String.t()]
|
@spec get_visible_custom_fields(%{String.t() => boolean()}) :: [String.t()]
|
||||||
def get_visible_custom_fields(field_selection) when is_map(field_selection) do
|
def get_visible_custom_fields(field_selection) when is_map(field_selection) do
|
||||||
|
prefix = Mv.Constants.custom_field_prefix()
|
||||||
|
|
||||||
field_selection
|
field_selection
|
||||||
|> Enum.filter(fn {field_string, visible} ->
|
|> Enum.filter(fn {field_string, visible} ->
|
||||||
visible && String.starts_with?(field_string, "custom_field_")
|
visible && String.starts_with?(field_string, prefix)
|
||||||
end)
|
end)
|
||||||
|> Enum.map(fn {field_string, _visible} -> field_string end)
|
|> Enum.map(fn {field_string, _visible} -> field_string end)
|
||||||
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)
|
# Gets custom field visibility (all custom fields with show_in_overview=true are visible)
|
||||||
defp get_custom_field_visibility(custom_fields) do
|
defp get_custom_field_visibility(custom_fields) do
|
||||||
|
prefix = Mv.Constants.custom_field_prefix()
|
||||||
|
|
||||||
Enum.reduce(custom_fields, %{}, fn custom_field, acc ->
|
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)
|
visible = Map.get(custom_field, :show_in_overview, true)
|
||||||
Map.put(acc, field_string, visible)
|
Map.put(acc, field_string, visible)
|
||||||
end)
|
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)
|
# 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
|
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
|
field_string
|
||||||
else
|
else
|
||||||
try do
|
try do
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue