From 4a502c2ced57c990005c34f0cb3fc014771fb3b8 Mon Sep 17 00:00:00 2001 From: Rebecca Le <543859+sevenseacat@users.noreply.github.com> Date: Thu, 25 Aug 2022 22:54:53 +0800 Subject: [PATCH] fix: Binary data display (#25) --- lib/ash_admin/components/resource/form.ex | 6 ++++++ lib/ash_admin/components/resource/show.ex | 12 ++++++++++++ lib/ash_admin/components/resource/table.ex | 12 ++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/ash_admin/components/resource/form.ex b/lib/ash_admin/components/resource/form.ex index ea7097a..069de52 100644 --- a/lib/ash_admin/components/resource/form.ex +++ b/lib/ash_admin/components/resource/form.ex @@ -553,6 +553,12 @@ defmodule AshAdmin.Components.Resource.Form do """ end + def render_attribute_input(assigns, %{type: Ash.Type.Binary}, _form, _value, _name) do + ~F""" + (binary fields cannot be edited) + """ + end + def render_attribute_input( assigns, %{ diff --git a/lib/ash_admin/components/resource/show.ex b/lib/ash_admin/components/resource/show.ex index 1e9eba8..dab5bb8 100644 --- a/lib/ash_admin/components/resource/show.ex +++ b/lib/ash_admin/components/resource/show.ex @@ -315,6 +315,18 @@ defmodule AshAdmin.Components.Resource.Show do end end + defp render_attribute(assigns, _resource, record, %{name: name, type: Ash.Type.Binary}, _) do + if Map.get(record, name) do + ~F""" + (binary data) + """ + else + ~F""" + (empty) + """ + end + end + defp render_attribute(assigns, resource, record, attribute, nested?) do if Ash.Type.embedded_type?(attribute.type) do both_classes = "ml-1 pl-2 pr-2" diff --git a/lib/ash_admin/components/resource/table.ex b/lib/ash_admin/components/resource/table.ex index 941c774..7530994 100644 --- a/lib/ash_admin/components/resource/table.ex +++ b/lib/ash_admin/components/resource/table.ex @@ -134,14 +134,22 @@ defmodule AshAdmin.Components.Resource.Table do |> Map.get(attribute.name) |> (&apply(mod, func, [&1] ++ args)).() - format_attribute_value(data) + format_attribute_value(data, attribute) end defp process_attribute(_api, _record, _attr, _formats) do "..." end - defp format_attribute_value(data) do + defp format_attribute_value(data, %{type: Ash.Type.Binary}) when data not in [[], nil, ""] do + assigns = %{} + + ~F""" + (binary) + """ + end + + defp format_attribute_value(data, _attribute) do if is_binary(data) and !String.valid?(data) do "..." else