tmbb / mandarin

Admin generators (and utilities) for phoenix + ecto
29 stars 4 forks source link

Supporting maps and embedded schemas #3

Open lewisf opened 4 years ago

lewisf commented 4 years ago

:map types and :embedded_schema types lead to a broken input right now.

I've fixed it locally by editing the field in edit.html.eex to render a text area instead

<%= textarea f, :my_map_field, class: "form-control", rows: 20, style: "font-family: monospace" %>

In order to actually render this, I've implemented the Phoenix.HTML.Safe protocol for Map (and all the other embedded schema types)

defimpl Phoenix.HTML.Safe, for: My.Embedded.Schema do
  def to_iodata(data), do: Jason.encode!(data, escape: :html_safe, pretty: true)
end

defimpl Phoenix.HTML.Safe, for: Map do
  def to_iodata(data), do: Jason.encode!(data, escape: :html_safe, pretty: true)
end

I also edited the update action within the generated controller to decode

    case Admin.update_user_identity(
           my_schema,
           my_schema
           |> Map.get_and_update("my_map_field", fn cv -> {cv, Jason.decode!(cv)} end)
           |> elem(1)
         ) do
           ...

While I can continue to do this to get these fields working, I'm wondering if handling of these types can be automatically supported by Mandarin by default?

Willing to help out if needed on this one. Loving Mandarin so far.

tmbb commented 3 years ago

Sorry for only answering now! I'll think about it. I have made some pretty important changes in Mandarin in the meantime, though. When I publish my changes I'll look into this issue.