From 8b3e93f2734e9b1ccc86e352b91fef2c148f6c60 Mon Sep 17 00:00:00 2001 From: Benjamin Milde Date: Mon, 1 Jul 2019 13:37:26 +0200 Subject: [PATCH] Update to use conn.script_name instead of manually supplied namespace --- README.md | 6 +++--- lib/fun_with_flags/ui/router.ex | 16 ++-------------- lib/fun_with_flags/ui/templates.ex | 2 +- lib/fun_with_flags/ui/utils.ex | 2 +- test/fun_with_flags/ui/templates_test.exs | 2 +- test/fun_with_flags/ui/utils_test.exs | 4 ++-- 6 files changed, 10 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index bd14945..c50d97b 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ defmodule MyPhoenixApp.Web.Router do scope path: "/feature-flags" do pipe_through :mounted_apps - forward "/", FunWithFlags.UI.Router, namespace: "feature-flags" + forward "/", FunWithFlags.UI.Router end end ``` @@ -42,7 +42,7 @@ Since it's just a plug, it can also be mounted into any other Plug application u ```elixir defmodule Another.App do use Plug.Router - forward "/feature-flags", to: FunWithFlags.UI.Router, init_opts: [namespace: "feature-flags"] + forward "/feature-flags", to: FunWithFlags.UI.Router end ``` @@ -98,7 +98,7 @@ defmodule MyPhoenixApp.Web.Router do scope path: "/feature-flags" do pipe_through :mounted_and_protected_apps - forward "/", FunWithFlags.UI.Router, namespace: "feature-flags" + forward "/", FunWithFlags.UI.Router end end ``` diff --git a/lib/fun_with_flags/ui/router.ex b/lib/fun_with_flags/ui/router.ex index 07e5179..36e35c2 100644 --- a/lib/fun_with_flags/ui/router.ex +++ b/lib/fun_with_flags/ui/router.ex @@ -29,12 +29,6 @@ defmodule FunWithFlags.UI.Router do plug :match plug :dispatch - @doc false - def call(conn, opts) do - conn = extract_namespace(conn, opts) - super(conn, opts) - end - get "/" do conn @@ -273,7 +267,7 @@ defmodule FunWithFlags.UI.Router do defp redirect_to(conn, uri) do - path = Path.join(conn.assigns[:namespace], uri) + path = "/" <> Path.join(conn.script_name ++ [uri]) conn |> put_resp_header("location", path) @@ -282,12 +276,6 @@ defmodule FunWithFlags.UI.Router do end - defp extract_namespace(conn, opts) do - ns = opts[:namespace] || "" - Plug.Conn.assign(conn, :namespace, "/" <> ns) - end - - defp assign_csrf_token(conn, _opts) do csrf_token = Plug.CSRFProtection.get_csrf_token() Plug.Conn.assign(conn, :csrf_token, csrf_token) @@ -297,7 +285,7 @@ defmodule FunWithFlags.UI.Router do # Custom CSRF protection plug. It wraps the default plug provided # by `Plug`, it calls `Plug.Conn.fetch_session/1` (no-op if already # fetched), and it bails out gracefully if no session is configured. - # + # defp protect_from_forgery(conn, opts) do try do conn diff --git a/lib/fun_with_flags/ui/templates.ex b/lib/fun_with_flags/ui/templates.ex index 53c9737..1f0b46c 100644 --- a/lib/fun_with_flags/ui/templates.ex +++ b/lib/fun_with_flags/ui/templates.ex @@ -62,6 +62,6 @@ defmodule FunWithFlags.UI.Templates do def path(conn, path) do - Path.join(conn.assigns[:namespace], path) + "/" <> Path.join(conn.script_name ++ [path]) end end diff --git a/lib/fun_with_flags/ui/utils.ex b/lib/fun_with_flags/ui/utils.ex index cb5c11f..10c6957 100644 --- a/lib/fun_with_flags/ui/utils.ex +++ b/lib/fun_with_flags/ui/utils.ex @@ -128,7 +128,7 @@ defmodule FunWithFlags.UI.Utils do def validate_flag_name(conn, name) do if Regex.match?(~r/^\w+$/, name) do if safe_flag_exists?(name) do - path = Path.join(conn.assigns[:namespace], "/flags/" <> name) + path = "/" <> Path.join(conn.script_name ++ ["/flags/" <> name]) {:fail, "A flag named '#{name}' already exists."} else :ok diff --git a/test/fun_with_flags/ui/templates_test.exs b/test/fun_with_flags/ui/templates_test.exs index dba8ebe..fa9a443 100644 --- a/test/fun_with_flags/ui/templates_test.exs +++ b/test/fun_with_flags/ui/templates_test.exs @@ -12,7 +12,7 @@ defmodule FunWithFlags.UI.TemplatesTest do end setup do - conn = Plug.Conn.assign(%Plug.Conn{}, :namespace, "/pear") + conn = %Plug.Conn{script_name: ["pear"]} conn = Plug.Conn.assign(conn, :csrf_token, Plug.CSRFProtection.get_csrf_token()) {:ok, conn: conn} end diff --git a/test/fun_with_flags/ui/utils_test.exs b/test/fun_with_flags/ui/utils_test.exs index 742a966..b94247b 100644 --- a/test/fun_with_flags/ui/utils_test.exs +++ b/test/fun_with_flags/ui/utils_test.exs @@ -115,7 +115,7 @@ defmodule FunWithFlags.UI.UtilsTest do test "a blank string is blank" do assert Utils.blank?(" ") assert Utils.blank?(" ") - assert Utils.blank?(" + assert Utils.blank?(" ") end @@ -179,7 +179,7 @@ defmodule FunWithFlags.UI.UtilsTest do describe "validate_flag_name(conn, name)" do setup do - conn = Plug.Conn.assign(%Plug.Conn{}, :namespace, "/") + conn = %Plug.Conn{} {:ok, conn: conn} end test "returns :ok for a valid name", %{conn: conn} do