mathieuprog / polymorphic_embed

Polymorphic embeds in Ecto
Apache License 2.0
325 stars 63 forks source link

Undocumented breaking change to `to_form` in v4.0.0 #103

Open PolyCement opened 2 months ago

PolyCement commented 2 months ago

Hi, I just ran into an unexpected compilation error while updating to the latest version:

    error: undefined function to_form/5 (expected MyAppWeb.LiveComponents.QuestionForm to define such a function or for it to be imported, but none are available)
    │
 37 │             to_form(@form.source, @form, :config, input_value(@form, :type), []) do
    │                    ^^^^^^^
    │
    └─ lib/mo_app_web/live/live_components/question_form.ex:37:20: MyAppWeb.LiveComponents.QuestionForm.render/1

From the docs it seems that between v3.0.7 and v4.0.0 (specifically, in commit 7fc853b) the type argument was dropped, which isn't documented in the changelog. Not a huge deal as that commit also adds a new polymorphic_type option, so changing the call as follows got it compiling again:

to_form(@form.source, @form, :config, polymorphic_type: input_value(@form, :type))

However, since both the breaking change and the options for to_form/4 are undocumented, it took some rummaging around in the repo to figure out. Maybe it'd be good to mention it in the changelog so no one else gets stumped by it like I did?

(Glad to see this project is still active btw!)

mathieuprog commented 2 months ago

Hi. It is undocumented because it should be internal and not supposed to be used. This is the way to use this lib in forms: https://github.com/mathieuprog/polymorphic_embed?tab=readme-ov-file#displaying-form-inputs-and-errors-in-liveview

Could you compare that to your code and let me know?

PolyCement commented 2 months ago

Sorry, I didn't realise they were intended for internal use only!

I'm using a virtual field of the parent to set the embed type via a select, and the new polymorphic_embed_inputs_for component in the example you linked doesn't seem to have a type attribute that would let me use it in the same way. Using PolymorphicEmbed.HTML.Form.polymorphic_embed_inputs_for/4 as below seems to work just fine though:

polymorphic_embed_inputs_for @form, :config, input_value(@form, :type), fn config_form ->

However, to_form is also available from the Form module. Maybe the docs could state which functions are meant to be for internal use only? Either way, thanks for the help!