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

Save associations changes in items_changes #30

Closed
wants to merge 6 commits into from
Closed
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
40 changes: 39 additions & 1 deletion lib/paper_trail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ defmodule PaperTrail do
event: "update",
item_type: changeset.data.__struct__ |> Module.split |> List.last,
item_id: changeset.data.id,
item_changes: changeset.changes,
item_changes: serialize_changes(changeset) || %{},
originator_id: case originator_ref do
nil -> nil
_ -> originator_ref |> Map.get(:id)
Expand Down Expand Up @@ -298,6 +298,44 @@ defmodule PaperTrail do
Map.drop(model, [:__struct__, :__meta__] ++ relationships)
end

defp serialize_changes(data), do: serialize_changes(data, true)
defp serialize_changes(data, is_root) do
case is_map(data) do
false -> data
true -> deep_serialize(data, is_root)
end
end

defp deep_serialize(data, is_root) do
if Map.has_key?(data, :__struct__) do
case data.__struct__ do
Ecto.Changeset -> extract_values(data, is_root)
_ -> data
end
else
data
end
end

defp extract_values(changeset, is_root) do
if changeset.changes != %{} do
init = initial_value(changeset, is_root)
changeset.changes
|> Enum.reduce(init, fn({key, value}, accum) ->
value = if is_list(value) do
Enum.map(value, &serialize_changes(&1, false))
|> Enum.filter(fn(v) -> ! is_nil(v) end)
else
serialize_changes(value, false)
end
Map.put(accum, key, value)
end)
end
end

defp initial_value(_, true), do: %{}
defp initial_value(changeset, false), do: %{id: changeset.data.id}

defp add_prefix(changeset, nil), do: changeset
defp add_prefix(changeset, prefix), do: Ecto.put_meta(changeset, prefix: prefix)
end
44 changes: 44 additions & 0 deletions test/paper_trail_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule PaperTrailTest do
@repo PaperTrail.RepoClient.repo
@create_company_params %{name: "Acme LLC", is_active: true, city: "Greenwich"}
@update_company_params %{city: "Hong Kong", website: "http://www.acme.com", facebook: "acme.llc"}
@create_company_with_people_params %{name: "Acme LLC", is_active: true, city: "Greenwich", people: [%{first_name: "Izel"}]}

doctest PaperTrail

Expand Down Expand Up @@ -326,6 +327,45 @@ defmodule PaperTrailTest do
assert person == first(Person, :id) |> @repo.one |> serialize
end

test "updating a company with people changes creates a version with correct attributes" do
{:ok, %{model: company}} = create_company_with_people_with_version()
person_id = List.first(company.people).id
{:ok, result} = Company.people_changeset(company, %{
name: "Another Company",
people: [%{id: person_id, first_name: "abmm"}]
}) |> PaperTrail.update
company_count = Person.count()
version_count = Version.count()
company = result[:model] |> serialize
version = result[:version] |> serialize
assert Map.keys(result) == [:model, :version]
assert company_count == 1
assert version_count == 2
assert Map.drop(company, [:id, :inserted_at, :updated_at]) == %{
name: "Another Company",
address: nil,
city: "Greenwich",
twitter: nil,
facebook: nil,
founded_in: nil,
is_active: true,
website: nil
}
assert Map.drop(version, [:id, :inserted_at]) == %{
event: "update",
item_type: "SimpleCompany",
item_id: company.id,
item_changes: %{
name: "Another Company",
people: [%{ first_name: "abmm", id: person_id}]
},
originator_id: nil,
origin: nil,
meta: nil
}
assert company == first(Company, :id) |> @repo.one |> serialize
end

test "deleting a person creates a person version with correct attributes" do
create_company_with_version(%{name: "Acme LLC", website: "http://www.acme.com"})
{:ok, target_company_insertion} = create_company_with_version(%{
Expand Down Expand Up @@ -391,6 +431,10 @@ defmodule PaperTrailTest do
Company.changeset(company, params) |> PaperTrail.update(options)
end

defp create_company_with_people_with_version(params \\ @create_company_with_people_params, options \\ nil) do
Company.people_changeset(%Company{}, params) |> PaperTrail.insert(options)
end

defp serialize(model) do
relationships = model.__struct__.__schema__(:associations)
Map.drop(model, [:__struct__, :__meta__] ++ relationships)
Expand Down
7 changes: 7 additions & 0 deletions test/support/simple_models.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ defmodule SimpleCompany do
|> no_assoc_constraint(:people)
end

def people_changeset(model, params \\ %{}) do
model
|> cast(params, @optional_fields)
|> cast_assoc(:people)
|> validate_required([:name])
end

def count do
from(record in __MODULE__, select: count(record.id)) |> PaperTrail.RepoClient.repo.one
end
Expand Down