Skip to content

Commit

Permalink
fixed for non-regular primary key with test
Browse files Browse the repository at this point in the history
  • Loading branch information
devvit authored and devvit committed Feb 16, 2018
1 parent a0f5dbf commit 2ced4a2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
6 changes: 1 addition & 5 deletions lib/paper_trail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ defmodule PaperTrail do
%Version{
event: "update",
item_type: changeset.data.__struct__ |> Module.split() |> List.last(),
item_id: get_model_id_from_changeset(changeset),
item_id: get_model_id(changeset.data),
item_changes: changeset.changes,
originator_id:
case originator_ref do
Expand Down Expand Up @@ -382,8 +382,4 @@ defmodule PaperTrail do
def get_model_id(model) do
Map.get(model, List.first(model.__struct__.__schema__(:primary_key)))
end

def get_model_id_from_changeset(changeset) do
get_model_id(changeset.data)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,12 @@ defmodule PaperTrail.UUIDRepo.Migrations.CreateUuidProducts do

timestamps()
end

create table(:items, primary_key: false) do
add :item_id, :binary_id, primary_key: true
add :title, :string, null: false

timestamps()
end
end
end
11 changes: 11 additions & 0 deletions test/paper_trail/uuid_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule PaperTrailTest.UUIDTest do
repo().delete_all(Version)
repo().delete_all(Admin)
repo().delete_all(Product)
repo().delete_all(Item)
:ok
end

Expand Down Expand Up @@ -47,4 +48,14 @@ defmodule PaperTrailTest.UUIDTest do

assert version.admin == admin
end

test "versioning models that have a non-regular primary key" do
item =
%Item{}
|> Item.changeset(%{title: "hello"})
|> PaperTrail.insert!()

version = Version |> last |> repo().one
assert version.item_id == item.item_id
end
end
18 changes: 18 additions & 0 deletions test/support/uuid_models.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,21 @@ defmodule Admin do
|> validate_required([:email])
end
end

defmodule Item do
use Ecto.Schema
import Ecto.Changeset

@primary_key {:item_id, :binary_id, autogenerate: true}
schema "items" do
field(:title, :string)

timestamps()
end

def changeset(model, params \\ %{}) do
model
|> cast(params, [:title])
|> validate_required(:title)
end
end

0 comments on commit 2ced4a2

Please sign in to comment.