-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
extension_routes/1
similar to extension_messages/1
Also added overridable routes for email confirmation
- Loading branch information
1 parent
6086b03
commit 2c0fe97
Showing
9 changed files
with
208 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule PowEmailConfirmation.Phoenix.Routes do | ||
@moduledoc """ | ||
Module that handles routes. | ||
""" | ||
|
||
alias PowEmailConfirmation.Phoenix.ConfirmationController | ||
|
||
@doc """ | ||
Path to redirect user to when user signs in, but e-mail hasn't been | ||
confirmed. | ||
By default this is the same as the `after_sign_in_path/1`. | ||
""" | ||
def after_halted_sign_in_path(conn), do: ConfirmationController.routes(conn).after_sign_in_path(conn) | ||
|
||
@doc """ | ||
Path to redirect user to when user signs up, but e-mail hasn't been | ||
confirmed. | ||
By default this is the same as the `after_registration_path/1`. | ||
""" | ||
def after_halted_registration_path(conn), do: ConfirmationController.routes(conn).after_registration_path(conn) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
defmodule Pow.Extension.Phoenix.Routes do | ||
@moduledoc """ | ||
Module that handles routes for extensions. | ||
## Usage | ||
defmodule MyAppWeb.Pow.Routes do | ||
use Pow.Phoenix.Routes | ||
use Pow.Extension.Phoenix.Routes, | ||
extensions: [PowExtensionOne, PowExtensionTwo] | ||
alias MyAppWeb.Router.Helpers, as: Routes | ||
def pow_extension_one_a_path(conn), do: Routes.some_path(conn, :index) | ||
end | ||
Remember to update configuration with `routes_backend: MyAppWeb.Pow.Routes`. | ||
""" | ||
alias Pow.Extension | ||
|
||
@doc false | ||
defmacro __using__(config) do | ||
quote do | ||
unquote(config) | ||
|> unquote(__MODULE__).__routes_modules__() | ||
|> Enum.map(&unquote(__MODULE__).__define_route_methods__/1) | ||
end | ||
end | ||
|
||
@doc false | ||
def __routes_modules__(config) do | ||
Extension.Config.discover_modules(config, ["Phoenix", "Routes"]) | ||
end | ||
|
||
@doc false | ||
defmacro __define_route_methods__(extension) do | ||
quote do | ||
extension = unquote(extension) | ||
methods = extension.__info__(:functions) | ||
|
||
for {fallback_method, 1} <- methods do | ||
method_name = unquote(__MODULE__).method_name(extension, fallback_method) | ||
unquote(__MODULE__).__define_route_method__(extension, method_name, fallback_method) | ||
end | ||
|
||
unquote(__MODULE__).__define_fallback_module__(extension, methods) | ||
end | ||
end | ||
|
||
@doc false | ||
defmacro __define_route_method__(extension, method_name, fallback_method) do | ||
quote bind_quoted: [extension: extension, method_name: method_name, fallback_method: fallback_method] do | ||
@spec unquote(method_name)(Conn.t()) :: binary() | ||
def unquote(method_name)(conn) do | ||
unquote(extension).unquote(fallback_method)(conn) | ||
end | ||
|
||
defoverridable [{method_name, 1}] | ||
end | ||
end | ||
|
||
@doc false | ||
defmacro __define_fallback_module__(extension, methods) do | ||
quote do | ||
name = Module.concat([__MODULE__, unquote(extension)]) | ||
quoted = for {method, 1} <- unquote(methods) do | ||
method_name = unquote(__MODULE__).method_name(unquote(extension), method) | ||
|
||
quote do | ||
@spec unquote(method)(Conn.t()) :: binary() | ||
def unquote(method)(conn) do | ||
unquote(__MODULE__).unquote(method_name)(conn) | ||
end | ||
end | ||
end | ||
|
||
Module.create(name, quoted, Macro.Env.location(__ENV__)) | ||
end | ||
end | ||
|
||
@doc """ | ||
Generates a namespaced method name for a route method. | ||
""" | ||
@spec method_name(atom(), atom()) :: atom() | ||
def method_name(extension, type) do | ||
namespace = namespace(extension) | ||
|
||
String.to_atom("#{namespace}_#{type}") | ||
end | ||
|
||
defp namespace(extension) do | ||
["Routes", "Phoenix" | base] = | ||
extension | ||
|> Module.split() | ||
|> Enum.reverse() | ||
|
||
base | ||
|> Enum.reverse() | ||
|> Enum.join() | ||
|> Macro.underscore() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule Pow.Extension.Phoenix.RoutesTest do | ||
defmodule Phoenix.Routes do | ||
def a_path(_conn), do: "/first" | ||
def b_path(_conn), do: "/second" | ||
end | ||
|
||
defmodule Routes do | ||
use Pow.Extension.Phoenix.Routes, | ||
extensions: [Pow.Extension.Phoenix.RoutesTest] | ||
|
||
def pow_extension_phoenix_routes_test_a_path(_conn), do: "/overridden" | ||
end | ||
|
||
use ExUnit.Case | ||
doctest Pow.Extension.Phoenix.Routes | ||
|
||
test "can override routes" do | ||
assert Routes.pow_extension_phoenix_routes_test_a_path(nil) == "/overridden" | ||
assert Routes.pow_extension_phoenix_routes_test_b_path(nil) == "/second" | ||
end | ||
|
||
test "has fallback module" do | ||
assert Routes.Pow.Extension.Phoenix.RoutesTest.Phoenix.Routes.a_path(nil) == "/overridden" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters