Skip to content

Commit

Permalink
fix: get checks passing, fix various bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Apr 3, 2024
1 parent 2b94944 commit 5d1b3a1
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 52 deletions.
1 change: 1 addition & 0 deletions .check.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
## curated tools may be disabled (e.g. the check for compilation warnings)
# {:compiler, false},
{:npm_test, false},
{:gettext, false},
{:check_formatter, command: "mix spark.formatter --check"}

## ...or adjusted (e.g. use one-line formatter for more compact credo output)
Expand Down
4 changes: 2 additions & 2 deletions lib/ash_admin/components/resource/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ defmodule AshAdmin.Components.Resource.Form do
def relationships(resource, action, nil) do
resource
|> Ash.Resource.Info.relationships()
|> Enum.filter(& &1.writable? && &1.public?)
|> Enum.filter(&(&1.writable? && &1.public?))
|> only_accepted(action)
|> sort_relationships()
end
Expand Down Expand Up @@ -1523,7 +1523,7 @@ defmodule AshAdmin.Components.Resource.Form do
attributes =
resource
|> Ash.Resource.Info.attributes()
|> Enum.filter(& &1.writable? && &1.public?)
|> Enum.filter(&(&1.writable? && &1.public?))
|> Enum.reject(&(&1.name == :autogenerated_id))
|> only_accepted(action)
|> Enum.reject(fn attribute ->
Expand Down
4 changes: 2 additions & 2 deletions lib/ash_admin/components/resource/metadata_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ defmodule AshAdmin.Components.Resource.MetadataTable do
defp attributes(resource) do
resource
|> Ash.Resource.Info.attributes()
|> Enum.sort_by(& not(&1.public?))
|> Enum.sort_by(&(not &1.public?))
end

defp attribute_type(attribute) do
Expand All @@ -173,6 +173,6 @@ defmodule AshAdmin.Components.Resource.MetadataTable do
defp relationships(resource) do
resource
|> Ash.Resource.Info.relationships()
|> Enum.sort_by(& not(&1.public?))
|> Enum.sort_by(&(not &1.public?))
end
end
113 changes: 91 additions & 22 deletions lib/ash_admin/components/resource/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ defmodule AshAdmin.Components.Resource.Show do

~H"""
<div class="mb-10">
<%= render_attributes(assigns, @record, @destination) %>
<%= render_attributes(assigns, @record, @destination, @name) %>
<div class="px-4 py-3 text-right sm:px-6">
<.link
:if={AshAdmin.Resource.show_action(@destination)}
Expand Down Expand Up @@ -192,7 +192,7 @@ defmodule AshAdmin.Components.Resource.Show do
"""
end

defp render_attributes(assigns, record, resource) do
defp render_attributes(assigns, record, resource, relationship_name \\ nil) do
{attributes, flags, bottom_attributes, _} =
AshAdmin.Components.Resource.Form.attributes(resource, :show)

Expand All @@ -202,7 +202,8 @@ defmodule AshAdmin.Components.Resource.Show do
resource: resource,
attributes: attributes,
flags: flags,
bottom_attributes: bottom_attributes
bottom_attributes: bottom_attributes,
relationship_name: relationship_name
)

~H"""
Expand All @@ -219,7 +220,13 @@ defmodule AshAdmin.Components.Resource.Show do
>
<div class="block text-sm font-medium text-gray-700"><%= to_name(attribute.name) %></div>
<div>
<%= render_maybe_sensitive_attribute(assigns, @resource, @record, attribute) %>
<%= render_maybe_sensitive_attribute(
assigns,
@resource,
@record,
attribute,
@relationship_name
) %>
</div>
</div>
</div>
Expand All @@ -241,7 +248,13 @@ defmodule AshAdmin.Components.Resource.Show do
>
<div class="block text-sm font-medium text-gray-700"><%= to_name(attribute.name) %></div>
<div>
<%= render_maybe_sensitive_attribute(assigns, @resource, @record, attribute) %>
<%= render_maybe_sensitive_attribute(
assigns,
@resource,
@record,
attribute,
@relationship_name
) %>
</div>
</div>
</div>
Expand All @@ -264,39 +277,46 @@ defmodule AshAdmin.Components.Resource.Show do
>
<div class="block text-sm font-medium text-gray-700"><%= to_name(attribute.name) %></div>
<div>
<%= render_maybe_sensitive_attribute(assigns, @resource, @record, attribute) %>
<%= render_maybe_sensitive_attribute(
assigns,
@resource,
@record,
attribute,
@relationship_name
) %>
</div>
</div>
</div>
"""
end

defp render_maybe_sensitive_attribute(assigns, resource, record, attribute) do
assigns = assign(assigns, attribute: attribute)
defp render_maybe_sensitive_attribute(assigns, resource, record, attribute, relationship_name) do
assigns = assign(assigns, attribute: attribute, relationship_name: relationship_name)
show_sensitive_fields = AshAdmin.Resource.show_sensitive_fields(resource)

if attribute.sensitive? && not Enum.member?(show_sensitive_fields, attribute.name) do
~H"""
<.live_component
id={"#{@record.id}-#{@attribute.name}"}
id={"#{@relationship_name}_#{AshAdmin.Helpers.encode_primary_key(@record)}-#{@attribute.name}"}
module={SensitiveAttribute}
value={Map.get(@record, @attribute.name)}
>
<%= render_attribute(assigns, @resource, @record, @attribute) %>
<%= render_attribute(assigns, @resource, @record, @attribute, @relationship_name) %>
</.live_component>
"""
else
render_attribute(assigns, resource, record, attribute)
render_attribute(assigns, resource, record, attribute, relationship_name)
end
end

defp render_attribute(assigns, resource, record, attribute, nested? \\ false)
defp render_attribute(assigns, resource, record, attribute, relationship_name, nested? \\ false)

defp render_attribute(
assigns,
resource,
record,
%{type: {:array, type}, name: name} = attribute,
relationship_name,
nested?
) do
if Map.get(record, name) in [[], nil] do
Expand All @@ -309,6 +329,7 @@ defmodule AshAdmin.Components.Resource.Show do
type: type,
name: name,
attribute: attribute,
relationship_name: relationship_name,
nested: nested?
)

Expand All @@ -321,6 +342,7 @@ defmodule AshAdmin.Components.Resource.Show do
@resource,
Map.put(@record, @name, value),
%{@attribute | type: @type},
@relationship_name,
true
) %>
</li>
Expand All @@ -334,6 +356,7 @@ defmodule AshAdmin.Components.Resource.Show do
@resource,
Map.put(@record, @name, value),
%{@attribute | type: @type},
@relationship_name,
true
) %>
</li>
Expand All @@ -349,29 +372,57 @@ defmodule AshAdmin.Components.Resource.Show do
resource,
record,
%{type: {:array, Ash.Type.Map}} = attribute,
relationship_name,
nested?
) do
render_attribute(assigns, resource, record, %{attribute | type: Ash.Type.Map}, nested?)
render_attribute(
assigns,
resource,
record,
%{attribute | type: Ash.Type.Map},
relationship_name,
nested?
)
end

defp render_attribute(assigns, _resource, record, %{type: Ash.Type.Map} = attribute, _nested?) do
defp render_attribute(
assigns,
_resource,
record,
%{type: Ash.Type.Map} = attribute,
relationship_name,
_nested?
) do
encoded = Jason.encode!(Map.get(record, attribute.name))

assigns = assign(assigns, record: record, attribute: attribute, encoded: encoded)
assigns =
assign(assigns,
record: record,
attribute: attribute,
encoded: encoded,
relationship_name: relationship_name
)

~H"""
<div
phx-hook="JsonView"
data-json={@encoded}
id={"_#{AshAdmin.Helpers.encode_primary_key(@record)}_#{@attribute.name}_json"}
id={"#{@relationship_name}_#{AshAdmin.Helpers.encode_primary_key(@record)}_#{@attribute.name}_json"}
/>
"""
rescue
_ ->
"..."
end

defp render_attribute(assigns, _resource, record, %{name: name, type: Ash.Type.Boolean}, _) do
defp render_attribute(
assigns,
_resource,
record,
%{name: name, type: Ash.Type.Boolean},
_relationship_name,
_
) do
case Map.get(record, name) do
true ->
~H"""
Expand All @@ -390,7 +441,14 @@ defmodule AshAdmin.Components.Resource.Show do
end
end

defp render_attribute(assigns, _resource, record, %{name: name, type: Ash.Type.Binary}, _) do
defp render_attribute(
assigns,
_resource,
record,
%{name: name, type: Ash.Type.Binary},
_relationship_name,
_
) do
if Map.get(record, name) do
~H"""
<span class="italic">(binary data)</span>
Expand All @@ -400,7 +458,7 @@ defmodule AshAdmin.Components.Resource.Show do
end
end

defp render_attribute(assigns, resource, record, attribute, nested?) do
defp render_attribute(assigns, resource, record, attribute, relationship_name, nested?) do
if Ash.Type.embedded_type?(attribute.type) do
if Map.get(record, attribute.name) in [nil, []] do
"None"
Expand All @@ -410,17 +468,28 @@ defmodule AshAdmin.Components.Resource.Show do
resource: resource,
record: record,
attribute: attribute,
nested: nested?
nested: nested?,
relationship_name: relationship_name
)

~H"""
<%= if @nested do %>
<div class="ml-1 pl-2 pr-2">
<%= render_attributes(assigns, Map.get(@record, @attribute.name), @attribute.type) %>
<%= render_attributes(
assigns,
Map.get(@record, @attribute.name),
@attribute.type,
@relationship_name
) %>
</div>
<% else %>
<div class="shadow-md border mt-4 mb-4 rounded py-2 px-2 ml-1 pl-2 pr-2">
<%= render_attributes(assigns, Map.get(@record, @attribute.name), @attribute.type) %>
<%= render_attributes(
assigns,
Map.get(@record, @attribute.name),
@attribute.type,
@relationship_name
) %>
</div>
<% end %>
"""
Expand Down
59 changes: 52 additions & 7 deletions lib/ash_admin/components/resource/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,24 @@ defmodule AshAdmin.Components.Resource.Table do
|> Enum.reject(&(&1.name in skip))
end

defp render_attribute(domain, record, attribute, formats, show_sensitive_fields, actor, relationship_name) do
process_attribute(domain, record, attribute, formats, show_sensitive_fields, actor, relationship_name)
defp render_attribute(
domain,
record,
attribute,
formats,
show_sensitive_fields,
actor,
relationship_name
) do
process_attribute(
domain,
record,
attribute,
formats,
show_sensitive_fields,
actor,
relationship_name
)
rescue
_ ->
"..."
Expand Down Expand Up @@ -138,13 +154,29 @@ defmodule AshAdmin.Components.Resource.Table do
attributes = attributes(attribute.destination, display_attributes, [])

Enum.map_join(attributes, " - ", fn x ->
render_attribute(domain, relationship, x, formats, show_sensitive_fields, actor, relationship_name)
render_attribute(
domain,
relationship,
x,
formats,
show_sensitive_fields,
actor,
relationship_name
)
end)
end
end
end

defp process_attribute(_, record, %struct{} = attribute, formats, show_sensitive_fields, _actor, relationship_name)
defp process_attribute(
_,
record,
%struct{} = attribute,
formats,
show_sensitive_fields,
_actor,
relationship_name
)
when struct in [Ash.Resource.Attribute, Ash.Resource.Aggregate, Ash.Resource.Calculation] do
{mod, func, args} =
Keyword.get(formats || [], attribute.name, {Phoenix.HTML.Safe, :to_iodata, []})
Expand All @@ -162,16 +194,29 @@ defmodule AshAdmin.Components.Resource.Table do
end
end

defp process_attribute(_domain, _record, _attr, _formats, _show_sensitive_fields, _actor) do
defp process_attribute(
_domain,
_record,
_attr,
_formats,
_show_sensitive_fields,
_actor,
_relationship_name
) do
"..."
end

defp format_sensitive_value(value, attribute, record, relationship_name) do
assigns = %{value: value, attribute: attribute, record: record, relationship_name: relationship_name}
assigns = %{
value: value,
attribute: attribute,
record: record,
relationship_name: relationship_name
}

~H"""
<.live_component
id={"#{@relationship_name}-#{@record.id}-#{@attribute.name}"}
id={"#{@relationship_name}-#{AshAdmin.Helpers.encode_primary_key(@record)}-#{@attribute.name}"}
module={SensitiveAttribute}
value={@value}
>
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule AshAdmin.MixProject do
end

defp elixirc_paths(:test) do
["test/support", "lib"]
["test/support", "lib", "dev"]
end

defp elixirc_paths(:prod) do
Expand Down
Loading

0 comments on commit 5d1b3a1

Please sign in to comment.