diff --git a/README.md b/README.md index d4d43ed4..9a07608e 100644 --- a/README.md +++ b/README.md @@ -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``` @@ -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: diff --git a/config/test.exs b/config/test.exs index 86a56ec4..a8d4feef 100644 --- a/config/test.exs +++ b/config/test.exs @@ -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", diff --git a/lib/paper_trail.ex b/lib/paper_trail.ex index 52a0ebaf..7b324193 100644 --- a/lib/paper_trail.ex +++ b/lib/paper_trail.ex @@ -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 """ @@ -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 """ @@ -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 """ @@ -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 diff --git a/lib/paper_trail/paper_trail_repo_client.ex b/lib/paper_trail/paper_trail_repo_client.ex new file mode 100644 index 00000000..149fa8c0 --- /dev/null +++ b/lib/paper_trail/paper_trail_repo_client.ex @@ -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 diff --git a/lib/paper_trail/version_queries.ex b/lib/paper_trail/version_queries.ex index c16f6d11..f907097c 100644 --- a/lib/paper_trail/version_queries.ex +++ b/lib/paper_trail/version_queries.ex @@ -2,12 +2,14 @@ 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 """ @@ -15,7 +17,7 @@ defmodule PaperTrail.VersionQueries do """ 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 """ @@ -23,7 +25,7 @@ defmodule PaperTrail.VersionQueries do """ 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 """ @@ -31,14 +33,14 @@ defmodule PaperTrail.VersionQueries do """ 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 diff --git a/mix.lock b/mix.lock index a10fea12..a23862cb 100644 --- a/mix.lock +++ b/mix.lock @@ -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], []}, diff --git a/test/paper_trail/version_queries_test.exs b/test/paper_trail/version_queries_test.exs index 8c69e7ac..edc22164 100644 --- a/test/paper_trail/version_queries_test.exs +++ b/test/paper_trail/version_queries_test.exs @@ -3,16 +3,18 @@ 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", @@ -20,7 +22,7 @@ defmodule PaperTrailTest.VersionQueries do 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", @@ -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", @@ -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], @@ -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 ) @@ -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 diff --git a/test/paper_trail_test.exs b/test/paper_trail_test.exs index 7f020809..52e13b8a 100644 --- a/test/paper_trail_test.exs +++ b/test/paper_trail_test.exs @@ -2,12 +2,14 @@ defmodule PaperTrailTest do use ExUnit.Case import Ecto.Query + @repo PaperTrail.RepoClient.repo + doctest PaperTrail 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) :ok end @@ -18,14 +20,14 @@ defmodule PaperTrailTest do {:ok, result} = PaperTrail.insert(new_company) - company_count = Repo.all( + company_count = @repo.all( from company in Company, select: count(company.id) ) company = result[:model] |> Map.drop([:__meta__, :__struct__, :inserted_at, :updated_at, :id]) - version_count = Repo.all( + version_count = @repo.all( from version in PaperTrail.Version, select: count(version.id) ) @@ -50,14 +52,14 @@ defmodule PaperTrailTest do assert Map.drop(version, [:id]) == %{ event: "create", item_type: "Company", - item_id: Repo.one(first(Company, :id)).id, + item_id: @repo.one(first(Company, :id)).id, item_changes: Map.drop(result[:model], [:__meta__, :__struct__, :people]), meta: nil } end test "updating a company creates a company version with correct item_changes" do - old_company = first(Company, :id) |> preload(:people) |> Repo.one + old_company = first(Company, :id) |> preload(:people) |> @repo.one new_company = Company.changeset(old_company, %{ city: "Hong Kong", website: "http://www.acme.com", @@ -66,14 +68,14 @@ defmodule PaperTrailTest do {:ok, result} = PaperTrail.update(new_company) - company_count = Repo.all( + company_count = @repo.all( from company in Company, select: count(company.id) ) company = result[:model] |> Map.drop([:__meta__, :__struct__, :inserted_at, :updated_at, :id]) - version_count = Repo.all( + version_count = @repo.all( from version in PaperTrail.Version, select: count(version.id) ) @@ -98,25 +100,25 @@ defmodule PaperTrailTest do assert Map.drop(version, [:id]) == %{ event: "update", item_type: "Company", - item_id: Repo.one(first(Company, :id)).id, + item_id: @repo.one(first(Company, :id)).id, item_changes: %{city: "Hong Kong", website: "http://www.acme.com", facebook: "acme.llc"}, meta: nil } end test "deleting a company creates a company version with correct attributes" do - company = first(Company, :id) |> preload(:people) |> Repo.one + company = first(Company, :id) |> preload(:people) |> @repo.one {:ok, result} = PaperTrail.delete(company) - company_count = Repo.all( + company_count = @repo.all( from company in Company, select: count(company.id) ) company_ref = result[:model] |> Map.drop([:__meta__, :__struct__, :inserted_at, :updated_at, :id]) - version_count = Repo.all( + version_count = @repo.all( from version in PaperTrail.Version, select: count(version.id) ) @@ -172,7 +174,7 @@ defmodule PaperTrailTest do address: "Sesame street 100/3, 101010" }) |> PaperTrail.insert - company = first(Company, :id) |> Repo.one + company = first(Company, :id) |> @repo.one new_person = Person.changeset(%Person{}, %{ first_name: "Izel", @@ -183,14 +185,14 @@ defmodule PaperTrailTest do {:ok, result} = PaperTrail.insert(new_person, %{originator: "admin"}) # add link name later on - person_count = Repo.all( + person_count = @repo.all( from person in Person, select: count(person.id) ) person = result[:model] |> Map.drop([:__meta__, :__struct__, :inserted_at, :updated_at, :id]) - version_count = Repo.all( + version_count = @repo.all( from version in PaperTrail.Version, select: count(version.id) ) @@ -212,16 +214,16 @@ defmodule PaperTrailTest do assert Map.drop(version, [:id]) == %{ event: "create", item_type: "Person", - item_id: Repo.one(first(Person, :id)).id, + item_id: @repo.one(first(Person, :id)).id, item_changes: Map.drop(result[:model], [:__meta__, :__struct__, :company]), meta: %{originator: "admin"} } end test "updating a person creates a person version with correct attributes" do - old_person = first(Person, :id) |> Repo.one + old_person = first(Person, :id) |> @repo.one - target_company = Repo.one( + target_company = @repo.one( from c in Company, where: c.name == "Another Company Corp.", limit: 1 @@ -239,14 +241,14 @@ defmodule PaperTrailTest do linkname: "izelnakri" }) - person_count = Repo.all( + person_count = @repo.all( from person in Person, select: count(person.id) ) person = result[:model] |> Map.drop([:__meta__, :__struct__, :inserted_at, :updated_at, :id]) - version_count = Repo.all( + version_count = @repo.all( from version in PaperTrail.Version, select: count(version.id) ) @@ -268,7 +270,7 @@ defmodule PaperTrailTest do assert Map.drop(version, [:id]) == %{ event: "update", item_type: "Person", - item_id: Repo.one(first(Person, :id)).id, + item_id: @repo.one(first(Person, :id)).id, item_changes: %{ first_name: "Isaac", visit_count: 10, @@ -283,16 +285,16 @@ defmodule PaperTrailTest do end test "deleting a person creates a person version with correct attributes" do - person = first(Person, :id) |> preload(:company) |> Repo.one + person = first(Person, :id) |> preload(:company) |> @repo.one {:ok, result} = PaperTrail.delete(person) - person_count = Repo.all( + person_count = @repo.all( from person in Person, select: count(person.id) ) - version_count = Repo.all( + version_count = @repo.all( from version in PaperTrail.Version, select: count(version.id) ) diff --git a/test/test_helper.exs b/test/test_helper.exs index 09e76133..c2bf58d4 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,9 +1,9 @@ -defmodule Repo do +defmodule PaperTrail.Repo do use Ecto.Repo, otp_app: :paper_trail end -Mix.Task.run "ecto.create", ~w(-r Repo) -Mix.Task.run "ecto.migrate", ~w(-r Repo) +Mix.Task.run "ecto.create", ~w(-r PaperTrail.Repo) +Mix.Task.run "ecto.migrate", ~w(-r PaperTrail.Repo) defmodule Company do use Ecto.Schema @@ -60,7 +60,7 @@ defmodule Person do end end -Repo.start_link +PaperTrail.Repo.start_link ExUnit.configure seed: 0