refactor: cleanup mix credo issues
This commit is contained in:
@@ -143,21 +143,7 @@ defmodule ComponentsElixir.AprilTag do
|
||||
results =
|
||||
all_apriltag_ids()
|
||||
|> Task.async_stream(
|
||||
fn apriltag_id ->
|
||||
filename = "tag36h11_id_#{String.pad_leading(to_string(apriltag_id), 3, "0")}.svg"
|
||||
file_path = Path.join(static_dir, filename)
|
||||
|
||||
if force_regenerate || !File.exists?(file_path) do
|
||||
svg_content = generate_apriltag_svg(apriltag_id, opts)
|
||||
|
||||
case File.write(file_path, svg_content) do
|
||||
:ok -> {:ok, apriltag_id, file_path}
|
||||
{:error, reason} -> {:error, apriltag_id, reason}
|
||||
end
|
||||
else
|
||||
{:ok, apriltag_id, file_path}
|
||||
end
|
||||
end,
|
||||
&generate_apriltag_file(&1, static_dir, force_regenerate, opts),
|
||||
timeout: :infinity,
|
||||
max_concurrency: System.schedulers_online() * 2
|
||||
)
|
||||
@@ -174,6 +160,26 @@ defmodule ComponentsElixir.AprilTag do
|
||||
}
|
||||
end
|
||||
|
||||
defp generate_apriltag_file(apriltag_id, static_dir, force_regenerate, opts) do
|
||||
filename = "tag36h11_id_#{String.pad_leading(to_string(apriltag_id), 3, "0")}.svg"
|
||||
file_path = Path.join(static_dir, filename)
|
||||
|
||||
if force_regenerate || !File.exists?(file_path) do
|
||||
write_apriltag_file(apriltag_id, file_path, opts)
|
||||
else
|
||||
{:ok, apriltag_id, file_path}
|
||||
end
|
||||
end
|
||||
|
||||
defp write_apriltag_file(apriltag_id, file_path, opts) do
|
||||
svg_content = generate_apriltag_svg(apriltag_id, opts)
|
||||
|
||||
case File.write(file_path, svg_content) do
|
||||
:ok -> {:ok, apriltag_id, file_path}
|
||||
{:error, reason} -> {:error, apriltag_id, reason}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Cleans up AprilTag SVG file for a specific ID.
|
||||
|
||||
|
||||
@@ -288,18 +288,25 @@ defmodule ComponentsElixir.Inventory do
|
||||
end
|
||||
|
||||
defp apply_component_sorting(query, opts) do
|
||||
case Keyword.get(opts, :sort_criteria, "name_asc") do
|
||||
"name_asc" -> order_by(query, [c], [asc: c.name, asc: c.id])
|
||||
"name_desc" -> order_by(query, [c], [desc: c.name, asc: c.id])
|
||||
"inserted_at_asc" -> order_by(query, [c], [asc: c.inserted_at, asc: c.id])
|
||||
"inserted_at_desc" -> order_by(query, [c], [desc: c.inserted_at, asc: c.id])
|
||||
"updated_at_asc" -> order_by(query, [c], [asc: c.updated_at, asc: c.id])
|
||||
"updated_at_desc" -> order_by(query, [c], [desc: c.updated_at, asc: c.id])
|
||||
"count_asc" -> order_by(query, [c], [asc: c.count, asc: c.id])
|
||||
"count_desc" -> order_by(query, [c], [desc: c.count, asc: c.id])
|
||||
# Default fallback
|
||||
_ -> order_by(query, [c], [asc: c.name, asc: c.id])
|
||||
end
|
||||
sort_criteria = Keyword.get(opts, :sort_criteria, "name_asc")
|
||||
sort_order = get_sort_order(sort_criteria)
|
||||
order_by(query, [c], ^sort_order)
|
||||
end
|
||||
|
||||
# Map of sort criteria to their corresponding sort orders
|
||||
@sort_orders %{
|
||||
"name_asc" => [asc: :name, asc: :id],
|
||||
"name_desc" => [desc: :name, asc: :id],
|
||||
"inserted_at_asc" => [asc: :inserted_at, asc: :id],
|
||||
"inserted_at_desc" => [desc: :inserted_at, asc: :id],
|
||||
"updated_at_asc" => [asc: :updated_at, asc: :id],
|
||||
"updated_at_desc" => [desc: :updated_at, asc: :id],
|
||||
"count_asc" => [asc: :count, asc: :id],
|
||||
"count_desc" => [desc: :count, asc: :id]
|
||||
}
|
||||
|
||||
defp get_sort_order(criteria) do
|
||||
Map.get(@sort_orders, criteria, [asc: :name, asc: :id])
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
||||
@@ -70,17 +70,21 @@ defmodule ComponentsElixir.Inventory.Component do
|
||||
|
||||
defp validate_url(changeset, field) do
|
||||
validate_change(changeset, field, fn ^field, url ->
|
||||
if url && url != "" do
|
||||
case URI.parse(url) do
|
||||
%URI{scheme: scheme} when scheme in ["http", "https"] -> []
|
||||
_ -> [{field, "must be a valid URL"}]
|
||||
end
|
||||
else
|
||||
[]
|
||||
cond do
|
||||
is_nil(url) or url == "" -> []
|
||||
valid_url?(url) -> []
|
||||
true -> [{field, "must be a valid URL"}]
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp valid_url?(url) do
|
||||
case URI.parse(url) do
|
||||
%URI{scheme: scheme} when scheme in ["http", "https"] -> true
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns true if the component has an image.
|
||||
"""
|
||||
|
||||
@@ -95,7 +95,7 @@ defmodule ComponentsElixir.Inventory.Hierarchical do
|
||||
# Remove self-reference
|
||||
entity_id == editing_entity_id ||
|
||||
# Remove descendants (they would create a cycle)
|
||||
is_descendant?(entities, entity_id, editing_entity_id, parent_id_accessor_fn)
|
||||
descendant?(entities, entity_id, editing_entity_id, parent_id_accessor_fn)
|
||||
end)
|
||||
end
|
||||
|
||||
@@ -103,16 +103,16 @@ defmodule ComponentsElixir.Inventory.Hierarchical do
|
||||
Checks if an entity is a descendant of an ancestor entity.
|
||||
Used for cycle detection in parent selection.
|
||||
"""
|
||||
def is_descendant?(entities, descendant_id, ancestor_id, parent_id_accessor_fn) do
|
||||
def descendant?(entities, descendant_id, ancestor_id, parent_id_accessor_fn) do
|
||||
descendant = Enum.find(entities, fn e -> e.id == descendant_id end)
|
||||
|
||||
case descendant do
|
||||
nil -> false
|
||||
entity -> is_descendant_recursive(entities, entity, ancestor_id, parent_id_accessor_fn)
|
||||
entity -> descendant_recursive?(entities, entity, ancestor_id, parent_id_accessor_fn)
|
||||
end
|
||||
end
|
||||
|
||||
defp is_descendant_recursive(entities, entity, ancestor_id, parent_id_accessor_fn) 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
|
||||
@@ -120,7 +120,7 @@ defmodule ComponentsElixir.Inventory.Hierarchical do
|
||||
parent = Enum.find(entities, fn e -> e.id == parent_id end)
|
||||
case parent do
|
||||
nil -> false
|
||||
parent_entity -> is_descendant_recursive(entities, parent_entity, ancestor_id, parent_id_accessor_fn)
|
||||
parent_entity -> descendant_recursive?(entities, parent_entity, ancestor_id, parent_id_accessor_fn)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user