Skip to content

Commit 60dc18e

Browse files
author
GlennGeelen
committed
Add live view to cells
1 parent 9b87d2e commit 60dc18e

File tree

7 files changed

+159
-15
lines changed

7 files changed

+159
-15
lines changed

lib/ex_cell/base.ex

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ defmodule ExCell.Base do
44
defmacro __using__(opts \\ []) do
55
quote do
66
import ExCell.View
7+
import ExCell.LiveView
78

89
@adapter unquote(opts[:adapter])
910
@namespace unquote(opts[:namespace])
@@ -69,7 +70,7 @@ defmodule ExCell.Base do
6970
iex(1)> safe_to_string(AvatarCell.container)
7071
"<div class=\\"AvatarCell\\" data-cell=\\"AvatarCell\\" data-cell-params=\\"{}\\">"
7172
"""
72-
def container, do: container(%{}, [], [do: nil])
73+
def container, do: container(%{}, [], do: nil)
7374

7475
@doc """
7576
Returns the container of a cell as a Phoenix.Tag with it's content.
@@ -78,7 +79,7 @@ defmodule ExCell.Base do
7879
iex(1)> safe_to_string(AvatarCell.container(do: "Hello"))
7980
"<div class=\\"AvatarCell\\" data-cell=\\"AvatarCell\\" data-cell-params=\\"{}\\">Hello</div>"
8081
"""
81-
def container(do: content), do: container(%{}, [], [do: content])
82+
def container(do: content), do: container(%{}, [], do: content)
8283
def container(callback) when is_function(callback), do: container(%{}, [], callback)
8384

8485
@doc """
@@ -99,7 +100,7 @@ defmodule ExCell.Base do
99100
iex(1)> safe_to_string(AvatarCell.container(tag: :a, data: [foo: "bar"], class: "Moo", href: "/"))
100101
"<a class=\\"AvatarCell Moo\\" data-foo="bar" data-cell=\\"AvatarCell\\" data-cell-params=\\"{}\\">"
101102
"""
102-
def container(options) when is_list(options), do: container(%{}, options, [do: nil])
103+
def container(options) when is_list(options), do: container(%{}, options, do: nil)
103104
def container(options, content) when is_list(options), do: container(%{}, options, content)
104105

105106
@doc """
@@ -112,18 +113,19 @@ defmodule ExCell.Base do
112113
iex(1)> safe_to_string(AvatarCell.container(%{ foo: "bar" }))
113114
"<a class=\\"AvatarCell\\" data-cell=\\"AvatarCell\\" data-cell-params=\\"{&quot;foo&quot;:&quot;bar&quot;}">"
114115
"""
115-
def container(%{} = params), do:
116-
container(params, [], [do: nil])
117-
def container(%{} = params, [do: content]), do:
118-
container(params, [], [do: content])
119-
def container(%{} = params, callback) when is_function(callback), do:
120-
container(params, [], callback)
121-
def container(%{} = params, options) when is_list(options), do:
122-
container(params, options, [do: nil])
123-
def container(%{} = params, options, content), do:
124-
ExCell.container(__MODULE__, UUID.uuid4(), params, options, content)
125-
126-
defoverridable [class_name: 0, cell_name: 0, params: 0]
116+
def container(%{} = params), do: container(params, [], do: nil)
117+
def container(%{} = params, do: content), do: container(params, [], do: content)
118+
119+
def container(%{} = params, callback) when is_function(callback),
120+
do: container(params, [], callback)
121+
122+
def container(%{} = params, options) when is_list(options),
123+
do: container(params, options, do: nil)
124+
125+
def container(%{} = params, options, content),
126+
do: ExCell.container(__MODULE__, UUID.uuid4(), params, options, content)
127+
128+
defoverridable class_name: 0, cell_name: 0, params: 0
127129
end
128130
end
129131
end

lib/ex_cell/live_view.ex

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
defmodule ExCell.LiveView do
2+
@moduledoc """
3+
Cell helpers used to render the live view cells in both Views and Cells
4+
"""
5+
@view_adapter ExCell.config(:view_adapter, Phoenix.LiveView)
6+
7+
@doc """
8+
Renders a cell in the view.
9+
10+
### Examples
11+
iex(0)> safe_to_string(AppWeb.AvatarView.live_cell(AvatarLiveCell, socket))
12+
"<div class=\"AvatarLiveCell\" ...>"
13+
"""
14+
def live_cell(cell, conn_or_socket) do
15+
render_cell(cell, conn_or_socket, [])
16+
end
17+
18+
@doc """
19+
Renders a cell in the view with children.
20+
21+
### Examples
22+
iex(0)> safe_to_string(AppWeb.AvatarView.live_cell(AvatarLiveCell, do: "Foo"))
23+
"<div class=\"AvatarLiveCell\" ...>Foo</div>"
24+
"""
25+
def live_cell(cell, conn_or_socket, do: children) do
26+
render_cell(cell, conn_or_socket, children: children)
27+
end
28+
29+
@doc """
30+
Renders a cell in the view with assigns.
31+
32+
### Examples
33+
iex(0)> safe_to_string(AppWeb.AvatarView.live_cell(AvatarLiveCell, user: %User{name: "Bar"}))
34+
"<div class=\"AvatarLiveCell\" ...>Bar</div>"
35+
"""
36+
def live_cell(cell, conn_or_socket, assigns) when is_list(assigns) do
37+
render_cell(cell, conn_or_socket, assigns)
38+
end
39+
40+
@doc """
41+
Renders a cell in the view with children without a block.
42+
43+
### Examples
44+
iex(0)> safe_to_string(AppWeb.AvatarView.live_cell(AvatarLiveCell, "Hello"))
45+
"<div class=\"AvatarLiveCell\" ...>Hello</div>"
46+
"""
47+
def live_cell(cell, conn_or_socket, children) do
48+
render_cell(cell, conn_or_socket, children: children)
49+
end
50+
51+
def live_cell(cell, conn_or_socket, assigns, do: children) when is_list(assigns) do
52+
render_cell(cell, conn_or_socket, [children: children] ++ assigns)
53+
end
54+
55+
def live_cell(cell, conn_or_socket, children, assigns) when is_list(assigns) do
56+
render_cell(cell, conn_or_socket, [children: children] ++ assigns)
57+
end
58+
59+
defp render_cell(cell, conn_or_socket, assigns) do
60+
assigns = Map.new(assigns)
61+
@view_adapter.live_render(conn_or_socket, cell, session: %{assigns: assigns})
62+
end
63+
end

mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ defmodule ExCell.Mixfile do
5252
{:excoveralls, "~> 0.7", only: :test},
5353
{:mix_test_watch, "~> 0.3", only: :dev, runtime: false},
5454
{:phoenix_html, "~> 2.10"},
55+
{:phoenix_live_view, github: "phoenixframework/phoenix_live_view"},
5556
{:phoenix, "~> 1.4.0", optional: true},
5657
{:jason, "~> 1.1"},
5758
{:elixir_uuid, "~> 1.2"}

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
2222
"phoenix": {:hex, :phoenix, "1.4.3", "8eed4a64ff1e12372cd634724bddd69185938f52c18e1396ebac76375d85677d", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm"},
2323
"phoenix_html": {:hex, :phoenix_html, "2.13.2", "f5d27c9b10ce881a60177d2b5227314fc60881e6b66b41dfe3349db6ed06cf57", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
24+
"phoenix_live_view": {:git, "https://github.com/phoenixframework/phoenix_live_view.git", "da34e4886885f1f0f0dd7692b72663e4e94d9c98", []},
2425
"phoenix_pubsub": {:hex, :phoenix_pubsub, "1.1.2", "496c303bdf1b2e98a9d26e89af5bba3ab487ba3a3735f74bf1f4064d2a845a3e", [:mix], [], "hexpm"},
2526
"plug": {:hex, :plug, "1.8.0", "9d2685cb007fe5e28ed9ac27af2815bc262b7817a00929ac10f56f169f43b977", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm"},
2627
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm"},

test/ex_cell/live_view_test.exs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
defmodule ExCell.LiveViewTest do
2+
use ExCell.ConnCase
3+
alias ExCell.LiveView
4+
5+
test "live_cell/2 with ExCell", %{conn: conn} do
6+
assert LiveView.live_cell(:mock_cell, conn) === [conn, :mock_cell, [session: %{assigns: %{}}]]
7+
end
8+
9+
test "live_cell/3 with assigns", %{conn: conn} do
10+
assert LiveView.live_cell(:mock_cell, conn, foo: "bar") === [
11+
conn,
12+
:mock_cell,
13+
[session: %{assigns: %{foo: "bar"}}]
14+
]
15+
end
16+
17+
test "live_cell/3 with do block", %{conn: conn} do
18+
assert LiveView.live_cell(:mock_cell, conn, do: "yes") === [
19+
conn,
20+
:mock_cell,
21+
[session: %{assigns: %{children: "yes"}}]
22+
]
23+
end
24+
25+
test "live_cell/3 with children", %{conn: conn} do
26+
assert LiveView.live_cell(:mock_cell, conn, "yes") === [
27+
conn,
28+
:mock_cell,
29+
[session: %{assigns: %{children: "yes"}}]
30+
]
31+
end
32+
33+
test "live_cell/3 with assign and do block", %{conn: conn} do
34+
assert LiveView.live_cell(:mock_cell, conn, [foo: "bar"], do: "yes") === [
35+
conn,
36+
:mock_cell,
37+
[session: %{assigns: %{children: "yes", foo: "bar"}}]
38+
]
39+
end
40+
41+
test "live_cell/3 with children and assign", %{conn: conn} do
42+
assert LiveView.live_cell(:mock_cell, conn, "yes", foo: "bar") === [
43+
conn,
44+
:mock_cell,
45+
[session: %{assigns: %{children: "yes", foo: "bar"}}]
46+
]
47+
end
48+
end

test/support/conn_case.ex

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule ExCell.ConnCase do
2+
@moduledoc """
3+
This module defines the test case to be used by
4+
tests that require setting up a connection.
5+
6+
Such tests rely on `Phoenix.ConnTest` and also
7+
import other functionality to make it easier
8+
to build common data structures and query the data layer.
9+
10+
Finally, if the test case interacts with the database,
11+
it cannot be async. For this reason, every test runs
12+
inside a transaction which is reset at the beginning
13+
of the test unless the test case is marked as async.
14+
"""
15+
16+
use ExUnit.CaseTemplate
17+
18+
using do
19+
quote do
20+
# Import conveniences for testing with connections
21+
use Phoenix.ConnTest
22+
end
23+
end
24+
25+
setup tags do
26+
{:ok, conn: Phoenix.ConnTest.build_conn()}
27+
end
28+
end

test/support/mock_view_adapter.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ defmodule ExCell.MockViewAdapter do
22
@moduledoc false
33
def render(cell, template, args), do: [cell, template, args]
44
def render_to_string(cell, template, args), do: [cell, template, args]
5+
def live_render(conn, cell, args), do: [conn, cell, args]
56
end

0 commit comments

Comments
 (0)