@@ -2045,7 +2045,7 @@ defmodule PolymorphicEmbedTest do
20452045 end
20462046
20472047 describe "Form.get_polymorphic_type/3" do
2048- test "returns type from changeset" do
2048+ test "returns type from changeset via identify_by_fields " do
20492049 reminder_module = get_module ( Reminder , :polymorphic )
20502050
20512051 attrs = % {
@@ -2094,9 +2094,9 @@ defmodule PolymorphicEmbedTest do
20942094 end )
20952095 end
20962096
2097- test "returns type from string parameters " do
2097+ test "returns type from changeset via custom type field " do
20982098 reminder_module = get_module ( Reminder , :polymorphic )
2099- attrs = % { "channel" => % { "my_type_field" => "email " } }
2099+ attrs = % { "channel" => % { "my_type_field" => "sms " } }
21002100
21012101 changeset =
21022102 reminder_module
@@ -2105,30 +2105,81 @@ defmodule PolymorphicEmbedTest do
21052105
21062106 safe_form_for ( changeset , fn f ->
21072107 assert PolymorphicEmbed.HTML.Form . get_polymorphic_type ( f , reminder_module , :channel ) ==
2108+ :sms
2109+
2110+ text_input ( f , :text )
2111+ end )
2112+ end
2113+
2114+ test "returns type from map with default type field (string)" do
2115+ reminder_module = get_module ( Reminder , :polymorphic )
2116+ attrs = % { "channel2" => % { "__type__" => "email" } }
2117+
2118+ changeset =
2119+ reminder_module
2120+ |> struct ( )
2121+ |> reminder_module . changeset ( attrs )
2122+
2123+ safe_form_for ( changeset , fn f ->
2124+ assert PolymorphicEmbed.HTML.Form . get_polymorphic_type ( f , reminder_module , :channel2 ) ==
21082125 :email
21092126
21102127 text_input ( f , :text )
21112128 end )
21122129 end
21132130
2114- test "returns type from atom parameters " do
2131+ test "returns type from map with default type field (atom) " do
21152132 reminder_module = get_module ( Reminder , :polymorphic )
2116- attrs = % { channel: % { my_type_field : :email } }
2133+ attrs = % { "channel2" => % { __type__ : :email } }
21172134
21182135 changeset =
21192136 reminder_module
21202137 |> struct ( )
21212138 |> reminder_module . changeset ( attrs )
21222139
21232140 safe_form_for ( changeset , fn f ->
2124- assert PolymorphicEmbed.HTML.Form . get_polymorphic_type ( f , reminder_module , :channel ) ==
2141+ assert PolymorphicEmbed.HTML.Form . get_polymorphic_type ( f , reminder_module , :channel2 ) ==
2142+ :email
2143+
2144+ text_input ( f , :text )
2145+ end )
2146+ end
2147+
2148+ test "returns type from map with custom type field (string)" do
2149+ reminder_module = get_module ( Reminder , :polymorphic )
2150+ attrs = % { "channel3" => % { "my_type_field" => "email" } }
2151+
2152+ changeset =
2153+ reminder_module
2154+ |> struct ( )
2155+ |> reminder_module . changeset ( attrs )
2156+
2157+ safe_form_for ( changeset , fn f ->
2158+ assert PolymorphicEmbed.HTML.Form . get_polymorphic_type ( f , reminder_module , :channel3 ) ==
2159+ :email
2160+
2161+ text_input ( f , :text )
2162+ end )
2163+ end
2164+
2165+ test "returns type from map with custom type field (atom)" do
2166+ reminder_module = get_module ( Reminder , :polymorphic )
2167+ attrs = % { "channel3" => % { my_type_field: "email" } }
2168+
2169+ changeset =
2170+ reminder_module
2171+ |> struct ( )
2172+ |> reminder_module . changeset ( attrs )
2173+
2174+ safe_form_for ( changeset , fn f ->
2175+ assert PolymorphicEmbed.HTML.Form . get_polymorphic_type ( f , reminder_module , :channel3 ) ==
21252176 :email
21262177
21272178 text_input ( f , :text )
21282179 end )
21292180 end
21302181
2131- test "returns type from parameters while type field is custom " do
2182+ test "returns nil with map when custom type field is configured and default type field is set " do
21322183 reminder_module = get_module ( Reminder , :polymorphic )
21332184 attrs = % { channel: % { __type__: :email } }
21342185
0 commit comments