style: format codebase
This commit is contained in:
@@ -29,10 +29,12 @@ defmodule ComponentsElixir.Inventory.Hierarchical do
|
||||
case parent_accessor_fn.(entity) do
|
||||
nil ->
|
||||
entity.name
|
||||
|
||||
%Ecto.Association.NotLoaded{} ->
|
||||
# Parent not loaded - fall back to database lookup
|
||||
# This is a fallback and should be rare if preloading is done correctly
|
||||
build_path_with_db_lookup(entity, separator)
|
||||
|
||||
parent ->
|
||||
"#{full_path(parent, parent_accessor_fn, separator)}#{separator}#{entity.name}"
|
||||
end
|
||||
@@ -52,12 +54,14 @@ defmodule ComponentsElixir.Inventory.Hierarchical do
|
||||
nil ->
|
||||
# This is a root entity, add its name and return the complete path
|
||||
[entity.name | path_so_far]
|
||||
|
||||
parent_id ->
|
||||
# Load parent from database
|
||||
case load_parent_entity(entity, parent_id) do
|
||||
nil ->
|
||||
# Parent not found (orphaned record), treat this as root
|
||||
[entity.name | path_so_far]
|
||||
|
||||
parent ->
|
||||
# Recursively get the path from the parent, then add current entity
|
||||
collect_path_from_root(parent, [entity.name | path_so_far])
|
||||
@@ -93,9 +97,9 @@ defmodule ComponentsElixir.Inventory.Hierarchical do
|
||||
entity_id = id_accessor_fn.(entity)
|
||||
|
||||
# Remove self-reference
|
||||
entity_id == editing_entity_id ||
|
||||
# Remove descendants (they would create a cycle)
|
||||
descendant?(entities, entity_id, editing_entity_id, parent_id_accessor_fn)
|
||||
entity_id == editing_entity_id ||
|
||||
descendant?(entities, entity_id, editing_entity_id, parent_id_accessor_fn)
|
||||
end)
|
||||
end
|
||||
|
||||
@@ -114,13 +118,21 @@ defmodule ComponentsElixir.Inventory.Hierarchical do
|
||||
|
||||
defp descendant_recursive?(entities, entity, ancestor_id, parent_id_accessor_fn) do
|
||||
case parent_id_accessor_fn.(entity) do
|
||||
nil -> false
|
||||
^ancestor_id -> true
|
||||
nil ->
|
||||
false
|
||||
|
||||
^ancestor_id ->
|
||||
true
|
||||
|
||||
parent_id ->
|
||||
parent = Enum.find(entities, fn e -> e.id == parent_id end)
|
||||
|
||||
case parent do
|
||||
nil -> false
|
||||
parent_entity -> descendant_recursive?(entities, parent_entity, ancestor_id, parent_id_accessor_fn)
|
||||
nil ->
|
||||
false
|
||||
|
||||
parent_entity ->
|
||||
descendant_recursive?(entities, parent_entity, ancestor_id, parent_id_accessor_fn)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -182,15 +194,20 @@ defmodule ComponentsElixir.Inventory.Hierarchical do
|
||||
Includes proper filtering to prevent cycles and formatted display names.
|
||||
Results are sorted hierarchically for intuitive navigation.
|
||||
"""
|
||||
def parent_select_options(entities, editing_entity_id, parent_accessor_fn, nil_option_text \\ "No parent") do
|
||||
def parent_select_options(
|
||||
entities,
|
||||
editing_entity_id,
|
||||
parent_accessor_fn,
|
||||
nil_option_text \\ "No parent"
|
||||
) do
|
||||
available_entities =
|
||||
filter_parent_options(
|
||||
entities,
|
||||
editing_entity_id,
|
||||
&(&1.id),
|
||||
&(&1.parent_id)
|
||||
& &1.id,
|
||||
& &1.parent_id
|
||||
)
|
||||
|> sort_hierarchically(&(&1.parent_id))
|
||||
|> sort_hierarchically(& &1.parent_id)
|
||||
|> Enum.map(fn entity ->
|
||||
{display_name(entity, parent_accessor_fn), entity.id}
|
||||
end)
|
||||
@@ -205,7 +222,7 @@ defmodule ComponentsElixir.Inventory.Hierarchical do
|
||||
def select_options(entities, parent_accessor_fn, nil_option_text \\ nil) do
|
||||
sorted_entities =
|
||||
entities
|
||||
|> sort_hierarchically(&(&1.parent_id))
|
||||
|> sort_hierarchically(& &1.parent_id)
|
||||
|> Enum.map(fn entity ->
|
||||
{display_name(entity, parent_accessor_fn), entity.id}
|
||||
end)
|
||||
@@ -300,9 +317,10 @@ defmodule ComponentsElixir.Inventory.Hierarchical do
|
||||
descendant_ids_only = List.delete(all_descendant_ids, entity_id)
|
||||
|
||||
# Sum counts for all descendants
|
||||
children_count = Enum.reduce(descendant_ids_only, 0, fn id, acc ->
|
||||
acc + count_fn.(id)
|
||||
end)
|
||||
children_count =
|
||||
Enum.reduce(descendant_ids_only, 0, fn id, acc ->
|
||||
acc + count_fn.(id)
|
||||
end)
|
||||
|
||||
{self_count, children_count, self_count + children_count}
|
||||
end
|
||||
@@ -320,7 +338,13 @@ defmodule ComponentsElixir.Inventory.Hierarchical do
|
||||
- singular_noun: What to call a single item (default: "component")
|
||||
- plural_noun: What to call multiple items (default: "components")
|
||||
"""
|
||||
def format_count_display(self_count, children_count, is_expanded, singular_noun \\ "component", plural_noun \\ "components") do
|
||||
def format_count_display(
|
||||
self_count,
|
||||
children_count,
|
||||
is_expanded,
|
||||
singular_noun \\ "component",
|
||||
plural_noun \\ "components"
|
||||
) do
|
||||
total_count = self_count + children_count
|
||||
|
||||
count_noun = if total_count == 1, do: singular_noun, else: plural_noun
|
||||
|
||||
Reference in New Issue
Block a user