Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom originator params #12

Merged
merged 4 commits into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ The library source code is minimal and well tested. It is suggested to read the

```elixir
def deps do
[{:paper_trail, "~> 0.6.0"}]
[{:paper_trail, "~> 0.7.0"}]
end
```

Expand All @@ -116,7 +116,7 @@ The library source code is minimal and well tested. It is suggested to read the

3. install and compile your dependency:

```mix deps.get && mix deps.compile```
```mix deps.get && mix compile```

4. run this command to generate the migration:

Expand Down Expand Up @@ -160,22 +160,26 @@ PaperTrail.insert(new_user_changeset, origin: "facebook_registration")
```

### Originator relationships
You can specify setter/originator relationship to paper_trail versions with ```originator_id``` assignment. This feature is only possible by specifying `:originator` keyword list for your application configuration:
You can specify setter/originator relationship to paper_trail versions with ```originator``` assignment. This feature is only possible by specifying `:originator` keyword list for your application configuration:

```elixir
# in your config/config.exs
config :paper_trail, originator: [name: :user, model: YourApp.User]
# For most applications originator should be the user since models can be updated/created/deleted by several users.
```

Then originator name could be used for querying and preloading however originator setting must be done via originator_id:
Then originator name could be used for querying and preloading. Originator setting must be done via ```:originator``` or originator name that is defined in the paper_trail configuration:

```elixir
user = create_user()
PaperTrail.insert(changeset, originator_id: user.id)
{:ok, result} = PaperTrail.update(edit_changeset, originator_id: user.id)
# all these set originator_id's for the version records
PaperTrail.insert(changeset, originator: user)
{:ok, result} = PaperTrail.update(edit_changeset, originator: user)
# or you can use :user in the params instead of :originator if this is your config:
# paper_trail originator: [name: :user, model: YourApplication.User]
{:ok, result} = PaperTrail.update(edit_changeset, user: user)
result[:version] |> Repo.preload(:user) |> Map.get(:user) # we can access the user who made the change from the version thanks to originator relationships!
PaperTrail.delete(edit_changeset, originator_id: user.id)
PaperTrail.delete(edit_changeset, user: user)
```

Also make sure you have the foreign-key constraint in the database and in your version migration file.
Expand Down Expand Up @@ -263,7 +267,7 @@ edited_company = Company.changeset(company, %{name: "Acme Inc."}) |> PaperTrail.
Additionally, you can put a null constraint on ```origin``` column, you should always put an ```origin``` reference to describe who makes the change. This is important for big applications because a model can change from many sources.

### Storing version meta data
You might want to add some meta data that doesn't belong to ``originator_id`` and ``origin`` fields. Such data could be stored in one object named ```meta``` in paper_trail versions. Meta field could be passed as the second optional parameter to PaperTrail.insert/2, PaperTrail.update/2, PaperTrail.delete/2 functions:
You might want to add some meta data that doesn't belong to ``originator`` and ``origin`` fields. Such data could be stored in one object named ```meta``` in paper_trail versions. Meta field could be passed as the second optional parameter to PaperTrail.insert/2, PaperTrail.update/2, PaperTrail.delete/2 functions:

```elixir
company = Company.changeset(%Company{}, %{name: "Acme Inc."})
Expand All @@ -274,7 +278,7 @@ edited_company = Company.changeset(company, %{name: "Acme LLC"})
# or even with an originator:
user = create_user()
deleted_company = Company.changeset(edited_company, %{})
|> PaperTrail.delete(origin: "worker:github", originator: user.id, meta: %{slug: "acme-llc", important: true})
|> PaperTrail.delete(origin: "worker:github", originator: user, meta: %{slug: "acme-llc", important: true})
```

## Suggestions
Expand Down
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use Mix.Config

config :paper_trail, ecto_repos: [PaperTrail.Repo]

config :paper_trail, repo: PaperTrail.Repo
config :paper_trail, repo: PaperTrail.Repo, originator: [name: :user, model: User]

config :paper_trail, PaperTrail.Repo,
adapter: Ecto.Adapters.Postgres,
Expand Down
4 changes: 2 additions & 2 deletions doc/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="ExDoc v0.14.5">
<title>404 – paper_trail v0.6.3</title>
<title>404 – paper_trail v0.7.0</title>
<link rel="stylesheet" href="dist/app-a07cea761b.css" />

<link rel="canonical" href="https://hexdocs.pm/paper_trail/404.html" />
Expand All @@ -28,7 +28,7 @@ <h1 class="sidebar-projectName">
paper_trail
</h1>
<h2 class="sidebar-projectVersion">
v0.6.3
v0.7.0
</h2>
</div>

Expand Down
6 changes: 3 additions & 3 deletions doc/Mix.Tasks.Papertrail.Install.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="ExDoc v0.14.5">
<title>Mix.Tasks.Papertrail.Install – paper_trail v0.6.3</title>
<title>Mix.Tasks.Papertrail.Install – paper_trail v0.7.0</title>
<link rel="stylesheet" href="dist/app-a07cea761b.css" />

<link rel="canonical" href="https://hexdocs.pm/paper_trail/Mix.Tasks.Papertrail.Install.html" />
Expand All @@ -28,7 +28,7 @@ <h1 class="sidebar-projectName">
paper_trail
</h1>
<h2 class="sidebar-projectVersion">
v0.6.3
v0.7.0
</h2>
</div>

Expand Down Expand Up @@ -60,7 +60,7 @@ <h2 class="sidebar-projectVersion">


<h1>
<small class="visible-xs">paper_trail v0.6.3</small>
<small class="visible-xs">paper_trail v0.7.0</small>
Mix.Tasks.Papertrail.Install


Expand Down
6 changes: 3 additions & 3 deletions doc/PaperTrail.Migration.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="ExDoc v0.14.5">
<title>PaperTrail.Migration – paper_trail v0.6.3</title>
<title>PaperTrail.Migration – paper_trail v0.7.0</title>
<link rel="stylesheet" href="dist/app-a07cea761b.css" />

<link rel="canonical" href="https://hexdocs.pm/paper_trail/PaperTrail.Migration.html" />
Expand All @@ -28,7 +28,7 @@ <h1 class="sidebar-projectName">
paper_trail
</h1>
<h2 class="sidebar-projectVersion">
v0.6.3
v0.7.0
</h2>
</div>

Expand Down Expand Up @@ -60,7 +60,7 @@ <h2 class="sidebar-projectVersion">


<h1>
<small class="visible-xs">paper_trail v0.6.3</small>
<small class="visible-xs">paper_trail v0.7.0</small>
PaperTrail.Migration


Expand Down
6 changes: 3 additions & 3 deletions doc/PaperTrail.RepoClient.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="ExDoc v0.14.5">
<title>PaperTrail.RepoClient – paper_trail v0.6.3</title>
<title>PaperTrail.RepoClient – paper_trail v0.7.0</title>
<link rel="stylesheet" href="dist/app-a07cea761b.css" />

<link rel="canonical" href="https://hexdocs.pm/paper_trail/PaperTrail.RepoClient.html" />
Expand All @@ -28,7 +28,7 @@ <h1 class="sidebar-projectName">
paper_trail
</h1>
<h2 class="sidebar-projectVersion">
v0.6.3
v0.7.0
</h2>
</div>

Expand Down Expand Up @@ -60,7 +60,7 @@ <h2 class="sidebar-projectVersion">


<h1>
<small class="visible-xs">paper_trail v0.6.3</small>
<small class="visible-xs">paper_trail v0.7.0</small>
PaperTrail.RepoClient


Expand Down
6 changes: 3 additions & 3 deletions doc/PaperTrail.Version.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="ExDoc v0.14.5">
<title>PaperTrail.Version – paper_trail v0.6.3</title>
<title>PaperTrail.Version – paper_trail v0.7.0</title>
<link rel="stylesheet" href="dist/app-a07cea761b.css" />

<link rel="canonical" href="https://hexdocs.pm/paper_trail/PaperTrail.Version.html" />
Expand All @@ -28,7 +28,7 @@ <h1 class="sidebar-projectName">
paper_trail
</h1>
<h2 class="sidebar-projectVersion">
v0.6.3
v0.7.0
</h2>
</div>

Expand Down Expand Up @@ -60,7 +60,7 @@ <h2 class="sidebar-projectVersion">


<h1>
<small class="visible-xs">paper_trail v0.6.3</small>
<small class="visible-xs">paper_trail v0.7.0</small>
PaperTrail.Version


Expand Down
6 changes: 3 additions & 3 deletions doc/PaperTrail.VersionQueries.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="ExDoc v0.14.5">
<title>PaperTrail.VersionQueries – paper_trail v0.6.3</title>
<title>PaperTrail.VersionQueries – paper_trail v0.7.0</title>
<link rel="stylesheet" href="dist/app-a07cea761b.css" />

<link rel="canonical" href="https://hexdocs.pm/paper_trail/PaperTrail.VersionQueries.html" />
Expand All @@ -28,7 +28,7 @@ <h1 class="sidebar-projectName">
paper_trail
</h1>
<h2 class="sidebar-projectVersion">
v0.6.3
v0.7.0
</h2>
</div>

Expand Down Expand Up @@ -60,7 +60,7 @@ <h2 class="sidebar-projectVersion">


<h1>
<small class="visible-xs">paper_trail v0.6.3</small>
<small class="visible-xs">paper_trail v0.7.0</small>
PaperTrail.VersionQueries


Expand Down
18 changes: 9 additions & 9 deletions doc/PaperTrail.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="ExDoc v0.14.5">
<title>PaperTrail – paper_trail v0.6.3</title>
<title>PaperTrail – paper_trail v0.7.0</title>
<link rel="stylesheet" href="dist/app-a07cea761b.css" />

<link rel="canonical" href="https://hexdocs.pm/paper_trail/PaperTrail.html" />
Expand All @@ -28,7 +28,7 @@ <h1 class="sidebar-projectName">
paper_trail
</h1>
<h2 class="sidebar-projectVersion">
v0.6.3
v0.7.0
</h2>
</div>

Expand Down Expand Up @@ -60,7 +60,7 @@ <h2 class="sidebar-projectVersion">


<h1>
<small class="visible-xs">paper_trail v0.6.3</small>
<small class="visible-xs">paper_trail v0.7.0</small>
PaperTrail


Expand All @@ -85,7 +85,7 @@ <h2>
</h2>
<div class="summary-row">
<div class="summary-signature">
<a href="#delete/2">delete(struct, options \\ [origin: nil, meta: nil, originator_id: nil])</a>
<a href="#delete/2">delete(struct, options \\ [origin: nil, meta: nil, originator: nil])</a>
</div>

<div class="summary-synopsis"><p>Deletes a record from the database with a related version insertion in one transaction</p>
Expand Down Expand Up @@ -139,7 +139,7 @@ <h2>
</div>
<div class="summary-row">
<div class="summary-signature">
<a href="#insert/2">insert(changeset, options \\ [origin: nil, meta: nil, originator_id: nil])</a>
<a href="#insert/2">insert(changeset, options \\ [origin: nil, meta: nil, originator: nil])</a>
</div>

<div class="summary-synopsis"><p>Inserts a record to the database with a related version insertion in one transaction</p>
Expand All @@ -148,7 +148,7 @@ <h2>
</div>
<div class="summary-row">
<div class="summary-signature">
<a href="#update/2">update(changeset, options \\ [origin: nil, meta: nil, originator_id: nil])</a>
<a href="#update/2">update(changeset, options \\ [origin: nil, meta: nil, originator: nil])</a>
</div>

<div class="summary-synopsis"><p>Updates a record from the database with a related version insertion in one transaction</p>
Expand Down Expand Up @@ -184,7 +184,7 @@ <h1 class="section-heading">
<a href="#delete/2" class="detail-link" title="Link to this function">
<i class="icon-link"></i>
</a>
<span class="signature">delete(struct, options \\ [origin: nil, meta: nil, originator_id: nil])</span>
<span class="signature">delete(struct, options \\ [origin: nil, meta: nil, originator: nil])</span>



Expand Down Expand Up @@ -282,7 +282,7 @@ <h1 class="section-heading">
<a href="#insert/2" class="detail-link" title="Link to this function">
<i class="icon-link"></i>
</a>
<span class="signature">insert(changeset, options \\ [origin: nil, meta: nil, originator_id: nil])</span>
<span class="signature">insert(changeset, options \\ [origin: nil, meta: nil, originator: nil])</span>



Expand All @@ -300,7 +300,7 @@ <h1 class="section-heading">
<a href="#update/2" class="detail-link" title="Link to this function">
<i class="icon-link"></i>
</a>
<span class="signature">update(changeset, options \\ [origin: nil, meta: nil, originator_id: nil])</span>
<span class="signature">update(changeset, options \\ [origin: nil, meta: nil, originator: nil])</span>



Expand Down
6 changes: 3 additions & 3 deletions doc/api-reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="ExDoc v0.14.5">
<title>API Reference – paper_trail v0.6.3</title>
<title>API Reference – paper_trail v0.7.0</title>
<link rel="stylesheet" href="dist/app-a07cea761b.css" />

<link rel="canonical" href="https://hexdocs.pm/paper_trail/api-reference.html" />
Expand All @@ -28,7 +28,7 @@ <h1 class="sidebar-projectName">
paper_trail
</h1>
<h2 class="sidebar-projectVersion">
v0.6.3
v0.7.0
</h2>
</div>

Expand Down Expand Up @@ -60,7 +60,7 @@ <h2 class="sidebar-projectVersion">


<h1>
<small class="visible-xs">paper_trail v0.6.3</small>
<small class="visible-xs">paper_trail v0.7.0</small>
API Reference
</h1>

Expand Down
2 changes: 1 addition & 1 deletion doc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="utf-8">
<title>paper_trail v0.6.3 – Documentation</title>
<title>paper_trail v0.7.0 – Documentation</title>
<meta http-equiv="refresh" content="0; url=api-reference.html">
<meta name="robots" content="noindex">
<meta name="generator" content="ExDoc v0.14.5">
Expand Down
Loading