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

Configurable repo module #1

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
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,21 @@ The library source code is minimal and tested. It is highly suggested that you c
end
```

2. install and compile your dependency:
2. configure the repo module to use

```elixir
config :paper_trail, repo: YourApplicationName.Repo
```

3. install and compile your dependency:

```mix deps.compile```

3. run this command to generate the migration:
4. run this command to generate the migration:

```mix papertrail.install```

4. run the migration:
5. run the migration:

```mix ecto.migrate```

Expand All @@ -122,7 +128,6 @@ Your application is now ready to collect some history!
## How to use paper_trail with phoenix?

A guide needs to be written for this. Although it isn't that hard to implement for the brave.
One caveat: your application Repo has to be ```Repo``` instead of ```YourApplicationName.Repo```
TODO AREA:
Expand Down
6 changes: 4 additions & 2 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use Mix.Config

config :paper_trail, ecto_repos: [Repo]
config :paper_trail, repo: PaperTrail.Repo

config :paper_trail, Repo,
config :paper_trail, ecto_repos: [PaperTrail.Repo]

config :paper_trail, PaperTrail.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
Expand Down
14 changes: 8 additions & 6 deletions lib/paper_trail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule PaperTrail do
alias Ecto.Multi
alias PaperTrail.Version

@repo PaperTrail.RepoClient.repo

@doc """
Gets all the versions of a record given a module and its id
"""
Expand Down Expand Up @@ -47,9 +49,9 @@ defmodule PaperTrail do
|> Multi.insert(:model, changeset)
|> Multi.run(:version, fn %{model: model} ->
version = make_version_struct(%{event: "create"}, model, meta)
Repo.insert(version)
@repo.insert(version)
end)
|> Repo.transaction
|> @repo.transaction
end

@doc """
Expand All @@ -60,9 +62,9 @@ defmodule PaperTrail do
|> Multi.update(:model, changeset)
|> Multi.run(:version, fn %{model: _model} ->
version = make_version_struct(%{event: "update"}, changeset, meta)
Repo.insert(version)
@repo.insert(version)
end)
|> Repo.transaction
|> @repo.transaction
end

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

defp make_version_struct(%{event: "create"}, model, meta) do
Expand Down
8 changes: 8 additions & 0 deletions lib/paper_trail/paper_trail_repo_client.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule PaperTrail.RepoClient do

@doc """
Gets the configured repo module or defaults to Repo if none configured
"""
def repo,
do: Application.get_env(:paper_trail, :repo) || Repo
end
12 changes: 7 additions & 5 deletions lib/paper_trail/version_queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,45 @@ defmodule PaperTrail.VersionQueries do
import Ecto.Query
alias PaperTrail.Version

@repo PaperTrail.RepoClient.repo

@doc """
Gets all the versions of a record given a module and its id
"""
def get_versions(model, id) do
item_type = model |> Module.split |> List.last
version_query(item_type, id) |> 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) |> 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)) |> 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)) |> Repo.one
last(version_query(item_type, record.id)) |> @repo.one
end

@doc """
Gets the current record of a version
"""
def get_current(version) do
Repo.get("Elixir." <> version.item_type |> String.to_atom, version.item_id)
@repo.get("Elixir." <> version.item_type |> String.to_existing_atom, version.item_id)
end


Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
%{"connection": {:hex, :connection, "1.0.3", "3145f7416be3df248a4935f24e3221dc467c1e3a158d62015b35bd54da365786", [:mix], []},
"db_connection": {:hex, :db_connection, "1.0.0-rc.3", "d9ceb670fe300271140af46d357b669983cd16bc0d01206d7d3222dde56cf038", [:mix], [{:sbroker, "~> 1.0.0-beta.3", [hex: :sbroker, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:connection, "~> 1.0.2", [hex: :connection, optional: false]}]},
"db_connection": {:hex, :db_connection, "1.0.0-rc.3", "d9ceb670fe300271140af46d357b669983cd16bc0d01206d7d3222dde56cf038", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:sbroker, "~> 1.0.0-beta.3", [hex: :sbroker, optional: true]}]},
"decimal": {:hex, :decimal, "1.1.2", "79a769d4657b2d537b51ef3c02d29ab7141d2b486b516c109642d453ee08e00c", [:mix], []},
"earmark": {:hex, :earmark, "0.2.1", "ba6d26ceb16106d069b289df66751734802777a3cbb6787026dd800ffeb850f3", [:mix], []},
"ecto": {:hex, :ecto, "2.0.2", "b02331c1f20bbe944dbd33c8ecd8f1ccffecc02e344c4471a891baf3a25f5406", [:mix], [{:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: true]}, {:sbroker, "~> 1.0-beta", [hex: :sbroker, optional: true]}, {:mariaex, "~> 0.7.7", [hex: :mariaex, optional: true]}, {:postgrex, "~> 0.11.2", [hex: :postgrex, optional: true]}, {:db_connection, "~> 1.0-rc.2", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}]},
"ecto": {:hex, :ecto, "2.0.2", "b02331c1f20bbe944dbd33c8ecd8f1ccffecc02e344c4471a891baf3a25f5406", [:mix], [{:db_connection, "~> 1.0-rc.2", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.7.7", [hex: :mariaex, optional: true]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.11.2", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0-beta", [hex: :sbroker, optional: true]}]},
"ex_doc": {:hex, :ex_doc, "0.12.0", "b774aabfede4af31c0301aece12371cbd25995a21bb3d71d66f5c2fe074c603f", [:mix], [{:earmark, "~> 0.2", [hex: :earmark, optional: false]}]},
"poison": {:hex, :poison, "2.1.0", "f583218ced822675e484648fa26c933d621373f01c6c76bd00005d7bd4b82e27", [:mix], []},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []},
Expand Down
30 changes: 16 additions & 14 deletions test/paper_trail/version_queries_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ defmodule PaperTrailTest.VersionQueries do
alias PaperTrail.Version
import Ecto.Query

@repo PaperTrail.RepoClient.repo

setup_all do
Repo.delete_all(Person)
Repo.delete_all(Company)
Repo.delete_all(PaperTrail.Version)
@repo.delete_all(Person)
@repo.delete_all(Company)
@repo.delete_all(PaperTrail.Version)

Company.changeset(%Company{}, %{
name: "Acme LLC", is_active: true, city: "Greenwich", people: []
}) |> PaperTrail.insert

old_company = first(Company, :id) |> preload(:people) |> Repo.one
old_company = first(Company, :id) |> preload(:people) |> @repo.one

Company.changeset(old_company, %{
city: "Hong Kong",
website: "http://www.acme.com",
facebook: "acme.llc"
}) |> PaperTrail.update

first(Company, :id) |> preload(:people) |> Repo.one |> PaperTrail.delete
first(Company, :id) |> preload(:people) |> @repo.one |> PaperTrail.delete

Company.changeset(%Company{}, %{
name: "Acme LLC",
Expand All @@ -33,7 +35,7 @@ defmodule PaperTrailTest.VersionQueries do
address: "Sesame street 100/3, 101010"
}) |> PaperTrail.insert

company = first(Company, :id) |> Repo.one
company = first(Company, :id) |> @repo.one

Person.changeset(%Person{}, %{
first_name: "Izel",
Expand All @@ -42,13 +44,13 @@ defmodule PaperTrailTest.VersionQueries do
company_id: company.id
}) |> PaperTrail.insert(%{originator: "admin"}) # add link name later on

another_company = Repo.one(
another_company = @repo.one(
from c in Company,
where: c.name == "Another Company Corp.",
limit: 1
)

Person.changeset(first(Person, :id) |> Repo.one, %{
Person.changeset(first(Person, :id) |> @repo.one, %{
first_name: "Isaac",
visit_count: 10,
birthdate: ~D[1992-04-01],
Expand All @@ -59,16 +61,16 @@ defmodule PaperTrailTest.VersionQueries do
end

test "get_version gives us the right version" do
last_person = last(Person, :id) |> Repo.one
target_version = last(Version, :id) |> Repo.one
last_person = last(Person, :id) |> @repo.one
target_version = last(Version, :id) |> @repo.one

assert PaperTrail.get_version(last_person) == target_version
assert PaperTrail.get_version(Person, last_person.id) == target_version
end

test "get_versions gives us the right versions" do
last_person = last(Person, :id) |> Repo.one
target_versions = Repo.all(
last_person = last(Person, :id) |> @repo.one
target_versions = @repo.all(
from version in Version,
where: version.item_type == "Person" and version.item_id == ^last_person.id
)
Expand All @@ -78,8 +80,8 @@ defmodule PaperTrailTest.VersionQueries do
end

test "get_current gives us the current record of a version" do
person = first(Person, :id) |> Repo.one
first_version = Version |> where([v], v.item_type == "Person" and v.item_id == ^person.id) |> first |> Repo.one
person = first(Person, :id) |> @repo.one
first_version = Version |> where([v], v.item_type == "Person" and v.item_id == ^person.id) |> first |> @repo.one

assert PaperTrail.get_current(first_version) == person
end
Expand Down
Loading