Skip to content

Commit

Permalink
test for tracking assocs - just works!
Browse files Browse the repository at this point in the history
custom data from the pid ets will now be pulled directly in the callbacks
so ecto will pass us the :ex_audit_custom opt in deeper recursion levels
  • Loading branch information
narrowtux committed Oct 19, 2017
1 parent bf4415a commit fece15b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion example/comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions example/util.ex
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions lib/repo/schema.ex
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -49,25 +54,37 @@ 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
end

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
6 changes: 1 addition & 5 deletions lib/tracking/tracking.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions test/assoc_test.exs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fece15b

Please sign in to comment.