Skip to content

Commit

Permalink
try other names before asking user
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenDE committed Feb 27, 2025
1 parent 90b3e17 commit 16c5522
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
32 changes: 31 additions & 1 deletion lib/mix/tasks/phx.gen.auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ defmodule Mix.Tasks.Phx.Gen.Auth do
{_, default_scope} =
Enum.find(existing_scopes, {nil, nil}, fn {_, scope} -> scope.default end)

key = String.to_atom(requested_scope || context.schema.singular)
key = String.to_atom(requested_scope || find_scope_name(context, existing_scopes))

{create_new?, scope, config_string} =
if Map.has_key?(existing_scopes, key) do
Expand All @@ -313,6 +313,36 @@ defmodule Mix.Tasks.Phx.Gen.Auth do
}
end

defp find_scope_name(context, existing_scopes) do
cond do
# user
is_new_scope?(existing_scopes, context.schema.singular) ->
context.schema.singular

# accounts_user
is_new_scope?(existing_scopes, "#{context.basename}_#{context.schema.singular}") ->
"#{context.basename}_#{context.schema.singular}"

# my_app_accounts_user
is_new_scope?(existing_scopes, "#{context.context_app}_#{context.basename}_#{context.schema.singular}") ->
"#{context.context_app}_#{context.basename}_#{context.schema.singular}"

true ->
Mix.raise """
Could not generate a scope name for #{context.schema.singular}! These scopes already exist:
* #{Enum.map(existing_scopes, fn {name, _scope} -> name end) |> Enum.join("\n * ")}
You can customize the scope name by passing the --scope option.
"""
end
end

defp is_new_scope?(existing_scopes, bin_key) do
key = String.to_atom(bin_key)
not Map.has_key?(existing_scopes, key)
end

defp new_scope(context, key, default_scope) do
Mix.Phoenix.Scope.new!(key, %{
default: !default_scope,
Expand Down
77 changes: 76 additions & 1 deletion test/mix/tasks/phx.gen.auth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ defmodule Mix.Tasks.Phx.Gen.AuthTest do
end)
end

test "when scope already exists", config do
test "when default scope already exists", config do
in_tmp_phx_project(config.test, fn ->
with_scope_env(
:my_app,
Expand All @@ -1356,6 +1356,81 @@ defmodule Mix.Tasks.Phx.Gen.AuthTest do
ecto_adapter: Ecto.Adapters.Postgres
)

help_text = """
Your application configuration already contains a default scope: :user.
phx.gen.auth will create a new accounts_user scope.
Do you want to proceed with the generation?\
"""

assert_received {:mix_shell, :yes?, [question]}
assert question == help_text
end
)
end)
end

test "when scope name cannot be generated", config do
in_tmp_phx_project(config.test, fn ->
with_scope_env(
:my_app,
[
user: [
default: true,
module: MyApp.Accounts.Scope,
assign_key: :current_scope,
access_path: [:user, :id],
schema_key: :user_id,
schema_type: :id,
schema_table: :users
],
accounts_user: [
default: false,
module: MyApp.Accounts.Scope
],
my_app_accounts_user: [
default: false,
module: MyApp.Accounts.Scope
]
],
fn ->
send(self(), {:mix_shell_input, :yes?, true})

assert_raise Mix.Error, ~r/Could not generate a scope name for user!/, fn ->
Gen.Auth.run(
~w(Accounts User users --no-compile --live),
ecto_adapter: Ecto.Adapters.Postgres
)
end
end
)
end)
end

test "when given scope already exists", config do
in_tmp_phx_project(config.test, fn ->
with_scope_env(
:my_app,
[
user: [
default: true,
module: MyApp.Accounts.Scope,
assign_key: :current_scope,
access_path: [:user, :id],
schema_key: :user_id,
schema_type: :id,
schema_table: :users
]
],
fn ->
send(self(), {:mix_shell_input, :yes?, true})

Gen.Auth.run(
~w(Accounts User users --no-compile --live --scope user),
ecto_adapter: Ecto.Adapters.Postgres
)

help_text = """
The scope user is already configured.
Expand Down

0 comments on commit 16c5522

Please sign in to comment.