Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/polymorphic_embed/html/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
default = options[:default] -> [default]
true -> []
end

data ->
List.wrap(data)
Expand Down
31 changes: 30 additions & 1 deletion test/polymorphic_embed_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 %>
</.polymorphic_embed_inputs_for>
</.form>
Expand Down