Skip to content

Commit

Permalink
fix: Embeds Many were crashing create forms (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
staylorwr authored Jan 24, 2024
1 parent 5b31ba9 commit 14195bf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/ex_teal/fields/embeds_many.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ defmodule ExTeal.Fields.EmbedsMany do
end

@impl true
def apply_options_for(field, _model, conn, type) do
def apply_options_for(field, model, conn, type) do
value =
field
|> Map.get(:value, [])
|> value_for(model, type)
|> Enum.map(fn related ->
Enum.map(related, fn value_field ->
value_field.type.apply_options_for(value_field, related, conn, type)
Expand Down
32 changes: 31 additions & 1 deletion test/ex_teal/fields/embeds_many_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule ExTeal.Fields.EmbedsManyTest do
use ExUnit.Case
use TestExTeal.ConnCase
alias TestExTeal.Song
alias ExTeal.Fields.{Boolean, EmbedsMany, Number, Text}

describe "value_for/3" do
Expand Down Expand Up @@ -63,4 +64,33 @@ defmodule ExTeal.Fields.EmbedsManyTest do
assert Enum.map(r2, & &1.value) == [2, "bar", []]
end
end

describe "apply_options_for/4" do
test "can handle a new struct", %{conn: conn} do
field =
EmbedsMany.make(:musicians)
|> EmbedsMany.fields([
Text.make(:name),
Text.make(:instrument)
])

updated_field = EmbedsMany.apply_options_for(field, %Song{}, conn, :new)
assert updated_field.value == []
end

test "can handle update fields", %{conn: conn} do
field =
EmbedsMany.make(:musicians)
|> EmbedsMany.fields([
Text.make(:name),
Text.make(:instrument)
])

s = insert(:song)
updated_field = EmbedsMany.apply_options_for(field, s, conn, :new)
assert length(updated_field.value) == 4
[musician | _others] = updated_field.value
assert Enum.map(musician, & &1.field) == ~w(id name instrument)a
end
end
end
14 changes: 13 additions & 1 deletion test/support/factories.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule TestExTeal.Factory do

use ExMachina.Ecto, repo: TestExTeal.Repo

alias TestExTeal.{Like, Order, Post, PreferredTag, SinglePostUser, Tag, User}
alias TestExTeal.{Like, Order, Post, PreferredTag, SinglePostUser, Song, Tag, User}

def user_factory do
%User{
Expand Down Expand Up @@ -56,4 +56,16 @@ defmodule TestExTeal.Factory do
identifier: sequence(:identifier, &"#{&1}")
}
end

def song_factory do
%Song{
name: sequence(:name, &"Song #{&1}"),
musicians: [
%{name: "John", instrument: "guitar"},
%{name: "Paul", instrument: "bass"},
%{name: "George", instrument: "guitar"},
%{name: "Ringo", instrument: "drums"}
]
}
end
end
2 changes: 2 additions & 0 deletions test/support/schema.exs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ defmodule TestExTeal.Song do
field(:name, :string)
field(:instrument, :string)
end

timestamps()
end

def changeset(%Song{} = song, params \\ %{}) do
Expand Down

0 comments on commit 14195bf

Please sign in to comment.