Skip to content

Commit

Permalink
Raise on unknown :only and :except in resources. Closes phoenixframew…
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismccord committed Jan 27, 2018
1 parent 9cccad3 commit 54a5b69
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
1 change: 0 additions & 1 deletion lib/phoenix/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ defmodule Phoenix.Router do

if resource.singleton do
Enum.each resource.actions, fn
:index -> raise ArgumentError, "singleton resources cannot have an :index route"
:show -> get path, ctrl, :show, opts
:new -> get path <> "/new", ctrl, :new, opts
:edit -> get path <> "/edit", ctrl, :edit, opts
Expand Down
35 changes: 29 additions & 6 deletions lib/phoenix/router/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,36 @@ defmodule Phoenix.Router.Resource do
end

defp extract_actions(opts, singleton) do
if only = Keyword.get(opts, :only) do
@actions -- (@actions -- only)
else
default_actions(singleton) -- Keyword.get(opts, :except, [])
only = Keyword.get(opts, :only)
except = Keyword.get(opts, :except)

cond do
only ->
supported_actions = validate_actions(:only, singleton, only)
supported_actions -- (supported_actions -- only)

except ->
supported_actions = validate_actions(:except, singleton, except)
supported_actions -- except

true -> default_actions(singleton)
end
end

defp default_actions(true), do: @actions -- [:index]
defp default_actions(false), do: @actions
defp validate_actions(type, singleton, actions) do
supported_actions = default_actions(singleton)

unless actions -- supported_actions == [], do: raise ArgumentError, """
invalid :#{type} action(s) passed to resources.
supported#{if singleton, do: " singleton", else: ""} actions: #{inspect(default_actions(singleton))}
got: #{inspect(actions)}
"""

supported_actions
end

defp default_actions(true = _singleton), do: @actions -- [:index]
defp default_actions(false = _singleton), do: @actions
end
20 changes: 19 additions & 1 deletion test/phoenix/router/resources_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,29 @@ defmodule Phoenix.Router.ResourcesTest do
end

test "singleton resources declaring an :index route throws an ArgumentError" do
assert_raise ArgumentError, ~r"cannot have an :index route", fn ->
assert_raise ArgumentError, ~r/supported singleton actions: \[:edit, :new, :show, :create, :update, :delete\]/, fn ->
defmodule SingletonRouter.Router do
use Phoenix.Router
resources "/", UserController, singleton: true, only: [:index]
end
end
end

test "resources validates :only actions" do
assert_raise ArgumentError, ~r/supported actions: \[:index, :edit, :new, :show, :create, :update, :delete\]/, fn ->
defmodule SingletonRouter.Router do
use Phoenix.Router
resources "/", UserController, only: [:bad_index]
end
end
end

test "resources validates :except actions" do
assert_raise ArgumentError, ~r/supported actions: \[:index, :edit, :new, :show, :create, :update, :delete\]/, fn ->
defmodule SingletonRouter.Router do
use Phoenix.Router
resources "/", UserController, except: [:bad_index]
end
end
end
end

0 comments on commit 54a5b69

Please sign in to comment.