@@ -10,7 +10,11 @@ defmodule PolymorphicEmbed do
1010 alias PolymorphicEmbed.OptionsValidator
1111
1212 defmacro polymorphic_embeds_one ( field_name , opts ) do
13- opts = Keyword . update! ( opts , :types , & expand_alias ( & 1 , __CALLER__ ) )
13+ opts =
14+ opts
15+ |> Keyword . put_new ( :array? , false )
16+ |> Keyword . put_new ( :default , nil )
17+ |> Keyword . update! ( :types , & expand_alias ( & 1 , __CALLER__ ) )
1418
1519 quote do
1620 field ( unquote ( field_name ) , PolymorphicEmbed , unquote ( opts ) )
@@ -20,6 +24,7 @@ defmodule PolymorphicEmbed do
2024 defmacro polymorphic_embeds_many ( field_name , opts ) do
2125 opts =
2226 opts
27+ |> Keyword . put_new ( :array? , true )
2328 |> Keyword . put_new ( :default , [ ] )
2429 |> Keyword . update! ( :types , & expand_alias ( & 1 , __CALLER__ ) )
2530
@@ -113,7 +118,8 @@ defmodule PolymorphicEmbed do
113118 end )
114119
115120 % {
116- default: Keyword . get ( opts , :default ) ,
121+ array?: Keyword . fetch! ( opts , :array? ) ,
122+ default: Keyword . fetch! ( opts , :default ) ,
117123 use_parent_field_for_type: Keyword . get ( opts , :use_parent_field_for_type ) ,
118124 on_replace: Keyword . fetch! ( opts , :on_replace ) ,
119125 on_type_not_found: Keyword . fetch! ( opts , :on_type_not_found ) ,
@@ -633,9 +639,9 @@ defmodule PolymorphicEmbed do
633639 _ in UndefinedFunctionError ->
634640 reraise ArgumentError , "#{ inspect ( schema ) } is not an Ecto schema" , __STACKTRACE__
635641 else
636- { :parameterized , PolymorphicEmbed , options } -> Map . put ( options , :array? , false )
637- { :array , { :parameterized , PolymorphicEmbed , options } } -> Map . put ( options , :array? , true )
638- { _ , { :parameterized , PolymorphicEmbed , options } } -> Map . put ( options , :array? , false )
642+ { :parameterized , PolymorphicEmbed , options } -> options
643+ { :array , { :parameterized , PolymorphicEmbed , options } } -> options
644+ { _ , { :parameterized , PolymorphicEmbed , options } } -> options
639645 nil -> raise ArgumentError , "#{ field } is not a polymorphic embed"
640646 end
641647 end
0 commit comments