diff --git a/README.md b/README.md index 8a57136..250bda8 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ defmodule MyApp.Version do field :entity_id, :integer # name of the table the entity is in - field :entity_schema, :string + field :entity_schema, ExAudit.Type.Schema # type of the action that has happened to the entity (created, updated, deleted) field :action, ExAudit.Type.Action diff --git a/example/comment.ex b/example/comment.ex index f5d6ccb..c8312e9 100644 --- a/example/comment.ex +++ b/example/comment.ex @@ -2,7 +2,7 @@ defmodule ExAudit.Test.Comment do use Ecto.Schema import Ecto.Changeset - schema "blog_post" do + schema "comments" do belongs_to :author, ExAudit.Test.User field :body, :string diff --git a/example/util.ex b/example/util.ex new file mode 100644 index 0000000..623f0fa --- /dev/null +++ b/example/util.ex @@ -0,0 +1,9 @@ +defmodule ExAudit.Test.Util do + alias ExAudit.Test.{Repo, User} + + def create_user(name \\ "Admin", email \\ "admin@example.com") do + params = %{name: name, email: email} + changeset = User.changeset(%User{}, params) + Repo.insert!(changeset) + end +end \ No newline at end of file diff --git a/lib/repo/schema.ex b/lib/repo/schema.ex index 9399929..acaec8a 100644 --- a/lib/repo/schema.ex +++ b/lib/repo/schema.ex @@ -1,10 +1,12 @@ defmodule ExAudit.Schema do def insert_all(module, adapter, schema_or_source, entries, opts) do # TODO! + opts = augment_opts(opts) Ecto.Repo.Schema.insert_all(module, adapter, schema_or_source, entries, opts) end def insert(module, adapter, struct, opts) do + opts = augment_opts(opts) result = Ecto.Repo.Schema.insert(module, adapter, struct, opts) case result do @@ -18,6 +20,7 @@ defmodule ExAudit.Schema do end def update(module, adapter, struct, opts) do + opts = augment_opts(opts) result = Ecto.Repo.Schema.update(module, adapter, struct, opts) case result do @@ -32,10 +35,12 @@ defmodule ExAudit.Schema do def insert_or_update(module, adapter, changeset, opts) do # TODO! + opts = augment_opts(opts) Ecto.Repo.Schema.insert_or_update(module, adapter, changeset, opts) end def delete(module, adapter, struct, opts) do + opts = augment_opts(opts) result = Ecto.Repo.Schema.delete(module, adapter, struct, opts) case result do @@ -49,12 +54,14 @@ defmodule ExAudit.Schema do end def insert!(module, adapter, struct, opts) do + opts = augment_opts(opts) result = Ecto.Repo.Schema.insert!(module, adapter, struct, opts) ExAudit.Tracking.track_change(module, adapter, :created, struct, result, opts) result end def update!(module, adapter, struct, opts) do + opts = augment_opts(opts) result = Ecto.Repo.Schema.update!(module, adapter, struct, opts) ExAudit.Tracking.track_change(module, adapter, :updated, struct, result, opts) result @@ -62,12 +69,22 @@ defmodule ExAudit.Schema do def insert_or_update!(module, adapter, changeset, opts) do # TODO + opts = augment_opts(opts) Ecto.Repo.Schema.insert_or_update!(module, adapter, changeset, opts) end def delete!(module, adapter, struct, opts) do + opts = augment_opts(opts) result = Ecto.Repo.Schema.delete!(module, adapter, struct, opts) ExAudit.Tracking.track_change(module, adapter, :deleted, struct, result, opts) result end + + defp augment_opts(opts) do + opts + |> Keyword.put_new(:ex_audit_custom, []) + |> Keyword.update(:ex_audit_custom, [], fn custom_fields -> + ExAudit.CustomData.get() ++ custom_fields + end) + end end \ No newline at end of file diff --git a/lib/tracking/tracking.ex b/lib/tracking/tracking.ex index b6b9ba5..b94cdb2 100644 --- a/lib/tracking/tracking.ex +++ b/lib/tracking/tracking.ex @@ -48,13 +48,9 @@ defmodule ExAudit.Tracking do changes = find_changes(action, changeset, resulting_struct) now = DateTime.utc_now - custom_fields_opts = + custom_fields = Keyword.get(opts, :ex_audit_custom, []) |> Enum.into(%{}) - custom_fields_process = - ExAudit.CustomData.get() - |> Enum.into(%{}) - custom_fields = Map.merge(custom_fields_process, custom_fields_opts) changes = Enum.map(changes, fn change -> change = Map.put(change, :recorded_at, now) diff --git a/test/assoc_test.exs b/test/assoc_test.exs new file mode 100644 index 0000000..5ba8539 --- /dev/null +++ b/test/assoc_test.exs @@ -0,0 +1,28 @@ +defmodule AssocTest do + use ExUnit.Case + + import Ecto.Query + + alias ExAudit.Test.{Repo, User, Version, BlogPost, Comment, Util} + + test "comment lifecycle tracked" do + user = Util.create_user() + + params = %{ + title: "Controversial post", + author_id: user.id, + comments: [ + %{ + body: "lorem impusdrfnia", + author_id: user.id + } + ] + } + + changeset = BlogPost.changeset(%BlogPost{}, params) + {:ok, %{comments: [comment]} = blog_post} = Repo.insert(changeset) + + comment_history = Repo.history(comment) + assert length(comment_history) == 1 + end +end \ No newline at end of file