Skip to content

Commit

Permalink
Fix ArithmeticError when moving moving only item in its scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Tmw committed Mar 10, 2019
1 parent 7f292a2 commit 8791c88
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/ecto_ranked.ex
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ defmodule EctoRanked do
|> limit(1)
|> select([m], field(m, ^options.rank_field))
|> cs.repo.one(prefix: options.prefix)
|> case do
nil -> @min
min -> min
end
end

defp find_prev_two(cs, options) do
Expand Down
6 changes: 6 additions & 0 deletions test/prefixed_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ defmodule EctoRanked.PrefixedTest do
assert model.my_rank == updated.my_rank
end

test "moving an item when its the only one in its scope" do
model = %Model{} |> Model.changeset(%{}) |> insert
updated = model |> Model.changeset(%{my_position: 1}) |> update
assert updated.my_rank == 0
end

test "moving an item up to a specific position" do
model1 = %Model{} |> Model.changeset(%{}) |> insert
model2 = %Model{} |> Model.changeset(%{}) |> insert
Expand Down
6 changes: 6 additions & 0 deletions test/ranked_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ defmodule EctoRanked.RankedTest do
assert model.my_rank == updated.my_rank
end

test "moving an item when its the only one in its scope" do
model = %Model{} |> Model.changeset(%{}) |> Repo.insert!()
updated = model |> Model.changeset(%{my_position: 1}) |> Repo.update!()
assert updated.my_rank == 0
end

test "moving an item up to a specific position" do
model1 = %Model{} |> Model.changeset(%{}) |> Repo.insert!
model2 = %Model{} |> Model.changeset(%{}) |> Repo.insert!
Expand Down

0 comments on commit 8791c88

Please sign in to comment.