Skip to content

Commit

Permalink
refactored, renaming to position_order for conistency, throw out orde…
Browse files Browse the repository at this point in the history
…r_by column on brainstorming, use nil as default for order position instead of 0 to avoid bugs when dragging around a freshly created brainstorming
  • Loading branch information
JannikStreek committed Sep 9, 2024
1 parent 8b9d248 commit caba8bf
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 35 deletions.
1 change: 0 additions & 1 deletion lib/mindwendel/brainstormings/brainstorming.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ defmodule Mindwendel.Brainstormings.Brainstorming do

schema "brainstormings" do
field :name, :string
field :order_by, :string
field :option_show_link_to_settings, :boolean
# credo:disable-for-next-line
# Todo: The following line can be changed `field :admin_url_id, Ecto.UUID, autogenerate: true`
Expand Down
4 changes: 2 additions & 2 deletions lib/mindwendel/brainstormings/idea.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Mindwendel.Brainstormings.Idea do

schema "ideas" do
field :body, :string
field :order_position, :integer
field :position_order, :integer
field :username, :string, default: "Anonymous"
field :deprecated_label, Ecto.Enum, source: :label, values: @label_values
has_one :link, Link
Expand All @@ -37,7 +37,7 @@ defmodule Mindwendel.Brainstormings.Idea do
:deprecated_label,
:label_id,
:user_id,
:order_position
:position_order
])
|> validate_required([:username, :body, :brainstorming_id])
|> validate_length(:body, min: 1, max: 1023)
Expand Down
14 changes: 7 additions & 7 deletions lib/mindwendel/ideas.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule Mindwendel.Ideas do
on: idea_count.idea_id == idea.id,
where: idea.brainstorming_id == ^id,
order_by: [
asc: idea.order_position,
asc_nulls_last: idea.position_order,
desc: idea.updated_at
]

Expand Down Expand Up @@ -92,7 +92,7 @@ defmodule Mindwendel.Ideas do
join: idea_ranks in subquery(idea_rank_query),
on: idea_ranks.idea_id == idea.id,
where: idea.brainstorming_id == ^id,
update: [set: [order_position: idea_ranks.idea_rank]]
update: [set: [position_order: idea_ranks.idea_rank]]
)
|> Repo.update_all([])
end
Expand Down Expand Up @@ -125,7 +125,7 @@ defmodule Mindwendel.Ideas do
join: idea_ranks in subquery(idea_rank_query),
on: idea_ranks.idea_id == idea.id,
where: idea.brainstorming_id == ^id,
update: [set: [order_position: idea_ranks.idea_rank]]
update: [set: [position_order: idea_ranks.idea_rank]]
)
|> Repo.update_all([])
end
Expand All @@ -145,13 +145,13 @@ defmodule Mindwendel.Ideas do
new_position,
old_position
) do
get_idea!(idea_id) |> update_idea(%{order_position: new_position})
get_idea!(idea_id) |> update_idea(%{position_order: new_position})

# depending on moving a card bottom up or up to bottom, we need to correct the ordering
order =
if new_position < old_position,
do: [asc: :order_position, desc: :updated_at],
else: [asc: :order_position, asc: :updated_at]
do: [asc: :position_order, desc: :updated_at],
else: [asc: :position_order, asc: :updated_at]

idea_rank_query =
from(idea in Idea,
Expand All @@ -167,7 +167,7 @@ defmodule Mindwendel.Ideas do
join: idea_ranks in subquery(idea_rank_query),
on: idea_ranks.idea_id == idea.id,
where: idea.brainstorming_id == ^brainstorming_id,
update: [set: [order_position: idea_ranks.idea_rank]]
update: [set: [position_order: idea_ranks.idea_rank]]
)
|> Repo.update_all([])
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
data-testid={idea.id}
data-id={idea.id}
data-brainstorming-id={idea.brainstorming_id}
data-position={idea.order_position}
data-position={idea.position_order}
>
<div class="card-body-mindwendel-idea">
<%= if @current_user.id in [idea.user_id | @brainstorming.moderating_users |> Enum.map(& &1.id)] do %>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Mindwendel.Repo.Migrations.AddPositionOrderToIdeas do
use Ecto.Migration

def change do
alter table(:ideas) do
add :position_order, :integer, default: nil
end
end
end
22 changes: 11 additions & 11 deletions test/mindwendel/ideas_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@ defmodule Mindwendel.IdeasTest do
brainstorming: brainstorming,
idea: idea
} do
second_idea = Factory.insert!(:idea, brainstorming: brainstorming, order_position: 1)
third_idea = Factory.insert!(:idea, brainstorming: brainstorming, order_position: 2)
second_idea = Factory.insert!(:idea, brainstorming: brainstorming, position_order: 1)
third_idea = Factory.insert!(:idea, brainstorming: brainstorming, position_order: 2)

ideas_sorted_by_position = Ideas.list_ideas_for_brainstorming(brainstorming.id)

assert Enum.map(ideas_sorted_by_position, & &1.id) == [
# default is 0, therefore idea comes first
idea.id,
# default is null, therefore idea comes last
second_idea.id,
third_idea.id
third_idea.id,
idea.id
]
end
end

describe "update_ideas_for_brainstorming_by_likes" do
test "updates the order position for three ideas", %{brainstorming: brainstorming, idea: idea} do
Ideas.update_ideas_for_brainstorming_by_likes(brainstorming.id)
assert Repo.reload(idea).order_position == 1
assert Repo.reload(idea).position_order == 1
end

test "update ideas in the correct order", %{
Expand All @@ -65,7 +65,7 @@ defmodule Mindwendel.IdeasTest do
query =
from(idea in Idea,
where: idea.brainstorming_id == ^brainstorming.id,
order_by: [asc_nulls_last: idea.order_position]
order_by: [asc_nulls_last: idea.position_order]
)

ideas_sorted_by_position = Repo.all(query)
Expand All @@ -81,7 +81,7 @@ defmodule Mindwendel.IdeasTest do
describe "update_ideas_for_brainstorming_by_labels" do
test "updates the order position for three ideas", %{brainstorming: brainstorming, idea: idea} do
Ideas.update_ideas_for_brainstorming_by_labels(brainstorming.id)
assert Repo.reload(idea).order_position == 1
assert Repo.reload(idea).position_order == 1
end

test "update ideas in the correct order", %{
Expand Down Expand Up @@ -119,14 +119,14 @@ defmodule Mindwendel.IdeasTest do
second_idea =
Factory.insert!(:idea,
brainstorming: brainstorming,
order_position: 1,
position_order: 1,
updated_at: ~N[2021-01-01 15:06:30]
)

third_idea =
Factory.insert!(:idea,
brainstorming: brainstorming,
order_position: 2,
position_order: 2,
updated_at: ~N[2021-01-01 15:06:30]
)

Expand All @@ -135,7 +135,7 @@ defmodule Mindwendel.IdeasTest do
query =
from(idea in Idea,
where: idea.brainstorming_id == ^brainstorming.id,
order_by: [asc_nulls_last: idea.order_position]
order_by: [asc_nulls_last: idea.position_order]
)

ideas_sorted_by_position = Repo.all(query)
Expand Down

0 comments on commit caba8bf

Please sign in to comment.