Skip to content

Commit

Permalink
Fix list rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
tfwright committed Mar 11, 2024
1 parent 8a1131f commit 6d6490e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
5 changes: 4 additions & 1 deletion dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ defmodule Demo.Populator do
posts: [
%Demo.Posts.Post{
title: Faker.Lorem.paragraph(1) |> String.slice(0..9),
categories: Ecto.Enum.values(Demo.Posts.Post, :categories) |> Enum.take(Enum.random(0..2)),
tags: Faker.Lorem.words(Enum.random(0..5)),
body: Faker.Lorem.paragraphs() |> Enum.join("\n\n"),
disabled_user: get_user_if(:rand.uniform(2) == 1)
disabled_user: get_user_if(:rand.uniform(2) == 1),
previous_versions: [%Demo.Posts.Post.Version{body: Faker.Lorem.paragraphs() |> Enum.join("\n\n")}]
}
]
}
Expand Down
5 changes: 4 additions & 1 deletion lib/live_admin/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ defmodule LiveAdmin.Resource do
end

defp render_field(val = %{}), do: Tag.content_tag(:pre, inspect(val, pretty: true))
defp render_field(val) when is_list(val), do: Enum.map_join(val, ", ", &render_field/1)

defp render_field(val) when is_list(val),
do: Enum.map(val, &Tag.content_tag(:pre, inspect(&1, pretty: true)))

defp render_field(val) when is_binary(val), do: val
defp render_field(val), do: inspect(val)
end
24 changes: 24 additions & 0 deletions test/live_admin/resource_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule LiveAdmin.ResourceTest do
use ExUnit.Case, async: true

alias LiveAdmin.Resource

setup do
Ecto.Adapters.SQL.Sandbox.checkout(LiveAdminTest.Repo)
end

describe "render/6 with a list of maps field handled by default implementation" do
test "returns a list with safe pre" do
assert Resource.render(
%{previous_versions: [%{}]},
:previous_versions,
LiveAdminTest.PostResource,
nil,
%{},
render_with: nil
)
|> List.first()
|> Phoenix.HTML.safe_to_string() =~ ~r/pre/
end
end
end

0 comments on commit 6d6490e

Please sign in to comment.