Skip to content

Commit

Permalink
fix for setting embed to nil (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen committed Jan 30, 2021
1 parent fd9463d commit 99fa948
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/paper_trail/serializer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ defmodule PaperTrail.Serializer do
end
end

@spec serialize_model_changes(nil) :: nil
defp serialize_model_changes(nil), do: nil

@spec serialize_model_changes(Ecto.Changeset.t()) :: map()
defp serialize_model_changes(%Ecto.Changeset{data: %schema{}} = changeset) do
field_values = serialize_model_field_changes(changeset)
Expand Down
25 changes: 25 additions & 0 deletions test/paper_trail/base_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,31 @@ defmodule PaperTrailTest do
assert old_person == person_before_deletion
end

test "works with nil embed" do
{:ok, target_company_insertion} =
create_company_with_version(%{
name: "Another Company Corp.",
is_active: true,
address: "Sesame street 100/3, 101010"
})

{:ok, insert_person_result} =
Person.changeset(%Person{}, %{
first_name: "Izel",
last_name: "Nakri",
gender: true,
company_id: target_company_insertion[:model].id,
singular: %{}
})
|> PaperTrail.insert(origin: "admin")

assert {:ok, insert_person_result} =
Person.changeset(insert_person_result[:model], %{
singular: nil
})
|> PaperTrail.update(origin: "admin")
end

defp create_user do
User.changeset(%User{}, %{token: "fake-token", username: "izelnakri"}) |> @repo.insert!
end
Expand Down
2 changes: 1 addition & 1 deletion test/support/simple_models.exs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ defmodule SimplePerson do

belongs_to(:company, SimpleCompany, foreign_key: :company_id)

embeds_one(:singular, SimpleEmbed)
embeds_one(:singular, SimpleEmbed, on_replace: :update)
embeds_many(:plural, SimpleEmbed)

timestamps()
Expand Down

0 comments on commit 99fa948

Please sign in to comment.