Skip to content

Commit

Permalink
making library work with any Elixir project
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Jul 15, 2016
1 parent 95a0ec7 commit ddef880
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
20 changes: 10 additions & 10 deletions lib/paper_trail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ defmodule PaperTrail do
"""
def get_versions(model, id) do
item_type = model |> Module.split |> List.last
version_query(item_type, id) |> Application.Repo.all
version_query(item_type, id) |> Repo.all
end

@doc """
Gets all the versions of a record
"""
def get_versions(record) do
item_type = record.__struct__ |> Module.split |> List.last
version_query(item_type, record.id) |> Application.Repo.all
version_query(item_type, record.id) |> Repo.all
end

@doc """
Gets the last version of a record given its module reference and its id
"""
def get_version(model, id) do
item_type = Module.split(model) |> List.last
last(version_query(item_type, id)) |> Application.Repo.one
last(version_query(item_type, id)) |> Repo.one
end

@doc """
Gets the last version of a record
"""
def get_version(record) do
item_type = record.__struct__ |> Module.split |> List.last
last(version_query(item_type, record.id)) |> Application.Repo.one
last(version_query(item_type, record.id)) |> Repo.one
end

defp version_query(item_type, id) do
Expand All @@ -50,9 +50,9 @@ defmodule PaperTrail do
|> Multi.insert(:model, struct)
|> Multi.run(:version, fn %{model: model} ->
version = make_version_struct(%{event: "create"}, model, meta)
Application.Repo.insert(version)
Repo.insert(version)
end)
|> Application.Repo.transaction
|> Repo.transaction
end

# might make the changeset version
Expand All @@ -65,9 +65,9 @@ defmodule PaperTrail do
|> Multi.update(:model, changeset)
|> Multi.run(:version, fn %{model: model} ->
version = make_version_struct(%{event: "update"}, changeset, meta)
Application.Repo.insert(version)
Repo.insert(version)
end)
|> Application.Repo.transaction
|> Repo.transaction
end

@doc """
Expand All @@ -78,9 +78,9 @@ defmodule PaperTrail do
|> Multi.delete(:model, struct)
|> Multi.run(:version, fn %{model: model} ->
version = make_version_struct(%{event: "destroy"}, model, meta)
Application.Repo.insert(version)
Repo.insert(version)
end)
|> Application.Repo.transaction
|> Repo.transaction
end

defp make_version_struct(%{event: "create"}, model, meta) do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule PaperTrail.Mixfile do

def project do
[app: :paper_trail,
version: "0.0.4",
version: "0.0.5",
elixir: "~> 1.3",
description: description,
build_embedded: Mix.env == :prod,
Expand Down

0 comments on commit ddef880

Please sign in to comment.