Skip to content

Commit

Permalink
Remove empty keys from Layout Resource Links (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
kgautreaux authored Oct 6, 2023
1 parent 12e9006 commit 2f31efe
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
18 changes: 16 additions & 2 deletions lib/beacon/live_admin/live/layout_editor_live/resource_links.ex
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,22 @@ defmodule Beacon.LiveAdmin.LayoutEditorLive.ResourceLinks do

case Map.fetch(params, field) do
{:ok, map} ->
list = Enum.sort_by(map, fn {key, _value} -> String.to_integer(key) end)
Map.put(params, field, Keyword.values(list))
list =
Enum.sort_by(map, &String.to_integer(elem(&1, 0)))
|> Enum.map(fn {_position, map} ->
Enum.reduce(map, [], fn {key, value}, acc ->
case value do
nil -> acc
"" -> acc
# only add non-empty values to the list
_ -> [{key, value} | acc]
end
end)
end)
|> List.flatten()
|> Enum.into(%{})

Map.put(params, field, list)

:error ->
params
Expand Down
42 changes: 41 additions & 1 deletion test/beacon/live_admin/live/layout_editor_live/edit_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@ defmodule Beacon.LiveAdmin.LayoutEditorLive.EditTest do
rpc(node1(), Beacon.Repo, :delete_all, [Beacon.Content.Layout, [log: false]])
end)

[layout: layout_fixture()]
[
layout: layout_fixture(),
resource_links_layout:
layout_fixture(node1(), %{
resource_links: [
%{
"crossorigin" => "",
"href" => "https://example.com",
"rel" => "preload",
"type" => "",
"as" => ""
}
]
})
]
end

test "save changes", %{conn: conn, layout: layout} do
Expand Down Expand Up @@ -36,4 +50,30 @@ defmodule Beacon.LiveAdmin.LayoutEditorLive.EditTest do

assert html =~ "Published"
end

test "simple remove nils from resource_links", %{
conn: conn,

Check warning on line 55 in test/beacon/live_admin/live/layout_editor_live/edit_test.exs

View workflow job for this annotation

GitHub Actions / test (OTP 23 | Elixir 1.13.0)

variable "conn" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 55 in test/beacon/live_admin/live/layout_editor_live/edit_test.exs

View workflow job for this annotation

GitHub Actions / test (OTP 26 | Elixir 1.15.1)

variable "conn" is unused (if the variable is not meant to be used, prefix it with an underscore)
resource_links_layout: resource_links_layout

Check warning on line 56 in test/beacon/live_admin/live/layout_editor_live/edit_test.exs

View workflow job for this annotation

GitHub Actions / test (OTP 23 | Elixir 1.13.0)

variable "resource_links_layout" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 56 in test/beacon/live_admin/live/layout_editor_live/edit_test.exs

View workflow job for this annotation

GitHub Actions / test (OTP 26 | Elixir 1.15.1)

variable "resource_links_layout" is unused (if the variable is not meant to be used, prefix it with an underscore)
} do
map =
Beacon.LiveAdmin.LayoutEditorLive.ResourceLinks.coerce_resource_link_params(%{
"resource_links" => %{
"0" => %{
"crossorigin" => nil,
"href" => "https://example.com",
"rel" => "preload",
"type" => "foo",
"as" => ""
}
}
})

assert map == %{
"resource_links" => %{
"href" => "https://example.com",
"rel" => "preload",
"type" => "foo"
}
}
end
end

0 comments on commit 2f31efe

Please sign in to comment.