Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move up an item in a list field #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions lib/ecto_nested_changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,28 @@ defmodule EctoNestedChangeset do
nested_get(:get, changeset, path)
end

@doc """
Moves the value in the to-many relation field or an array field
at the specified index to the index - 1 position.

The second to last path segment must be an atom referencing
either a to-many relation field or an array field.
The last path segment must be an integer.

## Example

iex> %Owner{pets: [%Pet{}, %Pet{toys: [%Toy{name: "stick"}]}]}
...> |> Ecto.Changeset.change()
...> |> move_up(changeset, [:pets, 1])
%Owner{pets: [%Pet{toys: [%Toy{name: "stick"}]}, %Pet{}]}
"""
@spec move_up(Changeset.t(), [atom | non_neg_integer]) :: any()
def move_up(%Changeset{} = changeset, path) do
index = path |> Enum.reverse() |> List.first()
path = Enum.drop(path, -1)
nested_update(:move_up, changeset, path, index)
end

defp nested_update(operation, changeset, field, value) when is_atom(field),
do: nested_update(operation, changeset, [field], value)

Expand All @@ -253,6 +275,18 @@ defmodule EctoNestedChangeset do
|> Changeset.put_change(field, Map.fetch!(data, field) ++ [value])
end

defp nested_update(:move_up, %Changeset{} = changeset, [field], index)
when is_atom(field) do
list_field = get_change_or_field(changeset, field)
current_item = Enum.at(list_field, index)

{above_index, item_above} = find_previous_item(list_field, index - 1)

list_field
|> List.replace_at(index, item_above)
|> List.replace_at(above_index, current_item)
end

defp nested_update(:prepend, %Changeset{} = changeset, [field], value)
when is_atom(field) do
new_value =
Expand Down Expand Up @@ -409,4 +443,15 @@ defmodule EctoNestedChangeset do
:error -> Map.get(changeset.data, field)
end
end

defp find_previous_item(changeset_list_field, index)
when is_list(changeset_list_field) do
current_item = Enum.at(changeset_list_field, index)

if current_item |> change() |> Changeset.get_change(:delete) do
find_previous_item(changeset_list_field, index - 1)
else
{index, current_item}
end
end
end
41 changes: 41 additions & 0 deletions test/ecto_nested_changeset_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -913,4 +913,45 @@ defmodule EctoNestedChangesetTest do
|> get_at([:posts])
end
end

describe "move_up/2" do
test "moves an item up in a list " do
field =
%Category{
id: 1,
posts: [%Post{id: 1, title: "first"}, %Post{id: 2, title: "second"}]
}
|> change()
|> move_up([:posts, 1])

assert field == [
%Post{id: 2, title: "second"},
%Post{id: 1, title: "first"}
]
end

test "moves an item up with its children" do
field =
%Category{
id: 1,
posts: [
%Post{id: 1, title: "first", comments: [%Comment{id: 1}]},
%Post{id: 2, title: "second", comments: [%Comment{id: 2}]}
]
}
|> change()
|> move_up([:posts, 1])

assert field == [
%Post{id: 2, title: "second", comments: [%Comment{id: 2}]},
%Post{id: 1, title: "first", comments: [%Comment{id: 1}]}
]
end

test "does not move up when the item is the first in the list" do
end

test "does not move up when there is only one item in the list" do
end
end
end