Skip to content

Commit

Permalink
Fix changes to multiple scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarkow committed Jul 17, 2017
1 parent d013d67 commit 4a4c8a7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/ecto_ranked.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ defmodule EctoRanked do
{min, max} = find_neighbors(cs, options, number)
rank_between(cs, options, min, max)
nil ->
if get_field(cs, options.rank_field) && (get_change(cs, options.rank_field) || !options.scope_field || !get_change(cs, options.scope_field)) do
if get_field(cs, options.rank_field) && (get_change(cs, options.rank_field) || !options.scope_field || !changes_to_scope_fields(cs, options.scope_field)) do
cs
else
update_index_from_position(cs, options, "last")
Expand All @@ -66,6 +66,16 @@ defmodule EctoRanked do
end
end

defp changes_to_scope_fields(cs, scope_field) when is_list(scope_field) do
Enum.any? scope_field, fn field ->
changes_to_scope_fields(cs, field)
end
end

defp changes_to_scope_fields(cs, scope_field) do
get_change(cs, scope_field)
end

defp rank_between(cs, options, min, max) do
rank = EctoRanked.Utils.ceiling((max - min) / 2) + min
rank_at(cs, options, rank)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule EctoRanked.Mixfile do

def project do
[app: :ecto_ranked,
version: "0.1.5",
version: "0.1.6",
elixir: "~> 1.4",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
Expand Down
31 changes: 31 additions & 0 deletions test/multiple_scope_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,35 @@ defmodule EctoRanked.MultipleScopeTest do
assert ranked_ids(1, 1) == [group1_model1.id, group1_model2.id, group1_model3.id]
assert ranked_ids(1, 2) == [group2_model2.id, group2_model1.id, group2_model3.id]
end

test "moving between scopes" do
group1_model1 = %MultiScopeModel{} |> MultiScopeModel.changeset(%{first_scope: 1, second_scope: 1}) |> Repo.insert!
group1_model2 = %MultiScopeModel{} |> MultiScopeModel.changeset(%{first_scope: 1, second_scope: 1}) |> Repo.insert!
group1_model3 = %MultiScopeModel{} |> MultiScopeModel.changeset(%{first_scope: 1, second_scope: 1}) |> Repo.insert!

group2_model1 = %MultiScopeModel{} |> MultiScopeModel.changeset(%{first_scope: 1, second_scope: 2}) |> Repo.insert!
group2_model2 = %MultiScopeModel{} |> MultiScopeModel.changeset(%{first_scope: 1, second_scope: 2}) |> Repo.insert!
group2_model3 = %MultiScopeModel{} |> MultiScopeModel.changeset(%{first_scope: 1, second_scope: 2}) |> Repo.insert!

group1_model2 |> MultiScopeModel.changeset(%{position: 1, second_scope: 2}) |> Repo.update

assert ranked_ids(1, 1) == [group1_model1.id, group1_model3.id]
assert ranked_ids(1, 2) == [group2_model1.id, group1_model2.id, group2_model2.id, group2_model3.id]
end

test "moving to a new scope" do
group1_model1 = %MultiScopeModel{} |> MultiScopeModel.changeset(%{first_scope: 1, second_scope: 1}) |> Repo.insert!
group1_model2 = %MultiScopeModel{} |> MultiScopeModel.changeset(%{first_scope: 1, second_scope: 1}) |> Repo.insert!
group1_model3 = %MultiScopeModel{} |> MultiScopeModel.changeset(%{first_scope: 1, second_scope: 1}) |> Repo.insert!

group2_model1 = %MultiScopeModel{} |> MultiScopeModel.changeset(%{first_scope: 1, second_scope: 2}) |> Repo.insert!
group2_model2 = %MultiScopeModel{} |> MultiScopeModel.changeset(%{first_scope: 1, second_scope: 2}) |> Repo.insert!
group2_model3 = %MultiScopeModel{} |> MultiScopeModel.changeset(%{first_scope: 1, second_scope: 2}) |> Repo.insert!

group1_model2 |> MultiScopeModel.changeset(%{first_scope: 2}) |> Repo.update

assert ranked_ids(1, 1) == [group1_model1.id, group1_model3.id]
assert ranked_ids(1, 2) == [group2_model1.id, group2_model2.id, group2_model3.id]
assert ranked_ids(2, 1) == [group1_model2.id]
end
end

0 comments on commit 4a4c8a7

Please sign in to comment.