From caba8bf206abd361cb3dd181753afe884812ca3e Mon Sep 17 00:00:00 2001 From: Jannik Streek Date: Mon, 9 Sep 2024 08:43:47 +0200 Subject: [PATCH] refactored, renaming to position_order for conistency, throw out order_by column on brainstorming, use nil as default for order position instead of 0 to avoid bugs when dragging around a freshly created brainstorming --- .../brainstormings/brainstorming.ex | 1 - lib/mindwendel/brainstormings/idea.ex | 4 ++-- lib/mindwendel/ideas.ex | 14 ++++++------ .../live/idea_live/index_component.html.heex | 2 +- ...0905135921_add_order_position_to_ideas.exs | 13 ----------- ...0905135921_add_position_order_to_ideas.exs | 9 ++++++++ test/mindwendel/ideas_test.exs | 22 +++++++++---------- 7 files changed, 30 insertions(+), 35 deletions(-) delete mode 100644 priv/repo/migrations/20240905135921_add_order_position_to_ideas.exs create mode 100644 priv/repo/migrations/20240905135921_add_position_order_to_ideas.exs diff --git a/lib/mindwendel/brainstormings/brainstorming.ex b/lib/mindwendel/brainstormings/brainstorming.ex index 5e378dcd..51759301 100644 --- a/lib/mindwendel/brainstormings/brainstorming.ex +++ b/lib/mindwendel/brainstormings/brainstorming.ex @@ -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` diff --git a/lib/mindwendel/brainstormings/idea.ex b/lib/mindwendel/brainstormings/idea.ex index b244715f..e9eea2d1 100644 --- a/lib/mindwendel/brainstormings/idea.ex +++ b/lib/mindwendel/brainstormings/idea.ex @@ -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 @@ -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) diff --git a/lib/mindwendel/ideas.ex b/lib/mindwendel/ideas.ex index 41495bbe..26e1fa08 100644 --- a/lib/mindwendel/ideas.ex +++ b/lib/mindwendel/ideas.ex @@ -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 ] @@ -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 @@ -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 @@ -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, @@ -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 diff --git a/lib/mindwendel_web/live/idea_live/index_component.html.heex b/lib/mindwendel_web/live/idea_live/index_component.html.heex index 2337230b..b22047ff 100644 --- a/lib/mindwendel_web/live/idea_live/index_component.html.heex +++ b/lib/mindwendel_web/live/idea_live/index_component.html.heex @@ -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} >
<%= if @current_user.id in [idea.user_id | @brainstorming.moderating_users |> Enum.map(& &1.id)] do %> diff --git a/priv/repo/migrations/20240905135921_add_order_position_to_ideas.exs b/priv/repo/migrations/20240905135921_add_order_position_to_ideas.exs deleted file mode 100644 index ddc9e917..00000000 --- a/priv/repo/migrations/20240905135921_add_order_position_to_ideas.exs +++ /dev/null @@ -1,13 +0,0 @@ -defmodule Mindwendel.Repo.Migrations.AddOrderPositionToIdeas do - use Ecto.Migration - - def change do - alter table(:ideas) do - add :order_position, :integer, default: 0 - end - - alter table(:brainstormings) do - add :order_by, :string, default: "asc" - end - end -end diff --git a/priv/repo/migrations/20240905135921_add_position_order_to_ideas.exs b/priv/repo/migrations/20240905135921_add_position_order_to_ideas.exs new file mode 100644 index 00000000..c641b31f --- /dev/null +++ b/priv/repo/migrations/20240905135921_add_position_order_to_ideas.exs @@ -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 diff --git a/test/mindwendel/ideas_test.exs b/test/mindwendel/ideas_test.exs index 8aca0c4b..798f5925 100644 --- a/test/mindwendel/ideas_test.exs +++ b/test/mindwendel/ideas_test.exs @@ -23,16 +23,16 @@ 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 @@ -40,7 +40,7 @@ defmodule Mindwendel.IdeasTest do 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", %{ @@ -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) @@ -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", %{ @@ -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] ) @@ -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)