Skip to content

Commit

Permalink
Merge pull request #40 from devvit/master
Browse files Browse the repository at this point in the history
fixed for non-regular primary key
  • Loading branch information
izelnakri authored Feb 17, 2018
2 parents ea407dc + 2ced4a2 commit 794bace
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
10 changes: 7 additions & 3 deletions lib/paper_trail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ defmodule PaperTrail do
%Version{
event: "insert",
item_type: model.__struct__ |> Module.split() |> List.last(),
item_id: model.id,
item_id: get_model_id(model),
item_changes: serialize(model),
originator_id:
case originator_ref do
Expand All @@ -322,7 +322,7 @@ defmodule PaperTrail do
%Version{
event: "update",
item_type: changeset.data.__struct__ |> Module.split() |> List.last(),
item_id: changeset.data.id,
item_id: get_model_id(changeset.data),
item_changes: changeset.changes,
originator_id:
case originator_ref do
Expand All @@ -342,7 +342,7 @@ defmodule PaperTrail do
%Version{
event: "delete",
item_type: model.__struct__ |> Module.split() |> List.last(),
item_id: model.id,
item_id: get_model_id(model),
item_changes: serialize(model),
originator_id:
case originator_ref do
Expand Down Expand Up @@ -378,4 +378,8 @@ defmodule PaperTrail do

defp add_prefix(changeset, nil), do: changeset
defp add_prefix(changeset, prefix), do: Ecto.put_meta(changeset, prefix: prefix)

def get_model_id(model) do
Map.get(model, List.first(model.__struct__.__schema__(:primary_key)))
end
end
4 changes: 2 additions & 2 deletions lib/paper_trail/version_queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule PaperTrail.VersionQueries do
@spec get_versions(record :: Ecto.Schema.t(), options :: []) :: Ecto.Query.t()
def get_versions(record, options) when is_map(record) do
item_type = record.__struct__ |> Module.split() |> List.last()
version_query(item_type, record.id, options) |> @repo.all
version_query(item_type, PaperTrail.get_model_id(record), options) |> @repo.all
end

@doc """
Expand Down Expand Up @@ -75,7 +75,7 @@ defmodule PaperTrail.VersionQueries do
@spec get_version(record :: Ecto.Schema.t(), options :: []) :: Ecto.Query.t()
def get_version(record, options) when is_map(record) do
item_type = record.__struct__ |> Module.split() |> List.last()
last(version_query(item_type, record.id, options)) |> @repo.one
last(version_query(item_type, PaperTrail.get_model_id(record), options)) |> @repo.one
end

@doc """
Expand Down
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 794bace

Please sign in to comment.