<%= if @id == "main" do %>
<% end %>
<%= for {field, _, _} <- @embeds, @record |> Map.fetch!(field) |> List.wrap() |> Enum.any? do %>
-
{trans(humanize(field))}
+
{trans(humanize(field))}
<% end %>
- <%= if @last > 0 do %>
- <%= for n <- 0..@last do %>
+ <%= if @sibling_count > 0 do %>
+ <%= for n <- 0..@sibling_count do %>
{n}
<% end %>
<% end %>
@@ -184,7 +184,7 @@ defmodule LiveAdmin.Components.Container.Show do
<% end %>
<.expand_modal
- id={"#{@id}-#{field}-#{@current}-expand"}
+ id={"#{indexed_id(@id, @current_index)}-#{field}-expand"}
title={@title}
field={field}
value={Map.fetch!(@record, field)}
@@ -197,12 +197,12 @@ defmodule LiveAdmin.Components.Container.Show do
<%= for {field, {_, {_, %{related: schema}}}, _} <- @embeds, embed = Map.fetch!(@record, field), list = List.wrap(embed) do %>
<%= for {record, index} <- Enum.with_index(list) do %>
<.detail_view
- id={"#{@id}_#{field}"}
+ id={"#{indexed_id(@id, @current_index)}_#{field}"}
fields={Enum.map(schema.__schema__(:fields), &{&1, schema.__schema__(:type, &1), []})}
record={record}
title={trans(humanize(field))}
- current={index}
- last={Enum.count(list) - 1}
+ current_index={index}
+ sibling_count={Enum.count(list) - 1}
resource={@resource}
resources={@resources}
base_path={@base_path}
@@ -219,6 +219,9 @@ defmodule LiveAdmin.Components.Container.Show do
defp renderable?({_, {Ecto.Embedded, _}}), do: false
defp renderable?(_), do: true
+ defp indexed_id("main", _), do: "main"
+ defp indexed_id(id, index), do: "#{id}_#{index}"
+
defp assoc_resource_key(resource, field, resources, config) do
schema = LiveAdmin.fetch_config(resource, :schema, config)
LiveAdmin.associated_resource(schema, field, resources, :key)
diff --git a/test/live_admin/components/container_test.exs b/test/live_admin/components/container_test.exs
index 0e1d0e27..a4d97920 100644
--- a/test/live_admin/components/container_test.exs
+++ b/test/live_admin/components/container_test.exs
@@ -266,6 +266,27 @@ defmodule LiveAdmin.Components.ContainerTest do
end
end
+ describe "view resource with plural embed containing nested plural embed" do
+ setup %{conn: conn} do
+ post =
+ Repo.insert!(%Post{
+ title: "test",
+ previous_versions: [
+ %Version{links: [%Version.Link{url: "https://a.example"}]},
+ %Version{links: [%Version.Link{url: "https://b.example"}]}
+ ]
+ })
+
+ {:ok, view, _} = live(conn, "/live_admin_test_post/#{post.post_id}")
+
+ %{view: view}
+ end
+
+ test "renders unique detail-view id for each parent's nested embed", %{view: view} do
+ assert has_element?(view, "#main_previous_versions_1_links_0")
+ end
+ end
+
describe "view child resource" do
setup %{conn: conn} do
user = Repo.insert!(%User{})
diff --git a/test/support/resources.ex b/test/support/resources.ex
index 739bfeb4..31932fbf 100644
--- a/test/support/resources.ex
+++ b/test/support/resources.ex
@@ -79,6 +79,17 @@ defmodule LiveAdminTest.Post.Version do
field(:body, :string)
field(:tags, {:array, :string})
+ embeds_many(:links, __MODULE__.Link)
+
timestamps(updated_at: false)
end
end
+
+defmodule LiveAdminTest.Post.Version.Link do
+ use Ecto.Schema
+
+ @primary_key false
+ embedded_schema do
+ field(:url, :string)
+ end
+end