Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Sep 13, 2018
1 parent 0a5a685 commit 194946e
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lib/mix/pow/phoenix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ defmodule Mix.Pow.Phoenix do
"""
@spec create_templates(atom(), binary(), binary(), [binary()]) :: :ok
def create_templates(module, name, web_prefix, actions) do
path = Path.join([web_prefix, "templates", Macro.underscore(module), name])
template_module = Module.concat([module, Phoenix, "#{Macro.camelize(name)}Template"])
path = Path.join([web_prefix, "templates", Macro.underscore(module), name])

actions
|> Enum.map(&String.to_atom/1)
Expand Down
8 changes: 4 additions & 4 deletions lib/mix/pow/phoenix/mailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ defmodule Mix.Pow.Phoenix.Mailer do
"""
@spec create_view_file(atom(), binary(), atom(), binary(), [binary()]) :: :ok
def create_view_file(module, name, web_mod, web_prefix, mails) do
subjects = subjects_methods(module, name, mails)
path = Path.join([web_prefix, "views", Macro.underscore(module), "#{name}_view.ex"])
content = """
subjects = subjects_methods(module, name, mails)
path = Path.join([web_prefix, "views", Macro.underscore(module), "#{name}_view.ex"])
content = """
defmodule #{inspect(web_mod)}.#{inspect(module)}.#{Macro.camelize(name)}View do
use #{inspect(web_mod)}, :mailer_view
Expand All @@ -27,8 +27,8 @@ defmodule Mix.Pow.Phoenix.Mailer do
"""
@spec create_templates(atom(), binary(), binary(), [binary()]) :: :ok
def create_templates(module, name, web_prefix, mails) do
path = Path.join([web_prefix, "templates", Macro.underscore(module), name])
template_module = template_module(module, name)
path = Path.join([web_prefix, "templates", Macro.underscore(module), name])

Enum.each(mails, fn mail ->
for type <- [:html, :text] do
Expand Down
6 changes: 2 additions & 4 deletions lib/pow/phoenix/mailer/mail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ defmodule Pow.Phoenix.Mailer.Mail do
"""
@spec new(Conn.t(), map(), {module(), atom()}, Keyword.t()) :: t()
def new(conn, user, {view_module, template}, assigns) do
web_module =
conn
|> Plug.fetch_config()
|> Config.get(:web_mailer_module)
config = Plug.fetch_config(conn)
web_module = Config.get(config, :web_mailer_module)

view_module = Pow.Phoenix.ViewHelpers.build_view_module(view_module, web_module)

Expand Down
12 changes: 9 additions & 3 deletions lib/pow/phoenix/views/view_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ defmodule Pow.Phoenix.ViewHelpers do
"""
@spec layout(Conn.t()) :: Conn.t()
def layout(conn) do
web_module = conn |> Plug.fetch_config() |> Config.get(:web_module)
config = Plug.fetch_config(conn)
web_module = Config.get(config, :web_module)
view = view(conn, web_module)
layout = layout(conn, web_module)

Expand Down Expand Up @@ -86,14 +87,19 @@ defmodule Pow.Phoenix.ViewHelpers do
build_view_module(module, split_module(web_module))
end
def build_view_module(module, base) do
[pow_module | _rest] = Module.split(module)
base = base ++ [pow_module]
base = pow_base(module, base)

module
|> split_module()
|> build_module(base)
end

defp pow_base(module, base) do
[pow_module | _rest] = Module.split(module)

base ++ [pow_module]
end

defp build_layout({view, template}, web_module) when is_atom(web_module) do
build_layout({view, template}, split_module(web_module))
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pow/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule Pow.Plug do
It'll raise an error if configuration hasn't been set as a private key.
"""
@spec fetch_config(Conn.t()) :: Config.t()
def fetch_config(%{private: private}) do
def fetch_config(%Conn{private: private}) do
private[@private_config_key] || no_config_error()
end

Expand Down
10 changes: 4 additions & 6 deletions test/extensions/persistent_session/plug/cookie_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,13 @@ defmodule PowPersistentSession.Plug.CookieTest do
end

test "call/2 assigns user from cookie with prepended `:otp_app`", %{config: config, ets: ets} do
user = %User{id: 1}
id = "test_app_test"
config = Pow.Config.merge(config, [otp_app: :test_app])
conn =
user = %User{id: 1}
conn =
:get
|> ConnHelpers.conn("/")
|> ConnHelpers.init_session()
|> Session.call(config)
|> store_persistent(ets, id, user, "test_app_persistent_session_cookie")
|> Session.call(config ++ [otp_app: :test_app])
|> store_persistent(ets, "test_app_test", user, "test_app_persistent_session_cookie")
|> Cookie.call(Cookie.init(config))

assert Plug.current_user(conn) == user
Expand Down
8 changes: 4 additions & 4 deletions test/pow/phoenix/mailer/mail_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ defmodule Pow.Phoenix.Mailer.MailTest do
use ExUnit.Case
doctest Pow.Phoenix.Mailer.Mail

alias Pow.Phoenix.Mailer.Mail
alias Pow.Phoenix.MailerView
alias Plug.Conn
alias Pow.Phoenix.{Mailer.Mail, MailerView}

test "new/4" do
conn = %{private: %{pow_config: []}}
conn = %Conn{private: %{pow_config: []}}
assert mail = Mail.new(conn, :user, {MailerView, :mail_test}, value: "test")

assert mail.user == :user
Expand All @@ -36,7 +36,7 @@ defmodule Pow.Phoenix.Mailer.MailTest do
end

test "new/4 with `:web_module`" do
conn = %{private: %{pow_config: [web_mailer_module: Pow.Test.Phoenix]}}
conn = %Conn{private: %{pow_config: [web_mailer_module: Pow.Test.Phoenix]}}
assert mail = Mail.new(conn, :user, {MailerView, :mail_test}, value: "test")

assert mail.user == :user
Expand Down
12 changes: 5 additions & 7 deletions test/pow/plug/session_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,17 @@ defmodule Pow.Plug.SessionTest do
end

test "call/2 with prepended `:otp_app` session key", %{conn: conn, ets: ets} do
ets.put(nil, "token", {"cached", :os.system_time(:millisecond)})

opts =
@default_opts
|> Keyword.delete(:session_key)
|> Keyword.put(:otp_app, :test)
session_key = "test_auth"

ets.put(nil, "token", {"cached", :os.system_time(:millisecond)})

opts = Session.init(opts)
|> Keyword.put(:otp_app, :test_app)
|> Session.init()
conn =
conn
|> Conn.fetch_session()
|> Conn.put_session(session_key, "token")
|> Conn.put_session("test_app_auth", "token")
|> Session.call(opts)

assert conn.assigns[:current_user] == "cached"
Expand Down

0 comments on commit 194946e

Please sign in to comment.