Skip to content
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
2 changes: 1 addition & 1 deletion lib/backpex/html/item_action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule Backpex.HTML.ItemAction do
id={:item_action_modal}
live_resource={@live_resource}
action_type={:item}
{Map.drop(assigns, [:socket, :flash, :fields])}
{Map.drop(assigns, [:fields | Backpex.HTML.Resource.lv_reserved_assigns()])}
/>
</div>
</Layout.modal>
Expand Down
16 changes: 13 additions & 3 deletions lib/backpex/html/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ defmodule Backpex.HTML.Resource do

embed_templates("resource/*")

@doc """
Returns the list of assigns that `Phoenix.LiveView` reserves and that must be dropped
before spreading parent assigns into a child `Phoenix.LiveComponent`.

`Phoenix.Component.assign/3` rejects these keys with an `ArgumentError`. Centralizing
the list here ensures every spread site forwards the same safe subset of the parent's
assigns.
"""
def lv_reserved_assigns, do: [:flash, :uploads, :streams, :socket, :myself]

@doc """
Renders a resource table.
"""
Expand Down Expand Up @@ -112,7 +122,7 @@ defmodule Backpex.HTML.Resource do
id={"resource_#{@name}_#{@primary_key}"}
module={@field_options.module}
type={@type}
{Map.drop(assigns, [:socket, :flash, :myself, :uploads])}
{Map.drop(assigns, lv_reserved_assigns())}
/>
"""
end
Expand Down Expand Up @@ -148,7 +158,7 @@ defmodule Backpex.HTML.Resource do
id={@id}
module={@field_options.module}
type={@type}
{Map.drop(assigns, [:socket, :flash, :myself, :uploads])}
{Map.drop(assigns, lv_reserved_assigns())}
/>
"""
end
Expand Down Expand Up @@ -183,7 +193,7 @@ defmodule Backpex.HTML.Resource do
module={@field_options.module}
lv_uploads={assigns[:uploads]}
type={@type}
{Map.drop(assigns, [:socket, :flash, :myself, :uploads])}
{Map.drop(assigns, lv_reserved_assigns())}
/>
"""
end
Expand Down
2 changes: 1 addition & 1 deletion lib/backpex/html/resource/resource_form_main.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
module={Backpex.FormComponent}
id={:edit}
live_resource={@live_resource}
{Map.drop(assigns, [:socket, :flash])}
{Map.drop(assigns, lv_reserved_assigns())}
/>
</.main_container>
2 changes: 1 addition & 1 deletion lib/backpex/html/resource/resource_index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
id={:modal}
live_resource={@live_resource}
action_type={:resource}
{Map.drop(assigns, [:socket, :flash, :fields])}
{Map.drop(assigns, [:fields | lv_reserved_assigns()])}
/>
</.modal>

Expand Down
24 changes: 24 additions & 0 deletions test/backpex/html/resource_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Backpex.HTML.ResourceTest do
use ExUnit.Case, async: true

alias Backpex.HTML.Resource

describe "lv_reserved_assigns/0" do
test "includes every assign reserved by Phoenix.LiveView" do
reserved = Resource.lv_reserved_assigns()

for key <- [:flash, :uploads, :streams, :socket, :myself] do
assert key in reserved, "expected #{inspect(key)} in lv_reserved_assigns/0"
end
end

test "dropping the reserved set removes :streams from a parent-style assigns map" do
assigns = %{streams: %{example: :ref}, foo: 1, bar: 2}

result = Map.drop(assigns, Resource.lv_reserved_assigns())

refute Map.has_key?(result, :streams)
assert result == %{foo: 1, bar: 2}
end
end
end
Loading