Skip to content

Commit fcb4f95

Browse files
committed
Allow aliases and helpers to be disabled via scope
1 parent 73f888d commit fcb4f95

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

lib/phoenix/router.ex

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,10 @@ defmodule Phoenix.Router do
470470
471471
## Options
472472
473-
* `:as` - configures the named helper exclusively
473+
* `:as` - configures the named helper exclusively. If false, does not generate
474+
a helper.
475+
* `:alias` - configure if the scope alias should be applied to the route.
476+
Defaults to true, disables scoping if false.
474477
* `:log` - the level to log the route dispatching under,
475478
may be set to false. Defaults to `:debug`
476479
* `:host` - a string containing the host scope, or prefix host scope,
@@ -744,11 +747,11 @@ defmodule Phoenix.Router do
744747
745748
The supported options are:
746749
747-
* `:path` - a string containing the path scope
748-
* `:as` - a string or atom containing the named helper scope
749-
* `:alias` - an alias (atom) containing the controller scope.
750-
When set, this value may be overridden per route by passing `alias: false`
751-
to route definitions, such as `get`, `post`, etc.
750+
* `:path` - a string containing the path scope.
751+
* `:as` - a string or atom containing the named helper scope. When set to
752+
false, it resets the nested helper scopes.
753+
* `:alias` - an alias (atom) containing the controller scope. When set to
754+
false, it resets all nested aliases.
752755
* `:host` - a string containing the host scope, or prefix host scope,
753756
ie `"foo.bar.com"`, `"foo."`
754757
* `:private` - a map of private data to merge into the connection when a route matches

lib/phoenix/router/scope.ex

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,27 @@ defmodule Phoenix.Router.Scope do
9090
end
9191

9292
def push(module, opts) when is_list(opts) do
93+
top = get_top(module)
94+
9395
path =
9496
if path = Keyword.get(opts, :path) do
9597
path |> validate_path() |> String.split("/", trim: true)
9698
else
9799
[]
98100
end
99101

100-
alias = Keyword.get(opts, :alias) |> List.wrap() |> Enum.map(&Atom.to_string/1)
101-
as = Keyword.get(opts, :as) |> List.wrap()
102+
alias = append_unless_false(top, opts, :alias, &Atom.to_string(&1))
103+
as = append_unless_false(top, opts, :as, & &1)
102104
host = Keyword.get(opts, :host)
103105
private = Keyword.get(opts, :private, %{})
104106
assigns = Keyword.get(opts, :assigns, %{})
105107

106-
top = get_top(module)
107108
update_stack(module, fn stack -> [top | stack] end)
108109

109110
put_top(module, %Scope{
110111
path: top.path ++ path,
111-
alias: top.alias ++ alias,
112-
as: top.as ++ as,
112+
alias: alias,
113+
as: as,
113114
host: host || top.host,
114115
pipes: top.pipes,
115116
private: Map.merge(top.private, private),
@@ -119,6 +120,14 @@ defmodule Phoenix.Router.Scope do
119120
})
120121
end
121122

123+
defp append_unless_false(top, opts, key, fun) do
124+
case opts[key] do
125+
false -> []
126+
nil -> Map.fetch!(top, key)
127+
other -> Map.fetch!(top, key) ++ [fun.(other)]
128+
end
129+
end
130+
122131
@doc """
123132
Pops a scope from the module stack.
124133
"""

test/phoenix/router/helpers_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ defmodule Phoenix.Router.HelpersTest do
5353

5454
scope "/admin/new", alias: Admin, as: "admin" do
5555
resources "/messages", MessageController
56+
57+
scope "/unscoped", as: false do
58+
resources "/messages", MessageController, as: :my_admin_message
59+
end
5660
end
5761

5862
scope "/trails", trailing_slash: true do
@@ -385,6 +389,13 @@ defmodule Phoenix.Router.HelpersTest do
385389
assert Helpers.admin_message_path(__MODULE__, :show, 1) == "/admin/new/messages/1"
386390
end
387391

392+
test "scoped route helpers generated unscoped :as options" do
393+
assert Helpers.my_admin_message_path(__MODULE__, :index, []) == "/admin/new/unscoped/messages"
394+
assert Helpers.my_admin_message_path(__MODULE__, :index) == "/admin/new/unscoped/messages"
395+
assert Helpers.my_admin_message_path(__MODULE__, :show, 1, []) == "/admin/new/unscoped/messages/1"
396+
assert Helpers.my_admin_message_path(__MODULE__, :show, 1) == "/admin/new/unscoped/messages/1"
397+
end
398+
388399
test "scoped route helpers generated with trailing slashes" do
389400
assert Helpers.trail_path(__MODULE__, :index) == "/trails/"
390401
assert Helpers.trail_path(__MODULE__, :index, id: 5) == "/trails/?id=5"

test/phoenix/router/scope_test.exs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,15 @@ defmodule Phoenix.Router.ScopedRoutingTest do
5151

5252
scope "/v1", alias: V1 do
5353
resources "/users", UserController, only: [:delete], private: %{private_token: "baz"}
54+
5455
get "/noalias", Api.V1.UserController, :proxy,
5556
private: %{proxy_to: {scoped_alias(__MODULE__, UserController), :show}},
5657
alias: false
58+
59+
scope "/scoped", alias: false do
60+
get "/noalias", Api.V1.UserController, :proxy,
61+
private: %{proxy_to: {scoped_alias(__MODULE__, Api.V1.UserController), :show}}
62+
end
5763
end
5864
end
5965

@@ -205,12 +211,18 @@ defmodule Phoenix.Router.ScopedRoutingTest do
205211
end
206212
end
207213

208-
test "alias false with expanded scoped alias" do
214+
test "alias false with expanded scoped alias via option" do
209215
conn = call(Router, :get, "/api/v1/noalias")
210216
assert conn.status == 200
211217
assert conn.resp_body == "api v1 users show"
212218
end
213219

220+
test "alias false with expanded scoped alias via scope" do
221+
conn = call(Router, :get, "/api/v1/scoped/noalias")
222+
assert conn.status == 200
223+
assert conn.resp_body == "api v1 users show"
224+
end
225+
214226
test "raises for reserved prefixes" do
215227
assert_raise ArgumentError, ~r/`static` is a reserved route prefix/, fn ->
216228
defmodule ErrorRouter do

0 commit comments

Comments
 (0)