Skip to content

Commit

Permalink
don't raise if --no-scope is omitted
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenDE committed Feb 26, 2025
1 parent 09a32f5 commit b28d5bc
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 162 deletions.
2 changes: 1 addition & 1 deletion guides/contexts.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ In order to run the context generators, we need to come up with a module name th

> #### A note on scopes {: .info}
>
> Many generators support the `--scope` option to generate scoped resources. For example, a scope might be the logged in user account of your application. Scoped resources are useful when different entities should see different resources. If a resource is scoped by user, the user will only be able to manage and see their own resources. For our catalog, we want everyone to see the same products, therefore we use `--no-scope`. We will see scopes in user later in this guide. You can also learn more about scopes in the [Scopes](scopes.html) guide.
> Many generators support the `--scope` option to generate scoped resources. For example, a scope might be the logged in user account of your application. Scoped resources are useful when different entities should see different resources. If a resource is scoped by user, the user will only be able to manage and see their own resources. For our catalog, we want everyone to see the same products, therefore we use `--no-scope`. Note that if no scopes are configured, `--no-scope` is the default. We will see scopes in user later in this guide. You can also learn more about scopes in the [Scopes](scopes.html) guide.
To jump-start our catalog context, we'll use `mix phx.gen.html` which creates a context module that wraps up Ecto access for creating, updating, and deleting products, along with web files like controllers and templates for the web interface into our context. Run the following command at your project root:

Expand Down
2 changes: 1 addition & 1 deletion guides/json_and_apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ For this guide let's create a simple JSON API to store our favourite links, that
For this guide, we will use Phoenix generators to scaffold our API infrastructure:

```console
mix phx.gen.json Urls Url urls link:string title:string --no-scope
mix phx.gen.json Urls Url urls link:string title:string
* creating lib/hello_web/controllers/url_controller.ex
* creating lib/hello_web/controllers/url_json.ex
* creating lib/hello_web/controllers/changeset_json.ex
Expand Down
12 changes: 6 additions & 6 deletions integration_test/test/code_generation/app_with_defaults_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithDefaultsTest do
{app_root_path, _} = generate_phoenix_app(tmp_dir, "phx_blog")

mix_run!(
~w(phx.gen.html Blog Post posts title:unique body:string status:enum:unpublished:published:deleted --no-scope),
~w(phx.gen.html Blog Post posts title:unique body:string status:enum:unpublished:published:deleted),
app_root_path
)

Expand All @@ -54,7 +54,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithDefaultsTest do
{app_root_path, _} = generate_phoenix_app(tmp_dir, "phx_blog")

mix_run!(
~w(phx.gen.html Blog Post posts title:unique body:string status:enum:unpublished:published:deleted order:integer:unique --no-scope),
~w(phx.gen.html Blog Post posts title:unique body:string status:enum:unpublished:published:deleted order:integer:unique),
app_root_path
)

Expand All @@ -81,7 +81,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithDefaultsTest do
{app_root_path, _} = generate_phoenix_app(tmp_dir, "phx_blog")

mix_run!(
~w(phx.gen.json Blog Post posts title:unique body:string status:enum:unpublished:published:deleted --no-scope),
~w(phx.gen.json Blog Post posts title:unique body:string status:enum:unpublished:published:deleted),
app_root_path
)

Expand All @@ -107,7 +107,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithDefaultsTest do
{app_root_path, _} = generate_phoenix_app(tmp_dir, "phx_blog")

mix_run!(
~w(phx.gen.json Blog Post posts title:unique body:string status:enum:unpublished:published:deleted --no-scope),
~w(phx.gen.json Blog Post posts title:unique body:string status:enum:unpublished:published:deleted),
app_root_path
)

Expand All @@ -134,7 +134,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithDefaultsTest do
{app_root_path, _} = generate_phoenix_app(tmp_dir, "phx_blog", ["--live"])

mix_run!(
~w(phx.gen.live Blog Post posts title:unique body:string p:boolean s:enum:a:b:c --no-scope),
~w(phx.gen.live Blog Post posts title:unique body:string p:boolean s:enum:a:b:c),
app_root_path
)

Expand Down Expand Up @@ -163,7 +163,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithDefaultsTest do
{app_root_path, _} = generate_phoenix_app(tmp_dir, "phx_blog", ["--live"])

mix_run!(
~w(phx.gen.live Blog Post posts title body:string public:boolean status:enum:unpublished:published:deleted --no-scope),
~w(phx.gen.live Blog Post posts title body:string public:boolean status:enum:unpublished:published:deleted),
app_root_path
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMSSQLAdapterTest do
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "default_mssql_app", ["--database", "mssql"])

mix_run!(~w(phx.gen.html Blog Post posts title body:string status:enum:unpublished:published:deleted --no-scope), app_root_path)
mix_run!(~w(phx.gen.html Blog Post posts title body:string status:enum:unpublished:published:deleted), app_root_path)

modify_file(Path.join(app_root_path, "lib/default_mssql_app_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -34,7 +34,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMSSQLAdapterTest do
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "default_mssql_app", ["--database", "mssql"])

mix_run!(~w(phx.gen.json Blog Post posts title body:string status:enum:unpublished:published:deleted --no-scope), app_root_path)
mix_run!(~w(phx.gen.json Blog Post posts title body:string status:enum:unpublished:published:deleted), app_root_path)

modify_file(Path.join(app_root_path, "lib/default_mssql_app_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -60,7 +60,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMSSQLAdapterTest do
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "default_mssql_app", ["--database", "mssql", "--live"])

mix_run!(~w(phx.gen.live Blog Post posts title body:string status:enum:unpublished:published:deleted --no-scope), app_root_path)
mix_run!(~w(phx.gen.live Blog Post posts title body:string status:enum:unpublished:published:deleted), app_root_path)

modify_file(Path.join(app_root_path, "lib/default_mssql_app_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -87,7 +87,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMSSQLAdapterTest do
with_installer_tmp("new with defaults", fn tmp_dir ->
{app_root_path, _} = generate_phoenix_app(tmp_dir, "phx_blog", ["--database", "mssql", "--live"])

mix_run!(~w(phx.gen.html Accounts Group groups name --no-scope), app_root_path)
mix_run!(~w(phx.gen.html Accounts Group groups name), app_root_path)

modify_file(Path.join(app_root_path, "lib/phx_blog_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -111,7 +111,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMSSQLAdapterTest do
with_installer_tmp("new with defaults", fn tmp_dir ->
{app_root_path, _} = generate_phoenix_app(tmp_dir, "phx_blog", ["--database", "mssql", "--live"])

mix_run!(~w(phx.gen.html Accounts Group groups name --no-scope), app_root_path)
mix_run!(~w(phx.gen.html Accounts Group groups name), app_root_path)

modify_file(Path.join(app_root_path, "lib/phx_blog_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -136,7 +136,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMSSQLAdapterTest do
with_installer_tmp("app_with_defaults (--live)", fn tmp_dir ->
{app_root_path, _} = generate_phoenix_app(tmp_dir, "phx_blog", ["--database", "mssql", "--live"])

mix_run!(~w(phx.gen.html Accounts Group groups name --no-scope), app_root_path)
mix_run!(~w(phx.gen.html Accounts Group groups name), app_root_path)

modify_file(Path.join(app_root_path, "lib/phx_blog_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -161,7 +161,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMSSQLAdapterTest do
with_installer_tmp("app_with_defaults", fn tmp_dir ->
{app_root_path, _} = generate_phoenix_app(tmp_dir, "phx_blog", ["--database", "mssql", "--live"])

mix_run!(~w(phx.gen.html Accounts Group groups name --no-scope), app_root_path)
mix_run!(~w(phx.gen.html Accounts Group groups name), app_root_path)

modify_file(Path.join(app_root_path, "lib/phx_blog_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMySqlAdapterTest do
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "default_mysql_app", ["--database", "mysql"])

mix_run!(~w(phx.gen.html Blog Post posts title body:string status:enum:unpublished:published:deleted --no-scope), app_root_path)
mix_run!(~w(phx.gen.html Blog Post posts title body:string status:enum:unpublished:published:deleted), app_root_path)

modify_file(Path.join(app_root_path, "lib/default_mysql_app_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -34,7 +34,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMySqlAdapterTest do
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "default_mysql_app", ["--database", "mysql"])

mix_run!(~w(phx.gen.json Blog Post posts title body:string status:enum:unpublished:published:deleted --no-scope), app_root_path)
mix_run!(~w(phx.gen.json Blog Post posts title body:string status:enum:unpublished:published:deleted), app_root_path)

modify_file(Path.join(app_root_path, "lib/default_mysql_app_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -60,7 +60,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMySqlAdapterTest do
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "default_mysql_app", ["--database", "mysql", "--live"])

mix_run!(~w(phx.gen.live Blog Post posts title body:string status:enum:unpublished:published:deleted --no-scope), app_root_path)
mix_run!(~w(phx.gen.live Blog Post posts title body:string status:enum:unpublished:published:deleted), app_root_path)

modify_file(Path.join(app_root_path, "lib/default_mysql_app_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithSQLite3AdapterTest do
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "default_sqlite3_app", ["--database", "sqlite3"])

mix_run!(~w(phx.gen.html Blog Post posts title body:string status:enum:unpublished:published:deleted --no-scope), app_root_path)
mix_run!(~w(phx.gen.html Blog Post posts title body:string status:enum:unpublished:published:deleted), app_root_path)

modify_file(Path.join(app_root_path, "lib/default_sqlite3_app_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -34,7 +34,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithSQLite3AdapterTest do
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "default_sqlite3_app", ["--database", "sqlite3"])

mix_run!(~w(phx.gen.json Blog Post posts title body:string status:enum:unpublished:published:deleted --no-scope), app_root_path)
mix_run!(~w(phx.gen.json Blog Post posts title body:string status:enum:unpublished:published:deleted), app_root_path)

modify_file(Path.join(app_root_path, "lib/default_sqlite3_app_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -60,7 +60,7 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithSQLite3AdapterTest do
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "default_sqlite3_app", ["--database", "sqlite3", "--live"])

mix_run!(~w(phx.gen.live Blog Post posts title body:string status:enum:unpublished:published:deleted --no-scope), app_root_path)
mix_run!(~w(phx.gen.live Blog Post posts title body:string status:enum:unpublished:published:deleted), app_root_path)

modify_file(Path.join(app_root_path, "lib/default_sqlite3_app_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule Phoenix.Integration.CodeGeneration.UmbrellaAppWithDefaultsTest do
{app_root_path, _} = generate_phoenix_app(tmp_dir, "rainy_day", ["--umbrella"])
web_root_path = Path.join(app_root_path, "apps/rainy_day_web")

mix_run!(~w(phx.gen.html Blog Post posts title:unique body:string status:enum:unpublished:published:deleted --no-scope), web_root_path)
mix_run!(~w(phx.gen.html Blog Post posts title:unique body:string status:enum:unpublished:published:deleted), web_root_path)

modify_file(Path.join(web_root_path, "lib/rainy_day_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -53,7 +53,7 @@ defmodule Phoenix.Integration.CodeGeneration.UmbrellaAppWithDefaultsTest do
{app_root_path, _} = generate_phoenix_app(tmp_dir, "rainy_day", ["--umbrella"])
web_root_path = Path.join(app_root_path, "apps/rainy_day_web")

mix_run!(~w(phx.gen.html Blog Post posts title body:string status:enum:unpublished:published:deleted --no-scope), web_root_path)
mix_run!(~w(phx.gen.html Blog Post posts title body:string status:enum:unpublished:published:deleted), web_root_path)

modify_file(Path.join(web_root_path, "lib/rainy_day_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -78,7 +78,7 @@ defmodule Phoenix.Integration.CodeGeneration.UmbrellaAppWithDefaultsTest do
{app_root_path, _} = generate_phoenix_app(tmp_dir, "rainy_day", ["--umbrella"])
web_root_path = Path.join(app_root_path, "apps/rainy_day_web")

mix_run!(~w(phx.gen.json Blog Post posts title:unique body:string status:enum:unpublished:published:deleted --no-scope), web_root_path)
mix_run!(~w(phx.gen.json Blog Post posts title:unique body:string status:enum:unpublished:published:deleted), web_root_path)

modify_file(Path.join(web_root_path, "lib/rainy_day_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -102,7 +102,7 @@ defmodule Phoenix.Integration.CodeGeneration.UmbrellaAppWithDefaultsTest do
{app_root_path, _} = generate_phoenix_app(tmp_dir, "rainy_day", ["--umbrella"])
web_root_path = Path.join(app_root_path, "apps/rainy_day_web")

mix_run!(~w(phx.gen.json Blog Post posts title body:string status:enum:unpublished:published:deleted --no-scope), web_root_path)
mix_run!(~w(phx.gen.json Blog Post posts title body:string status:enum:unpublished:published:deleted), web_root_path)

modify_file(Path.join(web_root_path, "lib/rainy_day_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -127,7 +127,7 @@ defmodule Phoenix.Integration.CodeGeneration.UmbrellaAppWithDefaultsTest do
{app_root_path, _} = generate_phoenix_app(tmp_dir, "rainy_day", ["--umbrella", "--live"])
web_root_path = Path.join(app_root_path, "apps/rainy_day_web")

mix_run!(~w(phx.gen.live Blog Post posts title:unique body:string status:enum:unpublished:published:deleted --no-scope), web_root_path)
mix_run!(~w(phx.gen.live Blog Post posts title:unique body:string status:enum:unpublished:published:deleted), web_root_path)

modify_file(Path.join(web_root_path, "lib/rainy_day_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -154,7 +154,7 @@ defmodule Phoenix.Integration.CodeGeneration.UmbrellaAppWithDefaultsTest do
{app_root_path, _} = generate_phoenix_app(tmp_dir, "rainy_day", ["--umbrella", "--live"])
web_root_path = Path.join(app_root_path, "apps/rainy_day_web")

mix_run!(~w(phx.gen.live Blog Post posts title body:string status:enum:unpublished:published:deleted --no-scope), web_root_path)
mix_run!(~w(phx.gen.live Blog Post posts title body:string status:enum:unpublished:published:deleted), web_root_path)

modify_file(Path.join(web_root_path, "lib/rainy_day_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand Down
19 changes: 2 additions & 17 deletions lib/mix/phoenix/scope.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,9 @@ defmodule Mix.Phoenix.Scope do
raise "--scope and --no-scope must not be used together"
end

def scope_from_opts(__otp_app, _name, true), do: nil
def scope_from_opts(_otp_app, _name, true), do: nil

def scope_from_opts(otp_app, nil, _) do
default_scope(otp_app) ||
raise """
no default scope configured!
Either run the generator with --no-scope to skip scoping, specify a scope with --scope,
or configure a default scope in your application's config:
config :#{otp_app}, :scopes, [
user: [
default: true,
...
]
]
"""
end
def scope_from_opts(otp_app, nil, _), do: default_scope(otp_app)

def scope_from_opts(otp_app, name, _) do
key = String.to_atom(name)
Expand Down
Loading

0 comments on commit b28d5bc

Please sign in to comment.