Skip to content

Commit 84a7da8

Browse files
authored
Merge pull request #3 from woylie/hidden-input-for-type
Hidden input for type
2 parents c9e1a1d + 16f0101 commit 84a7da8

File tree

3 files changed

+81
-9
lines changed

3 files changed

+81
-9
lines changed

lib/polymorphic_embed.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,14 @@ defmodule PolymorphicEmbed do
4646
}
4747
end)
4848

49+
type_field = Keyword.get(opts, :type_field, :__type__)
50+
4951
%{
5052
default: Keyword.get(opts, :default, nil),
5153
on_replace: Keyword.fetch!(opts, :on_replace),
5254
on_type_not_found: Keyword.get(opts, :on_type_not_found, :changeset_error),
53-
type_field: Keyword.get(opts, :type_field, :__type__) |> to_string(),
55+
type_field: type_field |> to_string(),
56+
type_field_atom: type_field,
5457
types_metadata: types_metadata
5558
}
5659
end
@@ -384,7 +387,8 @@ defmodule PolymorphicEmbed do
384387
Enum.find(types_metadata, &(type == to_string(&1.type)))
385388
end
386389

387-
defp get_field_options(schema, field) do
390+
@doc false
391+
def get_field_options(schema, field) do
388392
try do
389393
schema.__schema__(:type, field)
390394
rescue

lib/polymorphic_embed/html/form.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ if Code.ensure_loaded?(Phoenix.HTML) && Code.ensure_loaded?(Phoenix.HTML.Form) d
153153
valid?: errors == []
154154
}
155155

156+
%schema{} = form.source.data
157+
%{type_field_atom: type_field} = PolymorphicEmbed.get_field_options(schema, field)
158+
156159
%Phoenix.HTML.Form{
157160
source: changeset,
158161
impl: Phoenix.HTML.FormData.Ecto.Changeset,
@@ -162,7 +165,7 @@ if Code.ensure_loaded?(Phoenix.HTML) && Code.ensure_loaded?(Phoenix.HTML.Form) d
162165
errors: errors,
163166
data: data,
164167
params: params,
165-
hidden: [__type__: to_string(type)],
168+
hidden: [{type_field, to_string(type)}],
166169
options: options
167170
}
168171
end)

test/polymorphic_embed_test.exs

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,7 +1582,7 @@ defmodule PolymorphicEmbedTest do
15821582
end
15831583

15841584
describe "polymorphic_embed_inputs_for/2" do
1585-
test "generates forms that can be rendered" do
1585+
test "generates forms that can be rendered (custom type field/identify_by_fields)" do
15861586
reminder_module = get_module(Reminder, :polymorphic)
15871587

15881588
attrs = %{
@@ -1607,13 +1607,78 @@ defmodule PolymorphicEmbedTest do
16071607
)
16081608
|> Floki.parse_fragment!()
16091609

1610-
assert [input] = Floki.find(html, "#reminder_channel___type__")
1610+
assert [input] = Floki.find(html, "#reminder_channel_my_type_field")
1611+
assert Floki.attribute(input, "name") == ["reminder[channel][my_type_field]"]
16111612
assert Floki.attribute(input, "type") == ["hidden"]
16121613
assert Floki.attribute(input, "value") == ["email"]
16131614

16141615
assert [input] = Floki.find(html, "#reminder_channel_number")
16151616
assert Floki.attribute(input, "type") == ["text"]
16161617
end
1618+
1619+
test "generates forms that can be rendered (custom type field)" do
1620+
reminder_module = get_module(Reminder, :polymorphic)
1621+
1622+
attrs = %{
1623+
date: ~U[2020-05-28 02:57:19Z],
1624+
text: "This is an Email reminder",
1625+
channel3: %{
1626+
my_type_field: "email"
1627+
}
1628+
}
1629+
1630+
changeset =
1631+
reminder_module
1632+
|> struct()
1633+
|> reminder_module.changeset(attrs)
1634+
1635+
html =
1636+
render_component(
1637+
&liveview_form/1,
1638+
%{changeset: changeset, field: :channel3}
1639+
)
1640+
|> Floki.parse_fragment!()
1641+
1642+
assert [input] = Floki.find(html, "#reminder_channel3_my_type_field")
1643+
assert Floki.attribute(input, "name") == ["reminder[channel3][my_type_field]"]
1644+
assert Floki.attribute(input, "type") == ["hidden"]
1645+
assert Floki.attribute(input, "value") == ["email"]
1646+
end
1647+
1648+
test "generates forms that can be rendered (default type field)" do
1649+
reminder_module = get_module(Reminder, :polymorphic)
1650+
1651+
attrs = %{
1652+
date: ~U[2020-05-28 02:57:19Z],
1653+
text: "This is an Email reminder",
1654+
channel2: %{
1655+
__type__: "email",
1656+
address: "a",
1657+
valid: true,
1658+
confirmed: true
1659+
}
1660+
}
1661+
1662+
changeset =
1663+
reminder_module
1664+
|> struct()
1665+
|> reminder_module.changeset(attrs)
1666+
1667+
html =
1668+
render_component(
1669+
&liveview_form/1,
1670+
%{changeset: changeset, field: :channel2}
1671+
)
1672+
|> Floki.parse_fragment!()
1673+
1674+
assert [input] = Floki.find(html, "#reminder_channel2___type__")
1675+
assert Floki.attribute(input, "name") == ["reminder[channel2][__type__]"]
1676+
assert Floki.attribute(input, "type") == ["hidden"]
1677+
assert Floki.attribute(input, "value") == ["email"]
1678+
1679+
assert [input] = Floki.find(html, "#reminder_channel2_number")
1680+
assert Floki.attribute(input, "type") == ["text"]
1681+
end
16171682
end
16181683

16191684
test "inputs_for/4" do
@@ -1644,7 +1709,7 @@ defmodule PolymorphicEmbedTest do
16441709
expected_contents =
16451710
if(polymorphic?(generator),
16461711
do:
1647-
~s(<input id="reminder_channel___type__" name="reminder[channel][__type__]" type="hidden" value="email"><input id="reminder_channel_address" name="reminder[channel][address]" type="text" value="a">),
1712+
~s(<input id="reminder_channel_my_type_field" name="reminder[channel][my_type_field]" type="hidden" value="email"><input id="reminder_channel_address" name="reminder[channel][address]" type="text" value="a">),
16481713
else:
16491714
~s(<input id="reminder_channel_address" name="reminder[channel][address]" type="text" value="a">)
16501715
)
@@ -1665,7 +1730,7 @@ defmodule PolymorphicEmbedTest do
16651730
expected_contents =
16661731
if(polymorphic?(generator),
16671732
do:
1668-
~s(<input id="reminder_channel___type__" name="reminder[channel][__type__]" type="hidden" value="email"><input id="reminder_channel_address" name="reminder[channel][address]" type="text" value="a">),
1733+
~s(<input id="reminder_channel_my_type_field" name="reminder[channel][my_type_field]" type="hidden" value="email"><input id="reminder_channel_address" name="reminder[channel][address]" type="text" value="a">),
16691734
else:
16701735
~s(<input id="reminder_channel_address" name="reminder[channel][address]" type="text" value="a">)
16711736
)
@@ -1707,7 +1772,7 @@ defmodule PolymorphicEmbedTest do
17071772
expected_contents =
17081773
if(polymorphic?(generator),
17091774
do:
1710-
~s(<input id="reminder_channel___type__" name="reminder[channel][__type__]" type="hidden" value="sms"><input id="reminder_channel_number" name="reminder[channel][number]" type="text" value="1">),
1775+
~s(<input id="reminder_channel_my_type_field" name="reminder[channel][my_type_field]" type="hidden" value="sms"><input id="reminder_channel_number" name="reminder[channel][number]" type="text" value="1">),
17111776
else:
17121777
~s(<input id="reminder_channel_number" name="reminder[channel][number]" type="text" value="1">)
17131778
)
@@ -1728,7 +1793,7 @@ defmodule PolymorphicEmbedTest do
17281793
expected_contents =
17291794
if(polymorphic?(generator),
17301795
do:
1731-
~s(<input id="reminder_channel___type__" name="reminder[channel][__type__]" type="hidden" value="sms"><input id="reminder_channel_number" name="reminder[channel][number]" type="text" value="1">),
1796+
~s(<input id="reminder_channel_my_type_field" name="reminder[channel][my_type_field]" type="hidden" value="sms"><input id="reminder_channel_number" name="reminder[channel][number]" type="text" value="1">),
17321797
else:
17331798
~s(<input id="reminder_channel_number" name="reminder[channel][number]" type="text" value="1">)
17341799
)

0 commit comments

Comments
 (0)