diff --git a/lib/polymorphic_embed.ex b/lib/polymorphic_embed.ex index 848ffb5..26ee65a 100644 --- a/lib/polymorphic_embed.ex +++ b/lib/polymorphic_embed.ex @@ -332,6 +332,18 @@ defmodule PolymorphicEmbed do |> String.to_atom() end + @doc """ + Returns the possible types for a given schema and field + + you can call `types/2` like this: + PolymorphicEmbed.types(MySchema, :contexts) + #=> [:location, :age, :device] + """ + def types(schema, field) do + %{types_metadata: types_metadata} = get_field_options(schema, field) + Enum.map(types_metadata, &String.to_existing_atom(&1.type)) + end + defp get_metadata_for_module(module, types_metadata) do Enum.find(types_metadata, &(module == &1.module)) end diff --git a/test/polymorphic_embed_test.exs b/test/polymorphic_embed_test.exs index 0586e18..4864210 100644 --- a/test/polymorphic_embed_test.exs +++ b/test/polymorphic_embed_test.exs @@ -1597,6 +1597,13 @@ defmodule PolymorphicEmbedTest do end end + describe "types/2" do + test "returns the types for a polymoprhic embed field" do + assert PolymorphicEmbed.types(PolymorphicEmbed.Reminder, :channel) == + [:sms, :email] + end + end + describe "get_polymorphic_module/3" do test "returns the module for a type" do assert PolymorphicEmbed.get_polymorphic_module(PolymorphicEmbed.Reminder, :channel, :sms) ==