Skip to content

Commit

Permalink
PaperTrail.insert passes now fully with model relationships, it is al…
Browse files Browse the repository at this point in the history
…so tested
  • Loading branch information
izelnakri committed Jul 16, 2016
1 parent 5033788 commit 5218fac
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion example/mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"db_connection": {:hex, :db_connection, "1.0.0-rc.3", "d9ceb670fe300271140af46d357b669983cd16bc0d01206d7d3222dde56cf038", [:mix], [{:sbroker, "~> 1.0.0-beta.3", [hex: :sbroker, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:connection, "~> 1.0.2", [hex: :connection, optional: false]}]},
"decimal": {:hex, :decimal, "1.1.2", "79a769d4657b2d537b51ef3c02d29ab7141d2b486b516c109642d453ee08e00c", [:mix], []},
"ecto": {:hex, :ecto, "2.0.2", "b02331c1f20bbe944dbd33c8ecd8f1ccffecc02e344c4471a891baf3a25f5406", [:mix], [{:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: true]}, {:sbroker, "~> 1.0-beta", [hex: :sbroker, optional: true]}, {:mariaex, "~> 0.7.7", [hex: :mariaex, optional: true]}, {:postgrex, "~> 0.11.2", [hex: :postgrex, optional: true]}, {:db_connection, "~> 1.0-rc.2", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}]},
"paper_trail": {:hex, :paper_trail, "0.0.8", "bcba8e0214da58d4fccbe73307939e4152c337684ed35c5a782ba0be81cc3354", [:mix], [{:poison, "2.1.0", [hex: :poison, optional: false]}, {:ecto, "~> 2.0.2", [hex: :ecto, optional: false]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, optional: false]}]},
"paper_trail": {:hex, :paper_trail, "0.0.8", "9be51d594ebdc1a228ad3f3b5cbafcca5d5e8e8661eb44319fd011c510ef2628", [:mix], [{:poison, "2.1.0", [hex: :poison, optional: false]}, {:ecto, "~> 2.0.2", [hex: :ecto, optional: false]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, optional: false]}]},
"poison": {:hex, :poison, "2.1.0", "f583218ced822675e484648fa26c933d621373f01c6c76bd00005d7bd4b82e27", [:mix], []},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []},
"postgrex": {:hex, :postgrex, "0.11.2", "139755c1359d3c5c6d6e8b1ea72556d39e2746f61c6ddfb442813c91f53487e8", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 1.0-rc", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}}
3 changes: 2 additions & 1 deletion example/test/company_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule CompanyTest do
# maybe test meta tag insertion and relationships

setup_all do
Repo.delete_all(Person)
Repo.delete_all(Company)
Repo.delete_all(PaperTrail.Version)
:ok
Expand Down Expand Up @@ -52,7 +53,7 @@ defmodule CompanyTest do
event: "create",
item_type: "Company",
item_id: Repo.one(first(Company, :id)).id,
item_changes: Map.drop(result[:model], [:__meta__, :__struct__]),
item_changes: Map.drop(result[:model], [:__meta__, :__struct__, :people]),
meta: nil
}
end
Expand Down
10 changes: 6 additions & 4 deletions example/test/person_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule PersonTest do
doctest Person

setup_all do
Repo.delete_all(Person)
Repo.delete_all(Company)
Repo.delete_all(PaperTrail.Version)

Expand Down Expand Up @@ -51,20 +52,21 @@ defmodule PersonTest do
assert person_count == [1]
assert version_count == [1]

assert person == %{
assert Map.drop(person, [:company]) == %{
first_name: "Izel",
last_name: "Nakri",
gender: true,
visit_count: nil,
birthdate: nil
birthdate: nil,
company_id: company.id
}

assert Map.drop(version, [:id]) == %{
event: "create",
item_type: "Person",
item_id: Repo.one(first(Person, :id)).id,
item_changes: Map.drop(result[:model], [:__meta__, :__struct__]),
meta: nil
item_changes: Map.drop(result[:model], [:__meta__, :__struct__, :company]),
meta: %{originator: "admin"}
}
end

Expand Down
22 changes: 11 additions & 11 deletions lib/paper_trail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ defmodule PaperTrail do
|> Repo.transaction
end

defp make_version_struct(%{event: "update"}, changeset, meta) do
%Version{
event: "update",
item_type: changeset.data.__struct__ |> Module.split |> List.last,
item_id: changeset.data.id,
item_changes: changeset.changes,
meta: meta
}
end

@doc """
Deletes a record from the database with a related version insertion in one transaction
"""
Expand All @@ -101,22 +111,12 @@ defmodule PaperTrail do
|> Repo.transaction
end

defp make_version_struct(%{event: "update"}, changeset, meta) do
%Version{
event: "update",
item_type: changeset.data.__struct__ |> Module.split |> List.last,
item_id: changeset.data.id,
item_changes: changeset.changes,
meta: meta
}
end

defp make_version_struct(%{event: "destroy"}, model, meta) do
%Version{
event: "destroy",
item_type: model.__struct__ |> Module.split |> List.last,
item_id: model.id,
item_changes: Map.drop(model, [:__struct__, :__meta__]),
item_changes: filter_item_changes(model),
meta: meta
}
end
Expand Down

0 comments on commit 5218fac

Please sign in to comment.