Skip to content

Supporting maps and embedded schemas #3

@lewisf

Description

@lewisf

: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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions