feat: category filter includes subcategories
This commit is contained in:
@@ -143,6 +143,32 @@ defmodule ComponentsElixir.Inventory.Hierarchical do
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets all descendant IDs for a given entity ID, including the entity itself.
|
||||
This recursively finds all children, grandchildren, etc.
|
||||
|
||||
## Examples
|
||||
iex> categories = [
|
||||
...> %{id: 1, parent_id: nil},
|
||||
...> %{id: 2, parent_id: 1},
|
||||
...> %{id: 3, parent_id: 2},
|
||||
...> %{id: 4, parent_id: 1}
|
||||
...> ]
|
||||
iex> Hierarchical.descendant_ids(categories, 1, &(&1.parent_id))
|
||||
[1, 2, 3, 4]
|
||||
"""
|
||||
def descendant_ids(entities, entity_id, parent_id_accessor_fn) do
|
||||
[entity_id | get_descendant_ids_recursive(entities, entity_id, parent_id_accessor_fn)]
|
||||
end
|
||||
|
||||
defp get_descendant_ids_recursive(entities, parent_id, parent_id_accessor_fn) do
|
||||
children = child_entities(entities, parent_id, parent_id_accessor_fn)
|
||||
|
||||
Enum.flat_map(children, fn child ->
|
||||
[child.id | get_descendant_ids_recursive(entities, child.id, parent_id_accessor_fn)]
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Generates display name for entity including parent context.
|
||||
For dropdown displays: "Parent > Child"
|
||||
|
||||
Reference in New Issue
Block a user