Replace local attr helpers
This commit is contained in:
@@ -16,11 +16,12 @@ defmodule BDS.ImportDefinitions do
|
||||
%ImportDefinition{}
|
||||
|> ImportDefinition.changeset(%{
|
||||
id: Ecto.UUID.generate(),
|
||||
project_id: attr(attrs, :project_id),
|
||||
name: attr(attrs, :name) || "",
|
||||
wxr_file_path: attr(attrs, :wxr_file_path),
|
||||
uploads_folder_path: attr(attrs, :uploads_folder_path),
|
||||
last_analysis_result: normalize_analysis_result(attr(attrs, :last_analysis_result)),
|
||||
project_id: BDS.MapUtils.attr(attrs, :project_id),
|
||||
name: BDS.MapUtils.attr(attrs, :name) || "",
|
||||
wxr_file_path: BDS.MapUtils.attr(attrs, :wxr_file_path),
|
||||
uploads_folder_path: BDS.MapUtils.attr(attrs, :uploads_folder_path),
|
||||
last_analysis_result:
|
||||
normalize_analysis_result(BDS.MapUtils.attr(attrs, :last_analysis_result)),
|
||||
created_at: now,
|
||||
updated_at: now
|
||||
})
|
||||
@@ -39,12 +40,12 @@ defmodule BDS.ImportDefinitions do
|
||||
%ImportDefinition{} = definition ->
|
||||
updates =
|
||||
%{}
|
||||
|> maybe_put(:name, attr(attrs, :name))
|
||||
|> maybe_put(:wxr_file_path, attr(attrs, :wxr_file_path))
|
||||
|> maybe_put(:uploads_folder_path, attr(attrs, :uploads_folder_path))
|
||||
|> maybe_put(:name, BDS.MapUtils.attr(attrs, :name))
|
||||
|> maybe_put(:wxr_file_path, BDS.MapUtils.attr(attrs, :wxr_file_path))
|
||||
|> maybe_put(:uploads_folder_path, BDS.MapUtils.attr(attrs, :uploads_folder_path))
|
||||
|> maybe_put(
|
||||
:last_analysis_result,
|
||||
normalize_analysis_result(attr(attrs, :last_analysis_result))
|
||||
normalize_analysis_result(BDS.MapUtils.attr(attrs, :last_analysis_result))
|
||||
)
|
||||
|> Map.put(:updated_at, Persistence.now_ms())
|
||||
|
||||
@@ -85,8 +86,6 @@ defmodule BDS.ImportDefinitions do
|
||||
)
|
||||
end
|
||||
|
||||
defp attr(attrs, key), do: Map.get(attrs, key) || Map.get(attrs, Atom.to_string(key))
|
||||
|
||||
defp maybe_put(map, _key, nil), do: map
|
||||
defp maybe_put(map, key, value), do: Map.put(map, key, value)
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule BDS.Menu do
|
||||
@moduledoc false
|
||||
|
||||
import BDS.MapUtils, only: [attr: 2]
|
||||
|
||||
alias BDS.Persistence
|
||||
alias BDS.Projects
|
||||
|
||||
@@ -263,11 +265,4 @@ defmodule BDS.Menu do
|
||||
|> String.replace(~s("), """)
|
||||
end
|
||||
|
||||
defp attr(attrs, key) do
|
||||
cond do
|
||||
Map.has_key?(attrs, key) -> Map.get(attrs, key)
|
||||
Map.has_key?(attrs, Atom.to_string(key)) -> Map.get(attrs, Atom.to_string(key))
|
||||
true -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule BDS.Metadata do
|
||||
@moduledoc false
|
||||
|
||||
import BDS.MapUtils, only: [attr: 2, attr: 3]
|
||||
|
||||
require Logger
|
||||
|
||||
alias BDS.Embeddings
|
||||
@@ -674,19 +676,4 @@ defmodule BDS.Metadata do
|
||||
defp maybe_backfill_embeddings(result, _project_id, _previous_state, _project_metadata),
|
||||
do: result
|
||||
|
||||
defp attr(attrs, key) do
|
||||
cond do
|
||||
Map.has_key?(attrs, key) -> Map.get(attrs, key)
|
||||
Map.has_key?(attrs, Atom.to_string(key)) -> Map.get(attrs, Atom.to_string(key))
|
||||
true -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp attr(attrs, key, default) do
|
||||
cond do
|
||||
Map.has_key?(attrs, key) -> Map.get(attrs, key)
|
||||
Map.has_key?(attrs, Atom.to_string(key)) -> Map.get(attrs, Atom.to_string(key))
|
||||
true -> default
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,6 +3,7 @@ defmodule BDS.Projects do
|
||||
|
||||
import Ecto.Query
|
||||
|
||||
alias BDS.MapUtils
|
||||
alias BDS.Metadata
|
||||
alias BDS.Persistence
|
||||
alias BDS.Projects.Project
|
||||
@@ -151,8 +152,8 @@ defmodule BDS.Projects do
|
||||
@spec create_project(attrs()) :: {:ok, Project.t()} | {:error, term()}
|
||||
def create_project(attrs) do
|
||||
now = Persistence.now_ms()
|
||||
name = attr(attrs, :name) || ""
|
||||
slug = unique_slug(attr(attrs, :slug) || Slug.slugify(name))
|
||||
name = MapUtils.attr(attrs, :name) || ""
|
||||
slug = unique_slug(MapUtils.attr(attrs, :slug) || Slug.slugify(name))
|
||||
|
||||
retry_create_project(fn ->
|
||||
Repo.transaction(fn ->
|
||||
@@ -162,8 +163,8 @@ defmodule BDS.Projects do
|
||||
id: Ecto.UUID.generate(),
|
||||
name: name,
|
||||
slug: slug,
|
||||
description: attr(attrs, :description),
|
||||
data_path: attr(attrs, :data_path),
|
||||
description: MapUtils.attr(attrs, :description),
|
||||
data_path: MapUtils.attr(attrs, :data_path),
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
is_active: false
|
||||
@@ -391,11 +392,4 @@ defmodule BDS.Projects do
|
||||
File.write(path, Jason.encode!(registry))
|
||||
end
|
||||
|
||||
defp attr(attrs, key) do
|
||||
cond do
|
||||
Map.has_key?(attrs, key) -> Map.get(attrs, key)
|
||||
Map.has_key?(attrs, Atom.to_string(key)) -> Map.get(attrs, Atom.to_string(key))
|
||||
true -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,6 +3,7 @@ defmodule BDS.Tags do
|
||||
|
||||
import Ecto.Query
|
||||
|
||||
alias BDS.MapUtils
|
||||
alias BDS.Persistence
|
||||
alias BDS.Posts
|
||||
alias BDS.Posts.Post
|
||||
@@ -16,8 +17,8 @@ defmodule BDS.Tags do
|
||||
|
||||
@spec create_tag(attrs()) :: tag_result()
|
||||
def create_tag(attrs) do
|
||||
project_id = attr(attrs, :project_id)
|
||||
name = attr(attrs, :name) |> to_string() |> String.trim()
|
||||
project_id = MapUtils.attr(attrs, :project_id)
|
||||
name = MapUtils.attr(attrs, :name) |> to_string() |> String.trim()
|
||||
|
||||
with :ok <- validate_unique_name(project_id, name) do
|
||||
now = Persistence.now_ms()
|
||||
@@ -27,8 +28,8 @@ defmodule BDS.Tags do
|
||||
id: Ecto.UUID.generate(),
|
||||
project_id: project_id,
|
||||
name: name,
|
||||
color: attr(attrs, :color),
|
||||
post_template_slug: attr(attrs, :post_template_slug),
|
||||
color: MapUtils.attr(attrs, :color),
|
||||
post_template_slug: MapUtils.attr(attrs, :post_template_slug),
|
||||
created_at: now,
|
||||
updated_at: now
|
||||
})
|
||||
@@ -115,8 +116,8 @@ defmodule BDS.Tags do
|
||||
|
||||
tag ->
|
||||
updates = %{
|
||||
color: attr(attrs, :color),
|
||||
post_template_slug: attr(attrs, :post_template_slug),
|
||||
color: MapUtils.attr(attrs, :color),
|
||||
post_template_slug: MapUtils.attr(attrs, :post_template_slug),
|
||||
updated_at: Persistence.now_ms()
|
||||
}
|
||||
|
||||
@@ -389,11 +390,4 @@ defmodule BDS.Tags do
|
||||
defp maybe_put(map, _key, ""), do: map
|
||||
defp maybe_put(map, key, value), do: Map.put(map, key, value)
|
||||
|
||||
defp attr(attrs, key) do
|
||||
cond do
|
||||
Map.has_key?(attrs, key) -> Map.get(attrs, key)
|
||||
Map.has_key?(attrs, Atom.to_string(key)) -> Map.get(attrs, Atom.to_string(key))
|
||||
true -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -373,8 +373,8 @@ defmodule BDS.Tasks do
|
||||
status: status,
|
||||
progress: nil,
|
||||
message: nil,
|
||||
group_id: attr(attrs, :group_id),
|
||||
group_name: attr(attrs, :group_name),
|
||||
group_id: BDS.MapUtils.attr(attrs, :group_id),
|
||||
group_name: BDS.MapUtils.attr(attrs, :group_name),
|
||||
created_at: DateTime.utc_now(),
|
||||
started_at: nil,
|
||||
finished_at: nil,
|
||||
@@ -570,11 +570,4 @@ defmodule BDS.Tasks do
|
||||
|> Keyword.get(:finished_task_ttl_ms, @default_finished_task_ttl_ms)
|
||||
end
|
||||
|
||||
defp attr(attrs, key) do
|
||||
cond do
|
||||
Map.has_key?(attrs, key) -> Map.get(attrs, key)
|
||||
Map.has_key?(attrs, Atom.to_string(key)) -> Map.get(attrs, Atom.to_string(key))
|
||||
true -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user