Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Dec 26, 2024
1 parent a5ee0f9 commit c263a58
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 23 additions & 0 deletions lib/repo/repo_template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,29 @@ defmodule Bonfire.Common.RepoTemplate do
end
end

def reject_preload_ids(exclude_ids) do
custom_preload_fun(fn ids -> Enum.reject(ids, &(&1 in exclude_ids)) end)
end

def custom_preload_fun(fun) do
fn ids, assoc ->
# debug(ids)
# debug(assoc)

%{related_key: related_key, queryable: queryable} = assoc

ids = fun.(ids)
# |> debug()

repo().all(
from q in queryable,
where: field(q, ^related_key) in ^ids
)

# |> debug()
end
end

defdelegate maybe_preload(obj, preloads, opts \\ []),
to: Bonfire.Common.Repo.Preload

Expand Down
4 changes: 2 additions & 2 deletions lib/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ defmodule Bonfire.Common.Utils do
nil

_ ->
if !empty?(current_user_or_socket_or_opts) do
if !empty?(current_user_or_socket_or_opts) and recursing != :skip do
debug(
current_user_or_socket_or_opts,
"No current_user found, will fallback to looking for a current_user_id",
Expand Down Expand Up @@ -270,7 +270,7 @@ defmodule Bonfire.Common.Utils do
_ ->
if recursing != :skip,
do:
current_user(current_user_or_socket_or_opts)
current_user(current_user_or_socket_or_opts, :skip)
|> Types.uid()
end ||
(
Expand Down

0 comments on commit c263a58

Please sign in to comment.