Skip to content

Commit

Permalink
Add by_ prefix to query operations for clarity (#5611)
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Oct 23, 2023
1 parent c8b0f16 commit df6b839
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions priv/templates/phx.gen.auth/context_functions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 146,7 @@
Ecto.Multi.new()
|> Ecto.Multi.update(:<%= schema.singular %>, changeset)
|> Ecto.Multi.delete_all(:tokens, <%= inspect schema.alias %>Token.<%= schema.singular %>_and_contexts_query(<%= schema.singular %>, [context]))
|> Ecto.Multi.delete_all(:tokens, <%= inspect schema.alias %>Token.by_<%= schema.singular %>_and_contexts_query(<%= schema.singular %>, [context]))
end
@doc ~S"""
Expand Down Expand Up @@ -199,7 199,7 @@

Ecto.Multi.new()
|> Ecto.Multi.update(:<%= schema.singular %>, changeset)
|> Ecto.Multi.delete_all(:tokens, <%= inspect schema.alias %>Token.<%= schema.singular %>_and_contexts_query(<%= schema.singular %>, :all))
|> Ecto.Multi.delete_all(:tokens, <%= inspect schema.alias %>Token.by_<%= schema.singular %>_and_contexts_query(<%= schema.singular %>, :all))
|> Repo.transaction()
|> case do
{:ok, %{<%= schema.singular %>: <%= schema.singular %>}} -> {:ok, <%= schema.singular %>}
Expand Down Expand Up @@ -230,7 230,7 @@
Deletes the signed token with the given context.
"""
def delete_<%= schema.singular %>_session_token(token) do
Repo.delete_all(<%= inspect schema.alias %>Token.token_and_context_query(token, "session"))
Repo.delete_all(<%= inspect schema.alias %>Token.by_token_and_context_query(token, "session"))
:ok
end

Expand Down Expand Up @@ -278,7 278,7 @@
defp confirm_<%= schema.singular %>_multi(<%= schema.singular %>) do
Ecto.Multi.new()
|> Ecto.Multi.update(:<%= schema.singular %>, <%= inspect schema.alias %>.confirm_changeset(<%= schema.singular %>))
|> Ecto.Multi.delete_all(:tokens, <%= inspect schema.alias %>Token.<%= schema.singular %>_and_contexts_query(<%= schema.singular %>, ["confirm"]))
|> Ecto.Multi.delete_all(:tokens, <%= inspect schema.alias %>Token.by_<%= schema.singular %>_and_contexts_query(<%= schema.singular %>, ["confirm"]))
end

## Reset password
Expand Down Expand Up @@ -335,7 335,7 @@
def reset_<%= schema.singular %>_password(<%= schema.singular %>, attrs) do
Ecto.Multi.new()
|> Ecto.Multi.update(:<%= schema.singular %>, <%= inspect schema.alias %>.password_changeset(<%= schema.singular %>, attrs))
|> Ecto.Multi.delete_all(:tokens, <%= inspect schema.alias %>Token.<%= schema.singular %>_and_contexts_query(<%= schema.singular %>, :all))
|> Ecto.Multi.delete_all(:tokens, <%= inspect schema.alias %>Token.by_<%= schema.singular %>_and_contexts_query(<%= schema.singular %>, :all))
|> Repo.transaction()
|> case do
{:ok, %{<%= schema.singular %>: <%= schema.singular %>}} -> {:ok, <%= schema.singular %>}
Expand Down
12 changes: 6 additions & 6 deletions priv/templates/phx.gen.auth/schema_token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 58,7 @@ defmodule <%= inspect schema.module %>Token do
"""
def verify_session_token_query(token) do
query =
from token in token_and_context_query(token, "session"),
from token in by_token_and_context_query(token, "session"),
join: <%= schema.singular %> in assoc(token, :<%= schema.singular %>),
where: token.inserted_at > ago(@session_validity_in_days, "day"),
select: <%= schema.singular %>
Expand Down Expand Up @@ -116,7 116,7 @@ defmodule <%= inspect schema.module %>Token do
days = days_for_context(context)

query =
from token in token_and_context_query(hashed_token, context),
from token in by_token_and_context_query(hashed_token, context),
join: <%= schema.singular %> in assoc(token, :<%= schema.singular %>),
where: token.inserted_at > ago(^days, "day") and token.sent_to == <%= schema.singular %>.email,
select: <%= schema.singular %>
Expand Down Expand Up @@ -151,7 151,7 @@ defmodule <%= inspect schema.module %>Token do
hashed_token = :crypto.hash(@hash_algorithm, decoded_token)

query =
from token in token_and_context_query(hashed_token, context),
from token in by_token_and_context_query(hashed_token, context),
where: token.inserted_at > ago(@change_email_validity_in_days, "day")

{:ok, query}
Expand All @@ -164,18 164,18 @@ defmodule <%= inspect schema.module %>Token do
@doc """
Returns the token struct for the given token value and context.
"""
def token_and_context_query(token, context) do
def by_token_and_context_query(token, context) do
from <%= inspect schema.alias %>Token, where: [token: ^token, context: ^context]
end

@doc """
Gets all tokens for the given <%= schema.singular %> for the given contexts.
"""
def <%= schema.singular %>_and_contexts_query(<%= schema.singular %>, :all) do
def by_<%= schema.singular %>_and_contexts_query(<%= schema.singular %>, :all) do
from t in <%= inspect schema.alias %>Token, where: t.<%= schema.singular %>_id == ^<%= schema.singular %>.id
end

def <%= schema.singular %>_and_contexts_query(<%= schema.singular %>, [_ | _] = contexts) do
def by_<%= schema.singular %>_and_contexts_query(<%= schema.singular %>, [_ | _] = contexts) do
from t in <%= inspect schema.alias %>Token, where: t.<%= schema.singular %>_id == ^<%= schema.singular %>.id and t.context in ^contexts
end
end

0 comments on commit df6b839

Please sign in to comment.