Skip to content

Commit

Permalink
add :name opt in beacon_live_admin
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp committed Nov 27, 2024
1 parent 42826d9 commit 45b7ff1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/beacon/live_admin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Beacon.LiveAdmin do
alias Beacon.LiveAdmin.Config

@doc """
Starts Beacon LiveAdmin's main supervisor and a supervisor for each instance.
Starts Beacon LiveAdmin's instances.
"""
def start_link(opts) when is_list(opts) do
Supervisor.start_link(__MODULE__, opts, name: __MODULE__)
Expand Down
46 changes: 39 additions & 7 deletions lib/beacon/live_admin/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule Beacon.LiveAdmin.Router do
Routing for Beacon LiveAdmin.
"""

require Logger

@type conn_or_socket :: Phoenix.LiveView.Socket.t() | Plug.Conn.t()

defmacro __using__(_opts) do
Expand Down Expand Up @@ -61,8 +63,10 @@ defmodule Beacon.LiveAdmin.Router do
## Options
* `:on_mount` (optional) , an optional list of `on_mount` hooks passed to `live_session`.
This will allow for authenticated routes, among other uses.
* `:name` (required) `atom()` - register your instance with a unique name.
Note that the name has to match the one used in your instance configuration.
* `:on_mount` (optional) - an optional list of `on_mount` hooks passed to `live_session`.
This will allow for authenticated routes, among other uses.
"""
defmacro beacon_live_admin(prefix, opts \\ []) do
Expand All @@ -88,8 +92,7 @@ defmodule Beacon.LiveAdmin.Router do
{additional_pages, opts} = Keyword.pop(opts, :additional_pages, [])
pages = Beacon.LiveAdmin.Router.__pages__(additional_pages)

{session_name, session_opts} =
Beacon.LiveAdmin.Router.__session_options__(prefix, pages, opts)
{_instance_name, session_name, session_opts} = Beacon.LiveAdmin.Router.__options__(pages, opts)

import Phoenix.Router, only: [get: 4]
import Phoenix.LiveView.Router, only: [live: 4, live_session: 3]
Expand Down Expand Up @@ -196,8 +199,36 @@ defmodule Beacon.LiveAdmin.Router do
end

@doc false
def __session_options__(prefix, pages, opts) do
# TODO validate options
# TODO validate options
def __options__(pages, opts) do
instance_name =
Keyword.get_lazy(opts, :name, fn ->
Logger.warning("""
missing required option :name in beacon_live_admin/2
It will default to :admin but it's recommended to provide a unique name for your instance.
Example:
beacon_live_admin "/admin", name: :admin
""")

:admin
end)

instance_name =
cond do
String.starts_with?(Atom.to_string(instance_name), ["beacon", "__beacon"]) ->
raise ArgumentError, ":name can not start with beacon or __beacon, got: #{instance_name}"

instance_name && is_atom(instance_name) ->
instance_name

:invalid ->
raise ArgumentError, ":name must be an atom, got: #{inspect(instance_name)}"
end

if Keyword.has_key?(opts, :root_layout) do
raise ArgumentError, """
you cannot assign a different root_layout.
Expand All @@ -221,7 +252,8 @@ defmodule Beacon.LiveAdmin.Router do
]

{
opts[:live_session_name] || String.to_atom("beacon_live_admin_#{prefix}"),
instance_name,
opts[:live_session_name] || String.to_atom("beacon_live_admin_#{instance_name}"),
[
root_layout: {Beacon.LiveAdmin.Layouts, :admin},
session: {__MODULE__, :__session__, session_args},
Expand Down

0 comments on commit 45b7ff1

Please sign in to comment.