Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for phoenix 1.7 verified routes #143

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions lib/exq_ui_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,43 @@ defmodule ExqUIWeb.Router do
* live_session_on_mount - Declares an optional module callback to be invoked on the LiveView's mount
"""
defmacro live_exq_ui(path, opts \\ []) do
quote bind_quoted: binding() do
scope path, alias: false, as: false do
{session_name, session_opts, route_opts} = ExqUIWeb.Router.__options__(opts)
import Phoenix.LiveView.Router, only: [live: 4, live_session: 3]
scope =
quote bind_quoted: binding() do
scope path, alias: false, as: false do
{session_name, session_opts, route_opts} = ExqUIWeb.Router.__options__(opts)
import Phoenix.LiveView.Router, only: [live: 4, live_session: 3]

live_session session_name, session_opts do
live "/", ExqUIWeb.DashboardLive, :root, route_opts
live "/queues", ExqUIWeb.QueueLive.Index, :index, route_opts
live "/queues/:name", ExqUIWeb.QueueLive.Show, :index, route_opts
live "/busy", ExqUIWeb.BusyLive.Index, :index, route_opts
live "/retries", ExqUIWeb.RetryLive.Index, :index, route_opts
live "/retries/:score/:jid", ExqUIWeb.RetryLive.Show, :index, route_opts
live "/dead", ExqUIWeb.DeadLive.Index, :index, route_opts
live "/dead/:score/:jid", ExqUIWeb.DeadLive.Show, :index, route_opts
live "/scheduled", ExqUIWeb.ScheduledLive.Index, :index, route_opts
live "/scheduled/:score/:jid", ExqUIWeb.ScheduledLive.Show, :index, route_opts
live_session session_name, session_opts do
live "/", ExqUIWeb.DashboardLive, :root, route_opts
live "/queues", ExqUIWeb.QueueLive.Index, :index, route_opts
live "/queues/:name", ExqUIWeb.QueueLive.Show, :index, route_opts
live "/busy", ExqUIWeb.BusyLive.Index, :index, route_opts
live "/retries", ExqUIWeb.RetryLive.Index, :index, route_opts
live "/retries/:score/:jid", ExqUIWeb.RetryLive.Show, :index, route_opts
live "/dead", ExqUIWeb.DeadLive.Index, :index, route_opts
live "/dead/:score/:jid", ExqUIWeb.DeadLive.Show, :index, route_opts
live "/scheduled", ExqUIWeb.ScheduledLive.Index, :index, route_opts
live "/scheduled/:score/:jid", ExqUIWeb.ScheduledLive.Show, :index, route_opts

if Application.compile_env(:exq_ui, :exq_scheduler_name) do
live "/recurring", ExqUIWeb.RecurringLive.Index, :index, route_opts
if Application.compile_env(:exq_ui, :exq_scheduler_name) do
live "/recurring", ExqUIWeb.RecurringLive.Index, :index, route_opts
end
end
end
end

# TODO: Remove check once we require Phoenix v1.7
if Code.ensure_loaded?(Phoenix.VerifiedRoutes) do
quote do
unquote(scope)

unless Module.get_attribute(__MODULE__, :exq_ui_prefix) do
@exq_ui_prefix Phoenix.Router.scoped_path(__MODULE__, path)
def __exq_ui_prefix__, do: @exq_ui_prefix
end
end
else
scope
end
end

Expand Down
17 changes: 16 additions & 1 deletion lib/exq_ui_web/router/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,23 @@ defmodule ExqUIWeb.Router.Helpers do
def dead_show_path(socket, score, jid, params \\ %{}),
do: build(socket, "/dead/#{score}/#{jid}", params)

# TODO: Remove this and the conditional on Phoenix v1.7+
@compile {:no_warn_undefined, Phoenix.VerifiedRoutes}
defp build(socket, path, params) do
root = socket.router.__helpers__.exq_ui_path(socket, :root)
router = socket.router

root =
if function_exported?(router, :__exq_ui_prefix__, 0) do
prefix = router.__exq_ui_prefix__()

Phoenix.VerifiedRoutes.unverified_path(
socket,
router,
prefix
)
else
router.__helpers__.exq_ui_path(socket, :root)
end

query =
if Enum.empty?(params) do
Expand Down