From 9a870edf983cd95dc2d3fe4b5ca1fd904830fde4 Mon Sep 17 00:00:00 2001 From: James Gough Date: Sat, 3 May 2025 18:05:11 +0100 Subject: [PATCH 1/3] test it --- test/polymorphic_embed_test.exs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/test/polymorphic_embed_test.exs b/test/polymorphic_embed_test.exs index 7dced00..8844431 100644 --- a/test/polymorphic_embed_test.exs +++ b/test/polymorphic_embed_test.exs @@ -2598,6 +2598,35 @@ defmodule PolymorphicEmbedTest do assert [input] = Floki.find(html, "#reminder_channel2_0_number") assert Floki.attribute(input, "type") == ["text"] end + + test "uses default value if value is nil" do + reminder_module = get_module(Reminder, :polymorphic) + + attrs = %{ + date: ~U[2020-05-28 02:57:19Z], + text: "This is an Email reminder", + channel2: nil + } + + changeset = + reminder_module + |> struct() + |> reminder_module.changeset(attrs) + + html = + render_component( + &liveview_form_component/1, + %{changeset: changeset, field: :channel2} + ) + |> Floki.parse_fragment!() + + assert [input] = Floki.find(html, ~s([name="reminder[channel2][__type__]"])) + assert Floki.attribute(input, "type") == ["hidden"] + assert Floki.attribute(input, "value") == ["sms"] + + assert [input] = Floki.find(html, "#reminder_channel2_0_number") + assert Floki.attribute(input, "type") == ["text"] + end end describe "polymorphic_embed_inputs_for/2" do @@ -3513,7 +3542,7 @@ defmodule PolymorphicEmbedTest do :let={f} for={@changeset} > - <.polymorphic_embed_inputs_for field={f[@field]} :let={sms_form}> + <.polymorphic_embed_inputs_for field={f[@field]} :let={sms_form} default={%PolymorphicEmbed.Channel.SMS{}}> <%= text_input sms_form, :number %> From ddc2768d3cf6d929a062ebd6c4b62449d185a505 Mon Sep 17 00:00:00 2001 From: James Gough Date: Sat, 3 May 2025 18:05:24 +0100 Subject: [PATCH 2/3] fix it --- lib/polymorphic_embed/html/helpers.ex | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/polymorphic_embed/html/helpers.ex b/lib/polymorphic_embed/html/helpers.ex index 7822d46..1091a55 100644 --- a/lib/polymorphic_embed/html/helpers.ex +++ b/lib/polymorphic_embed/html/helpers.ex @@ -62,7 +62,12 @@ if Code.ensure_loaded?(Phoenix.HTML) && Code.ensure_loaded?(Phoenix.HTML.Form) d nil -> type = Keyword.get(options, :polymorphic_type, get_polymorphic_type(form, field)) module = PolymorphicEmbed.get_polymorphic_module(struct.__struct__, field, type) - if module, do: [struct(module)], else: [] + + cond do + module -> [struct(module)] + options[:default] -> [options[:default]] + true -> [] + end data -> List.wrap(data) From 8ddf0b36d23ff6305e77f028fe232a7fa892860b Mon Sep 17 00:00:00 2001 From: Andrea Leopardi Date: Tue, 6 May 2025 14:26:55 +0200 Subject: [PATCH 3/3] Update lib/polymorphic_embed/html/helpers.ex --- lib/polymorphic_embed/html/helpers.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/polymorphic_embed/html/helpers.ex b/lib/polymorphic_embed/html/helpers.ex index 1091a55..433a73a 100644 --- a/lib/polymorphic_embed/html/helpers.ex +++ b/lib/polymorphic_embed/html/helpers.ex @@ -65,7 +65,7 @@ if Code.ensure_loaded?(Phoenix.HTML) && Code.ensure_loaded?(Phoenix.HTML.Form) d cond do module -> [struct(module)] - options[:default] -> [options[:default]] + default = options[:default] -> [default] true -> [] end