Skip to content

Commit

Permalink
Refactor handling of identities module
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed May 25, 2020
1 parent 235d275 commit 14f41d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
16 changes: 7 additions & 9 deletions lib/pow_assent/ecto/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,13 @@ defmodule PowAssent.Ecto.Schema do

@doc false
def __identities_module__(module) do
module
|> Module.split()
|> Enum.reverse()
|> case do
[_schema, base] -> [base]
[_schema, _context | rest] -> rest
end
|> Enum.reverse()
|> Enum.concat([Users, UserIdentity])
{_schema, base} =
module
|> Module.split()
|> List.pop_at(-1)

base
|> Kernel.++([UserIdentity])
|> Module.concat()
end

Expand Down
11 changes: 8 additions & 3 deletions test/pow_assent/ecto/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ defmodule PowAssent.Ecto.SchemaTest do
user = %User{}

assert Map.has_key?(user, :identities)
assert %{on_delete: :delete_all} = User.__schema__(:association, :identities)
assert %{on_delete: on_delete, queryable: queryable} = User.__schema__(:association, :identities)
assert on_delete == :delete_all
assert queryable == PowAssent.Test.Ecto.Users.UserIdentity
end

@identity %{
Expand Down Expand Up @@ -175,13 +177,16 @@ defmodule PowAssent.Ecto.SchemaTest do
user = %OverrideAssocUser{}

assert Map.has_key?(user, :identities)
assert %{on_delete: :nothing} = OverrideAssocUser.__schema__(:association, :identities)
assert %{on_delete: on_delete, queryable: queryable} = OverrideAssocUser.__schema__(:association, :identities)
assert on_delete == :nothing
assert queryable == MyApp.Users.UserIdentity
end

test "schema/2 with no context user module name" do
user = %PowAssent.NoContextUser{}

assert Map.has_key?(user, :identities)
assert %{queryable: PowAssent.Users.UserIdentity} = PowAssent.NoContextUser.__schema__(:association, :identities)
assert %{queryable: queryable} = PowAssent.NoContextUser.__schema__(:association, :identities)
assert queryable == PowAssent.UserIdentity
end
end

0 comments on commit 14f41d7

Please sign in to comment.