Skip to content

Commit a7b85e0

Browse files
committed
Refactor layout function to simplify error handling for layout configuration
1 parent 431b8a1 commit a7b85e0

File tree

1 file changed

+2
-28
lines changed

1 file changed

+2
-28
lines changed

lib/backpex/html/layout.ex

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,8 @@ defmodule Backpex.HTML.Layout do
1313

1414
def layout(assigns) do
1515
case assigns.live_resource.config(:layout) do
16-
{module, fun} ->
17-
apply(module, fun, [assigns])
18-
19-
fun when is_function(fun, 1) ->
20-
fun.(assigns)
21-
22-
wrong_config ->
23-
raise ArgumentError, """
24-
Invalid layout configuration for Backpex LiveResource.
25-
26-
Expected one of:
27-
• A tuple with module and function: {MyAppWeb.Layouts, :admin}
28-
• A function reference: &MyAppWeb.Layouts.admin/1
29-
30-
Got: #{inspect(wrong_config)}
31-
32-
Examples of valid configurations:
33-
34-
# Option 1: Module and function tuple
35-
use Backpex.LiveResource,
36-
layout: {MyAppWeb.Layouts, :admin}
37-
38-
# Option 2: Function capture
39-
use Backpex.LiveResource,
40-
layout: &MyAppWeb.Layouts.admin/1
41-
42-
Make sure your layout function accepts assigns and returns rendered HTML.
43-
"""
16+
{module, fun} -> apply(module, fun, [assigns])
17+
fun when is_function(fun, 1) -> fun.(assigns)
4418
end
4519
end
4620

0 commit comments

Comments
 (0)