Closed MrYawe closed 5 months ago
Hi. What does
mix xref graph --label compile-connected --fail-above 0
say for your current code?
Thanks for the quick response ❤️
I've found a way to fix it using macros:
defmodule MyApp.Reminder do
use Ecto.Schema
import Ecto.Changeset
import PolymorphicEmbed
import MyApp.Reminder.Channels
schema "reminders" do
embeds_one_channel(:channel)
end
end
defmodule MyApp.Reminder.Channels do
@channels [
sms: MyApp.Channel.SMS,
email: MyApp.Channel.Email
]
defmacro embeds_one_channel(field_name) do
quote do
polymorphic_embeds_one(unquote(field_name),
types: unquote(@channels),
type_field: :type,
on_type_not_found: :raise,
on_replace: :delete
)
end
end
end
@MrYawe Cool:) I'll suggest that snippet to others. Do you confirm though that your compilation was suboptimal through
mix xref graph --label compile-connected --fail-above 0
and that it made a difference?
In other words, is the warning message even right?
@mathieuprog From what I'm seeing, there is no difference.
Every schemas (let's call them Reminder1, Reminder2, Reminder3) that has the same polymorphic_embeds_one
field with channels types has as compile time dependency to MyApp.Reminder.Channels
. Thats seems expected If I understand compilation correctly.
mix xref graph --label compile-connected --fail-above 0
Results before and after:
...
lib/my_app/reminder_1.ex
└── lib/my_app/channels.ex (compile)
lib/my_app/reminder_2.ex
└── lib/my_app/channels.ex (compile)
lib/my_app/reminder_3.ex
└── lib/my_app/channels.ex (compile)
...
It might have been me not understanding the compile-time dependencies: https://github.com/mathieuprog/polymorphic_embed/issues/85
In which case you refactored for nothing and made your code more complicated for nothing.
I'll make a test project and try to understand better. I added the warning too fast.
Since polymorphic_embed 3.0.7 I get this warning:
I understand that I must refactor my code to avoid unnecessary compile-time dependencies but I'm not sure how.
Here is how I use polymorphic_embed with a fictional example:
The reason why we use a separate module to list all polymorphic_embed types is because we are using it in a lot of different schemas. Can I refactor my code while keeping a single source of truth with all types?