Skip to content

Commit

Permalink
Do not trigger preloads for brainstorming when unneeded
Browse files Browse the repository at this point in the history
  • Loading branch information
PragTob committed Nov 30, 2024
1 parent 8a20554 commit bc36a24
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
7 changes: 6 additions & 1 deletion lib/mindwendel/brainstormings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ defmodule Mindwendel.Brainstormings do
"""
# See https://stackoverflow.com/questions/53802091/elixir-uuid-how-to-handle-500-error-when-uuid-doesnt-match
def get_brainstorming!(id) do
Repo.get!(Brainstorming, id)
id
|> get_bare_brainstorming!()
|> Repo.preload([
:users,
:moderating_users,
Expand All @@ -76,6 +77,10 @@ defmodule Mindwendel.Brainstormings do
|> update_last_accessed_at
end

def get_bare_brainstorming!(id) do
Repo.get!(Brainstorming, id)
end

@doc """
Gets a single brainstorming with the admin url id
"""
Expand Down
19 changes: 7 additions & 12 deletions lib/mindwendel/lanes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,9 @@ defmodule Mindwendel.Lanes do
"""
def get_lanes_for_brainstorming_with_labels_filtered(id) do
# optimization: brainstorming is only needed for filter_label_ideas
brainstorming = Brainstormings.get_brainstorming!(id)
brainstorming = Brainstormings.get_bare_brainstorming!(id)

# Enum/any?
filter_label =
if length(brainstorming.filter_labels_ids) > 0,
do: %{filter_labels_ids: brainstorming.filter_labels_ids},
else: %{}
filter_label = %{filter_labels_ids: brainstorming.filter_labels_ids}

get_lanes_for_brainstorming(id, filter_label)
end
Expand All @@ -91,7 +86,7 @@ defmodule Mindwendel.Lanes do
[%Lane{}, ...]
"""
def get_lanes_for_brainstorming(id, filters \\ %{}) do
def get_lanes_for_brainstorming(id, filters \\ %{filter_labels_ids: []}) do
lane_query =
from lane in Lane,
where: lane.brainstorming_id == ^id,
Expand All @@ -107,6 +102,10 @@ defmodule Mindwendel.Lanes do
|> Repo.preload(ideas: {ideas_advanced_query, [:link, :likes, :idea_labels, :files]})
end

defp build_ideas_query_with_filter(%{filter_labels_ids: []}) do
from(idea in Idea)
end

defp build_ideas_query_with_filter(%{filter_labels_ids: filter_labels_ids}) do
distinct_ideas =
from idea in Idea,
Expand All @@ -120,10 +119,6 @@ defmodule Mindwendel.Lanes do
)
end

defp build_ideas_query_with_filter(%{} = _filters) do
from(idea in Idea)
end

@doc """
Creates a lane.
Expand Down
7 changes: 5 additions & 2 deletions test/mindwendel/idea_labels_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ defmodule Mindwendel.IdeaLabelsTest do
@tag :skip
test "does not add the same IdeaLabel twice to Idea", %{idea_label: idea_label, idea: idea} do
# Calling this method twice does not fail and does not create duplicates
{:ok, idea_idea_label_after_method_call_1} = IdeaLabels.add_idea_label_to_idea(idea, idea_label)
{:ok, idea_idea_label_after_method_call_2} = IdeaLabels.add_idea_label_to_idea(idea, idea_label)
{:ok, idea_idea_label_after_method_call_1} =
IdeaLabels.add_idea_label_to_idea(idea, idea_label)

{:ok, idea_idea_label_after_method_call_2} =
IdeaLabels.add_idea_label_to_idea(idea, idea_label)

# There should still be only one IdeaIdeaLabel
assert Repo.count(IdeaIdeaLabel) == 1
Expand Down

0 comments on commit bc36a24

Please sign in to comment.