Skip to content

Commit

Permalink
initial tests on actual paper_trail directory similar to the ones on …
Browse files Browse the repository at this point in the history
…examples
  • Loading branch information
izelnakri committed Jul 16, 2016
1 parent 9e2b340 commit 05ddb6c
Show file tree
Hide file tree
Showing 14 changed files with 315 additions and 130 deletions.
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ use Mix.Config
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"

import_config "#{Mix.env}.exs"
11 changes: 11 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use Mix.Config

config :paper_trail, ecto_repos: [Repo]

config :paper_trail, Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
database: "paper_trail_test",
hostname: "localhost",
poolsize: 10
4 changes: 0 additions & 4 deletions example/lib/example.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ defmodule Example do
import Supervisor.Spec, warn: false

children = [
# Start the endpoint when the application starts
supervisor(Repo, []),
# Start the Ecto repository
# Here you could define other workers and supervisors as children
# worker(Caplair.Worker, [arg1, arg2, arg3]),
]

# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
Expand Down
2 changes: 1 addition & 1 deletion example/mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"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]}]},
"decimal": {:hex, :decimal, "1.1.2", "79a769d4657b2d537b51ef3c02d29ab7141d2b486b516c109642d453ee08e00c", [: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]}]},
"paper_trail": {:hex, :paper_trail, "0.0.9", "4e9342ee2bae6df9c80a2a3b5619e8df2a0113a545e5671d964ffa5dc9c37b69", [:mix], [{:poison, "2.1.0", [hex: :poison, optional: false]}, {:ecto, "~> 2.0.2", [hex: :ecto, optional: false]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, optional: false]}]},
"paper_trail": {:hex, :paper_trail, "0.1.0", "0ebc0159795bb8d99b1633a552312a947b80f88f48d338ff02f1ba445fd3605e", [:mix], [{:poison, "2.1.0", [hex: :poison, optional: false]}, {:ecto, "~> 2.0.2", [hex: :ecto, optional: false]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, optional: false]}]},
"poison": {:hex, :poison, "2.1.0", "f583218ced822675e484648fa26c933d621373f01c6c76bd00005d7bd4b82e27", [:mix], []},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []},
"postgrex": {:hex, :postgrex, "0.11.2", "139755c1359d3c5c6d6e8b1ea72556d39e2746f61c6ddfb442813c91f53487e8", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 1.0-rc", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}}
2 changes: 0 additions & 2 deletions example/test/company_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ defmodule CompanyTest do

doctest Company

# maybe test meta tag insertion and relationships

setup_all do
Repo.delete_all(Person)
Repo.delete_all(Company)
Expand Down
97 changes: 29 additions & 68 deletions lib/paper_trail.ex
Original file line number Diff line number Diff line change
@@ -1,47 +1,10 @@
defmodule PaperTrail do
alias Ecto.Multi
import Ecto.Query
import PaperTrail.VersionQueries

alias Ecto.Multi
alias PaperTrail.Version

@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
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
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
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
end

defp version_query(item_type, id) do
from v in Version,
where: v.item_type == ^item_type and v.item_id == ^id
end

# changeset = Model.changeset(Ecto.Repo.get(Model, id), params)

@doc """
Inserts a record to the database with a related version insertion in one transaction
"""
Expand All @@ -55,24 +18,6 @@ defmodule PaperTrail do
|> Repo.transaction
end

defp make_version_struct(%{event: "create"}, model, meta) do
%Version{
event: "create",
item_type: model.__struct__ |> Module.split |> List.last,
item_id: model.id,
item_changes: filter_item_changes(model),
meta: meta
}
end

defp filter_item_changes(model) do
relationships = model.__struct__.__schema__(:associations)

Map.drop(model, [:__struct__, :__meta__] ++ relationships)
end

# might make the changeset version

@doc """
Updates a record from the database with a related version insertion in one transaction
"""
Expand All @@ -86,16 +31,6 @@ defmodule PaperTrail do
|> Repo.transaction
end

defp make_version_struct(%{event: "update"}, changeset, meta) do
%Version{
event: "update",
item_type: changeset.data.__struct__ |> Module.split |> List.last,
item_id: changeset.data.id,
item_changes: changeset.changes,
meta: meta
}
end

@doc """
Deletes a record from the database with a related version insertion in one transaction
"""
Expand All @@ -109,6 +44,26 @@ defmodule PaperTrail do
|> Repo.transaction
end

defp make_version_struct(%{event: "create"}, model, meta) do
%Version{
event: "create",
item_type: model.__struct__ |> Module.split |> List.last,
item_id: model.id,
item_changes: filter_item_changes(model),
meta: meta
}
end

defp make_version_struct(%{event: "update"}, changeset, meta) do
%Version{
event: "update",
item_type: changeset.data.__struct__ |> Module.split |> List.last,
item_id: changeset.data.id,
item_changes: changeset.changes,
meta: meta
}
end

defp make_version_struct(%{event: "destroy"}, model, meta) do
%Version{
event: "destroy",
Expand All @@ -118,4 +73,10 @@ defmodule PaperTrail do
meta: meta
}
end

defp filter_item_changes(model) do
relationships = model.__struct__.__schema__(:associations)

Map.drop(model, [:__struct__, :__meta__] ++ relationships)
end
end
4 changes: 4 additions & 0 deletions lib/paper_trail/migration.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule PaperTrail.Migration do
# TODO: this module will handle column changes in database schemas. It will query legacy column names
# and change the legacy column names to new column names in every :item_changes field of a PaperTrail.Version
end
42 changes: 42 additions & 0 deletions lib/paper_trail/version_queries.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
defmodule PaperTrail.VersionQueries do
import Ecto.Query
alias PaperTrail.Version


@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
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
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
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
end

defp version_query(item_type, id) do
from v in Version,
where: v.item_type == ^item_type and v.item_id == ^id
end
end
2 changes: 0 additions & 2 deletions lib/version.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ defmodule PaperTrail.Version do
import Ecto.Changeset
import Ecto.Query

# how to record column changes in migration ?

schema "versions" do
field :event, :string
field :item_type, :string
Expand Down
4 changes: 3 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ defmodule PaperTrail.Mixfile do
#
# Type "mix help compile.app" for more information
def application do
[]
[
applications: [:logger, :postgrex, :ecto]
]
end

defp deps do
Expand Down
4 changes: 2 additions & 2 deletions priv/repo/migrations/20160619190937_add_companies.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule Repo.Migrations.AddCompanies do
defmodule Repo.Migrations.CreateCompanies do
use Ecto.Migration

def change do
create table(:companies) do
add :name, :string
add :is_active, :string
add :is_active, :boolean
add :website, :string
add :city, :string
add :address, :string
Expand Down
6 changes: 5 additions & 1 deletion priv/repo/migrations/20160619190938_add_people.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Repo.Migrations.AddPeople do
defmodule Repo.Migrations.CreatePeople do
use Ecto.Migration

def change do
Expand All @@ -9,7 +9,11 @@ defmodule Repo.Migrations.AddPeople do
add :gender, :boolean
add :birthdate, :date

add :company_id, references(:companies), null: false

timestamps
end

create index(:people, [:company_id])
end
end
Loading

0 comments on commit 05ddb6c

Please sign in to comment.