Skip to content

Commit a74f12f

Browse files
committed
Simplify get_field_opts
1 parent 75b7d84 commit a74f12f

3 files changed

Lines changed: 15 additions & 6 deletions

File tree

lib/polymorphic_embed.ex

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/polymorphic_embed/options_validator.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ defmodule PolymorphicEmbed.OptionsValidator do
1010
:use_parent_field_for_type,
1111
:retain_unlisted_types_on_load,
1212
:nilify_unlisted_types_on_load,
13+
:array?,
1314
# Ecto
1415
:field,
1516
:schema,

test/polymorphic_embed_test.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3414,7 +3414,9 @@ defmodule PolymorphicEmbedTest do
34143414
]
34153415
],
34163416
on_replace: :update,
3417-
type_field_name: :my_type_field
3417+
type_field_name: :my_type_field,
3418+
array?: false,
3419+
default: nil
34183420
]
34193421

34203422
PolymorphicEmbed.init(opts)

0 commit comments

Comments
 (0)