Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix nested embeds errors #115

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/polymorphic_embed/html/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ if Code.ensure_loaded?(Phoenix.HTML) && Code.ensure_loaded?(Phoenix.HTML.Form) d
index: if(array?, do: i),
errors: errors,
data: data,
action: parent_action,
params: params,
hidden: [{type_field_name, to_string(type)}],
options: options
Expand Down
55 changes: 55 additions & 0 deletions test/polymorphic_embed_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2503,6 +2503,43 @@ defmodule PolymorphicEmbedTest do
end

describe "polymorphic_embed_inputs_for/1" do
test "errors in form for polymorphic embed and nested embed" do
reminder_module = get_module(Reminder, :polymorphic)

sms_reminder_attrs = %{
text: "This is an SMS reminder",
contexts: [
%{
__type__: "device",
extra: %{}
}
]
}

changeset =
reminder_module
|> struct()
|> reminder_module.changeset(sms_reminder_attrs)

changeset = %{changeset | action: :insert}

html_string =
render_component(
&liveview_form_with_inputs_for/1,
%{changeset: changeset, field: :contexts}
)

assert String.contains?(
html_string,
"[type: {"can't be blank", [validation: :required]}]"
)

assert String.contains?(
html_string,
"[imei: {"can't be blank", [validation: :required]}]"
)
end

test "generates forms that can be rendered (custom type field/identify_by_fields)" do
reminder_module = get_module(Reminder, :polymorphic)

Expand Down Expand Up @@ -3520,6 +3557,24 @@ defmodule PolymorphicEmbedTest do
"""
end

defp liveview_form_with_inputs_for(assigns) do
~H"""
<.form
:let={f}
for={@changeset}
>
<.polymorphic_embed_inputs_for field={f[@field]} :let={sms_form}>
<%= text_input sms_form, :number %>
<%= sms_form.errors |> inspect() %>
<.inputs_for field={sms_form[:extra]} :let={channel_form}>
<%= text_input channel_form, :imei %>
<%= channel_form.errors |> inspect() %>
</.inputs_for>
</.polymorphic_embed_inputs_for>
</.form>
"""
end

defp polymorphic?(:polymorphic), do: true
defp polymorphic?(:not_polymorphic), do: false
end
5 changes: 2 additions & 3 deletions test/support/models/polymorphic/reminder/context/device.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ defmodule PolymorphicEmbed.Reminder.Context.Device do
field :ref, :string
field :type, :string

embeds_one :extra, Extra do
field :imei, :string
end
embeds_one :extra, PolymorphicEmbed.Reminder.Context.Extra
end

def changeset(struct, params) do
struct
|> cast(params, ~w(ref type)a)
|> validate_required(~w(type)a)
|> cast_embed(:extra)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ defmodule PolymorphicEmbed.Reminder.Context.DeviceNoId do
field :ref, :string
field :type, :string

embeds_one :extra, Extra do
field :imei, :string
end
embeds_one :extra, PolymorphicEmbed.Reminder.Context
end

def changeset(struct, params) do
Expand Down
14 changes: 14 additions & 0 deletions test/support/models/polymorphic/reminder/context/extra.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule PolymorphicEmbed.Reminder.Context.Extra do
use Ecto.Schema
import Ecto.Changeset

embedded_schema do
field :imei, :string
end

def changeset(extra, attrs) do
extra
|> cast(attrs, [:imei])
|> validate_required([:imei])
end
end