Skip to content

Commit 26e3300

Browse files
Steffen Deuschwoylie
authored andcommitted
finish implementation of hidden input name
hidden-input-for-type
1 parent 6d6cb78 commit 26e3300

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
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: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,6 @@ defmodule PolymorphicEmbedTest do
16061606
%{changeset: changeset, field: :channel}
16071607
)
16081608
|> Floki.parse_fragment!()
1609-
|> dbg()
16101609

16111610
assert [input] = Floki.find(html, "#reminder_channel_my_type_field")
16121611
assert Floki.attribute(input, "name") == ["reminder[channel][my_type_field]"]
@@ -1711,7 +1710,7 @@ defmodule PolymorphicEmbedTest do
17111710
expected_contents =
17121711
if(polymorphic?(generator),
17131712
do:
1714-
~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">),
1713+
~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">),
17151714
else:
17161715
~s(<input id="reminder_channel_address" name="reminder[channel][address]" type="text" value="a">)
17171716
)
@@ -1732,7 +1731,7 @@ defmodule PolymorphicEmbedTest do
17321731
expected_contents =
17331732
if(polymorphic?(generator),
17341733
do:
1735-
~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">),
1734+
~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">),
17361735
else:
17371736
~s(<input id="reminder_channel_address" name="reminder[channel][address]" type="text" value="a">)
17381737
)
@@ -1774,7 +1773,7 @@ defmodule PolymorphicEmbedTest do
17741773
expected_contents =
17751774
if(polymorphic?(generator),
17761775
do:
1777-
~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">),
1776+
~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">),
17781777
else:
17791778
~s(<input id="reminder_channel_number" name="reminder[channel][number]" type="text" value="1">)
17801779
)
@@ -1795,7 +1794,7 @@ defmodule PolymorphicEmbedTest do
17951794
expected_contents =
17961795
if(polymorphic?(generator),
17971796
do:
1798-
~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">),
1797+
~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">),
17991798
else:
18001799
~s(<input id="reminder_channel_number" name="reminder[channel][number]" type="text" value="1">)
18011800
)

0 commit comments

Comments
 (0)