Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix for setting embed to nil #129

Merged
merged 1 commit into from
Jan 30, 2021
Merged
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
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