Skip to content

Commit

Permalink
test preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Jul 14, 2016
1 parent f725ba6 commit 471c929
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
10 changes: 5 additions & 5 deletions lib/paper_trail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ defmodule PaperTrail do

def get_versions(changeset) do
item_type = changeset.__struct__ |> Module.split |> List.last
version_query(item_type, changeset.id) |> Application.Repo.all # where id, item_type last inserted_at
version_query(item_type, changeset.id) |> Application.Repo.all
end

def get_version(model, id) do
item_type = Module.split(model) |> List.last
version_query(item_type, id) |> Application.Repo.one
last(version_query(item_type, id)) |> Application.Repo.one
end

def get_version(changeset) do
item_type = changeset.__struct__ |> Module.split |> List.last
last(version_query(item_type, changeset.id)) |> Application.Repo.one # where id, item_type last inserted_at
last(version_query(item_type, changeset.id)) |> Application.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)

def insert(struct, meta \\ nil) do
Expand Down Expand Up @@ -72,7 +72,7 @@ defmodule PaperTrail do

def make_version_struct(%{event: "update"}, changeset, meta) do
%Version{
event: "create",
event: "update",
item_type: changeset.data.__struct__ |> Module.split |> List.last,
item_id: changeset.data.id,
item_changes: changeset.changes,
Expand Down
6 changes: 3 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule PaperTrail.Mixfile do

def project do
[app: :paper_trail,
version: "0.0.2",
elixir: "~> 1.2",
version: "0.0.3",
elixir: "~> 1.3",
description: description,
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
Expand All @@ -22,7 +22,7 @@ defmodule PaperTrail.Mixfile do
defp deps do
[
{:postgrex, ">= 0.0.0"},
{:ecto, "~> 2.0.0-rc.1"},
{:ecto, "~> 2.0.2"},
{:poison, "2.1.0"}
]
end
Expand Down
6 changes: 3 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%{"connection": {:hex, :connection, "1.0.2", "f4a06dd3ecae4141aa66f94ce92ea4c4b8753069472814932f1cadbc3078ab80", [:mix], []},
"db_connection": {:hex, :db_connection, "1.0.0-rc.1", "4d9ac0db8a6edf401103b85ad29b40188b70adc649ba2a028df6ff2207cfb250", [:mix], [{:sbroker, "~> 1.0.0-beta.2", [hex: :sbroker, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:connection, "~> 1.0.2", [hex: :connection, optional: false]}]},
%{"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]}]},
"decimal": {:hex, :decimal, "1.1.2", "79a769d4657b2d537b51ef3c02d29ab7141d2b486b516c109642d453ee08e00c", [:mix], []},
"ecto": {:hex, :ecto, "2.0.0-rc.6", "a207218470d8c6366a20940e33f6f9fcd2e9a34c8e9779b808fe100f88c05f5e", [:mix], [{:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: true]}, {:sbroker, "~> 1.0-beta", [hex: :sbroker, optional: true]}, {:postgrex, "~> 0.11.2", [hex: :postgrex, optional: true]}, {:mariaex, "~> 0.7.7", [hex: :mariaex, optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}]},
"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]}]},
"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]}]}}
39 changes: 39 additions & 0 deletions test/paper_trail_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,45 @@ defmodule PaperTrailTest do
use ExUnit.Case
doctest PaperTrail

defmodule Company do
use Ecto.Schema

import Ecto
import Ecto.Changeset
import Ecto.Query

schema "companies" do
field :name, :string
field :is_active, :boolean
field :website, :string
field :city, :string
field :address, :string
field :facebook, :string
field :twitter, :string
field :founded_in, :string

timestamps
end
end

defmodule Person do
use Ecto.Schema

import Ecto
import Ecto.Changeset
import Ecto.Query

schema "people" do
field :first_name, :string
field :last_name, :string
field :visit_count, :integer
field :gender, :boolean
field :birthdate, Ecto.Date

timestamps
end
end

test "the truth" do
assert 1 + 1 == 2
end
Expand Down

0 comments on commit 471c929

Please sign in to comment.