mathieuprog

mathieuprog

Hello :waving_hand:

I published a library that brings support for polymorphic/dynamic embeds in Ecto.

Ecto’s embeds_one macro requires a specific schema module to be specified. This library removes this restriction by dynamically determining which schema to use, based on data to be stored (from a form or API) and retrieved (from the data source).

Example use case:

Say you have a Reminder schema. A reminder has a set date and text, but also specific fields whether it is an email reminder or sms reminder. Instead of adding and mixing all the email- and sms-related fields together in the Reminder schema, where one set of fields or the other are null values, you can add a polymorphic embed, only containing the relevant sms or email fields. The polymorphic embed also supports its own changeset validations.

See example code and usage in Readme file :backhand_index_pointing_down:

https://github.com/mathieuprog/polymorphic_embed

Inspired by ecto_poly library.

Showing Posts 16 to 7

munksgaard

munksgaard

This seems to work the first time I select something. But if I then change my selection, it doesn’t actually change anything. Even in my handle_event I’m not seeing the updated selection.

sodapopcan

sodapopcan

Big frameworks like Rails and Django calls the relational non-embedded versions “polymorphic associations” as does Ecto so that is likely what most people would be searching for.

murrgelb

murrgelb

I really love this and often faced the problem. The reason I haven’t found this before is the naming. Isn’t that more of a (algebraic) sum-type than polymorphism?

djcoin

djcoin

You just need to manipulate the type field in the polymorphic field (by default __type__).

So, given a “type” field (of type string) you could try something along this for example:

<.input field={@form[:type]} type="select" label="Select type" name={@form[:my_polymorphic_field].name <> "[__type__]"}
          options={[{:sms, :sms}, {:email, :email}]} prompt="" />
linusdm

linusdm

At work we got stuck using the library to allow changing the type using a form. Is polymorphic_embed intended for this usecase? Or is the type meant to be chose once at creation time, and then stay fixed throughout its lifetime?

Edit: sorry, did not intend to make this a reply… it was meant to be a general question.

KP123

KP123

The module, PolymorphicEmbed.HTML.Form appears to be entirely outdated for forms in Phoenix 1.7.2 and above. I needed PolymorphicEmbed.HTML.Form.get_polymorphic_type/3 to render the UI properly so I rewrote it:

def get_polymorphic_type(%Phoenix.HTML.Form{} = form, schema, field) do
    module = PolymorphicEmbed.get_polymorphic_module(schema, field, form.params)
    PolymorphicEmbed.get_polymorphic_type(schema, field, module)
end
mathieuprog

mathieuprog OP

A tricky bug has been fixed where atoms were not persisted in the compilation files. The atoms specifying the types were converted into strings in the Ecto Type’s init/1 function at compile-time, and so as we only worked with the strings afterwards, the atoms were not persisted:
https://github.com/mathieuprog/polymorphic_embed/issues/59

Fixed in 3.0.3 thanks to the support of @LostKobrakai on Slack and the contribution of Alessio ! :clap:

mathieuprog

mathieuprog OP

Version 3.0.0 has been released!

We now encourage the use of polymorphic_embeds_one/2 and polymorphic_embeds_many/2 macros to define your polymorphic embeds.

If you’re curious as to why: a field containing a polymorphic list of embeds defaulted to nil before version 3. To follow embeds_many/3 we should default to []. This can only be done by forcing the :default option of the field/3 to [] (ie we can’t configure this in the Type). I added the new macros in order to set these kind of defaults setting up the field for the polymorphic embeds.

Thanks to Alexandre @Matsa59 for his contribution which led to this update!

mathieuprog

mathieuprog OP

  • Support for LiveView forms has been added thanks to the quality contribution of Mathias!

  • Support for primary keys has been added

The latter may introduce breaking changes, therefore Polymoprhic Embed version has been bumped to 2.0.0

Migration from 1.x to 2.x is easy:

  • Make sure that every existing polymorphic embedded_schema contains the setting @primary_key false
belaustegui

belaustegui

Just wanted to jump in and say thanks for this awesome library @mathieuprog

We have been using it for the last few months at my day job and it has been an absolute pleasure. Works well and reliably and the documentation is top-notch.

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews