Skip to content

Commit

Permalink
Generate proper alias if --repo option is used, closes #5717
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Feb 23, 2024
1 parent 9426858 commit 0b3dec5
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 136 deletions.
4 changes: 4 additions & 0 deletions lib/mix/phoenix/schema.ex
Expand Up @@ -5,6 +5,7 @@ defmodule Mix.Phoenix.Schema do

defstruct module: nil,
repo: nil,
repo_alias: nil,
table: nil,
collection: nil,
embedded?: false,
Expand Down Expand Up @@ -78,6 +79,7 @@ defmodule Mix.Phoenix.Schema do
basename = Phoenix.Naming.underscore(schema_name)
module = Module.concat([base, schema_name])
repo = opts[:repo] || Module.concat([base, "Repo"])
repo_alias = if String.ends_with?(Atom.to_string(repo), ".Repo"), do: "", else: ", as: Repo"
file = Mix.Phoenix.context_lib_path(ctx_app, basename <> ".ex")
table = opts[:table] || schema_plural
{cli_attrs, uniques, redacts} = extract_attr_flags(cli_attrs)
Expand All @@ -88,6 +90,7 @@ defmodule Mix.Phoenix.Schema do
api_prefix = Application.get_env(otp_app, :generators)[:api_prefix] || "/api"
embedded? = Keyword.get(opts, :embedded, false)
generate? = Keyword.get(opts, :schema, true)

singular =
module
|> Module.split()
Expand All @@ -113,6 +116,7 @@ defmodule Mix.Phoenix.Schema do
migration?: Keyword.get(opts, :migration, true),
module: module,
repo: repo,
repo_alias: repo_alias,
table: table,
embedded?: embedded?,
alias: module |> Module.split() |> List.last() |> Module.concat(nil),
Expand Down
Expand Up @@ -2,7 +2,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
use <%= inspect context.web_module %>.ConnCase<%= test_case_options %>

alias <%= inspect context.module %>
alias <%= inspect schema.repo %>
alias <%= inspect schema.repo %><%= schema.repo_alias %>
import <%= inspect context.module %>Fixtures

setup do
Expand Down
Expand Up @@ -5,7 +5,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
import <%= inspect context.module %>Fixtures

alias <%= inspect context.module %>
alias <%= inspect schema.repo %>
alias <%= inspect schema.repo %><%= schema.repo_alias %>

setup do
%{<%= schema.singular %>: <%= schema.singular %>_fixture()}
Expand Down
2 changes: 1 addition & 1 deletion priv/templates/phx.gen.auth/confirmation_live_test.exs
Expand Up @@ -5,7 +5,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
import <%= inspect context.module %>Fixtures

alias <%= inspect context.module %>
alias <%= inspect schema.repo %>
alias <%= inspect schema.repo %><%= schema.repo_alias %>

setup do
%{<%= schema.singular %>: <%= schema.singular %>_fixture()}
Expand Down
2 changes: 1 addition & 1 deletion priv/templates/phx.gen.auth/forgot_password_live_test.exs
Expand Up @@ -5,7 +5,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
import <%= inspect context.module %>Fixtures

alias <%= inspect context.module %>
alias <%= inspect schema.repo %>
alias <%= inspect schema.repo %><%= schema.repo_alias %>

describe "Forgot password page" do
test "renders email page", %{conn: conn} do
Expand Down
Expand Up @@ -2,7 +2,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
use <%= inspect context.web_module %>.ConnCase<%= test_case_options %>

alias <%= inspect context.module %>
alias <%= inspect schema.repo %>
alias <%= inspect schema.repo %><%= schema.repo_alias %>
import <%= inspect context.module %>Fixtures

setup do
Expand Down
2 changes: 1 addition & 1 deletion priv/templates/phx.gen.context/context.ex
Expand Up @@ -4,5 +4,5 @@ defmodule <%= inspect context.module %> do
"""

import Ecto.Query, warn: false
alias <%= inspect schema.repo %>
alias <%= inspect schema.repo %><%= schema.repo_alias %>
end

0 comments on commit 0b3dec5

Please sign in to comment.