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

Allow updating via custom context #733

Open
wants to merge 3 commits 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
2 changes: 1 addition & 1 deletion lib/extensions/email_confirmation/ecto/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule PowEmailConfirmation.Ecto.Context do
def confirm_email(%user_mod{} = user, params, config) do
user
|> user_mod.confirm_email_changeset(params)
|> Context.do_update(config)
|> Operations.do_update(config)
end

# TODO: Remove by 1.1.0
Expand Down
2 changes: 1 addition & 1 deletion lib/extensions/invitation/ecto/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule PowInvitation.Ecto.Context do
def update(%user_mod{} = user, params, config) do
user
|> user_mod.accept_invitation_changeset(params)
|> Context.do_update(config)
|> Operations.do_update(config)
end

@doc """
Expand Down
5 changes: 3 additions & 2 deletions lib/extensions/reset_password/ecto/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ defmodule PowResetPassword.Ecto.Context do
@spec get_by_email(binary(), Config.t()) :: Context.user() | nil
def get_by_email(email, config), do: Operations.get_by([email: email], config)

@spec update_password(Context.user(), map(), Config.t()) :: {:ok, Context.user()} | {:error, Context.changeset()}
@spec update_password(Context.user(), map(), Config.t()) ::
{:ok, Context.user()} | {:error, Context.changeset()}
def update_password(%user_mod{} = user, params, config) do
user
|> user_mod.reset_password_changeset(params)
|> Context.do_update(config)
|> Operations.do_update(config)
end

# TODO: Remove by 1.1.0
Expand Down
1 change: 1 addition & 0 deletions lib/pow/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Pow.Context do
@callback authenticate(map()) :: user() | nil
@callback create(map()) :: {:ok, user()} | {:error, changeset()}
@callback update(user(), map()) :: {:ok, user()} | {:error, changeset()}
@callback do_update(changeset()) :: {:ok, map()} | {:error, changeset()}
@callback delete(user()) :: {:ok, user()} | {:error, changeset()}
@callback get_by(Keyword.t() | map()) :: user() | nil
end
6 changes: 6 additions & 0 deletions lib/pow/ecto/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule Pow.Ecto.Context do
* `pow_authenticate/1`
* `pow_create/1`
* `pow_update/2`
* `pow_do_update/1`
* `pow_delete/1`
* `pow_get_by/1`

Expand All @@ -50,6 +51,7 @@ defmodule Pow.Ecto.Context do
def authenticate(params), do: pow_authenticate(params)
def create(params), do: pow_create(params)
def update(user, params), do: pow_update(user, params)
def do_update(changeset), do: pow_do_update(changeset)
def delete(user), do: pow_delete(user)
def get_by(clauses), do: pow_get_by(clauses)

Expand All @@ -65,6 +67,10 @@ defmodule Pow.Ecto.Context do
unquote(__MODULE__).update(user, params, @pow_config)
end

def pow_do_update(changeset) do
unquote(__MODULE__).do_update(changeset, @pow_config)
end

def pow_delete(user) do
unquote(__MODULE__).delete(user, @pow_config)
end
Expand Down
14 changes: 14 additions & 0 deletions lib/pow/operations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ defmodule Pow.Operations do
module -> module.update(user, params)
end
end

@doc """
Update the database given a changeset.

This calls `Pow.Ecto.Context.do_update/2` or `do_update/1` on a custom context
module.
"""
@spec do_update(Context.changeset(), Config.t()) :: {:ok, map()} | {:error, map()}
def do_update(changeset, config) do
case context_module(config) do
Context -> Context.do_update(changeset, config)
module -> module.do_update(changeset)
end
end

@doc """
Delete an existing user.
Expand Down