Skip to content

Commit

Permalink
Run Formatter (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen authored Dec 16, 2020
1 parent d5f5e8a commit a294a31
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 23 deletions.
26 changes: 24 additions & 2 deletions lib/paper_trail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@ defmodule PaperTrail do
@doc """
Inserts a record to the database with a related version insertion in one transaction
"""
def insert(changeset, options \\ [origin: nil, meta: nil, originator: nil, prefix: nil, model_key: :model, version_key: :version, ecto_options: []]) do
def insert(
changeset,
options \\ [
origin: nil,
meta: nil,
originator: nil,
prefix: nil,
model_key: :model,
version_key: :version,
ecto_options: []
]
) do
PaperTrail.Multi.new()
|> PaperTrail.Multi.insert(changeset, options)
|> PaperTrail.Multi.commit()
Expand All @@ -31,7 +42,18 @@ defmodule PaperTrail do
@doc """
Same as insert/2 but returns only the model struct or raises if the changeset is invalid.
"""
def insert!(changeset, options \\ [origin: nil, meta: nil, originator: nil, prefix: nil, model_key: :model, version_key: :version, ecto_options: []]) do
def insert!(
changeset,
options \\ [
origin: nil,
meta: nil,
originator: nil,
prefix: nil,
model_key: :model,
version_key: :version,
ecto_options: []
]
) do
repo = RepoClient.repo()
ecto_options = options[:ecto_options] || []

Expand Down
19 changes: 15 additions & 4 deletions lib/paper_trail/multi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,19 @@ defmodule PaperTrail.Multi do
defdelegate get_item_type(data), to: Serializer
defdelegate get_model_id(model), to: Serializer

def insert(%Ecto.Multi{} = multi, changeset, options \\ [
origin: nil, meta: nil, originator: nil, prefix: nil, model_key: :model, version_key: :version, ecto_options: []
]) do
def insert(
%Ecto.Multi{} = multi,
changeset,
options \\ [
origin: nil,
meta: nil,
originator: nil,
prefix: nil,
model_key: :model,
version_key: :version,
ecto_options: []
]
) do
model_key = options[:model_key] || :model
version_key = options[:version_key] || :version
ecto_options = options[:ecto_options] || []
Expand Down Expand Up @@ -56,7 +66,8 @@ defmodule PaperTrail.Multi do

repo.insert(updated_changeset, ecto_options)
end)
|> Ecto.Multi.run(version_key, fn repo, %{initial_version: initial_version, model: model} ->
|> Ecto.Multi.run(version_key, fn repo,
%{initial_version: initial_version, model: model} ->
target_version = make_version_struct(%{event: "insert"}, model, options) |> serialize()

Version.changeset(initial_version, target_version) |> repo.update
Expand Down
1 change: 0 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ defmodule PaperTrail.Mixfile do
[
{:ecto, ">= 3.4.6"},
{:ecto_sql, ">= 3.4.5"},

{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
{:jason, ">= 1.2.0", only: [:dev, :test]},
{:postgrex, ">= 0.0.0", only: [:dev, :test]}
Expand Down
11 changes: 8 additions & 3 deletions test/paper_trail/bang_functions_simple_mode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,17 @@ defmodule PaperTrailTest.SimpleModeBangFunctions do

test "PaperTrail.insert!/2 passes ecto options through (e.g. upsert options)" do
user = create_user()
_result = create_company_with_version(@create_company_params, [originator: user])
_result = create_company_with_version(@create_company_params, originator: user)

new_create_company_params = @create_company_params |> Map.replace!(:city, "Barcelona")

ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name]
result = create_company_with_version(new_create_company_params, [originator: user, ecto_options: ecto_options])
ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name]

result =
create_company_with_version(new_create_company_params,
originator: user,
ecto_options: ecto_options
)

assert Company.count() == 1
assert Version.count() == 2
Expand Down
12 changes: 8 additions & 4 deletions test/paper_trail/bang_functions_strict_mode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,17 @@ defmodule PaperTrailTest.StrictModeBangFunctions do

test "PaperTrail.insert!/2 passes ecto options through (e.g. upsert options)" do
user = create_user()
_result = create_company_with_version(@create_company_params, [originator: user])
_result = create_company_with_version(@create_company_params, originator: user)

new_create_company_params = @create_company_params |> Map.replace!(:city, "Barcelona")

ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name]
result = create_company_with_version(new_create_company_params, [originator: user, ecto_options: ecto_options])
ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name]

result =
create_company_with_version(new_create_company_params,
originator: user,
ecto_options: ecto_options
)

assert Company.count() == 1
assert Version.count() == 2
Expand Down Expand Up @@ -1046,5 +1051,4 @@ defmodule PaperTrailTest.StrictModeBangFunctions do
defp convert_to_string_map(map) do
map |> Jason.encode!() |> Jason.decode!()
end

end
16 changes: 12 additions & 4 deletions test/paper_trail/base_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,25 @@ defmodule PaperTrailTest do

test "PaperTrail.insert/2 passes ecto options through (e.g. upsert options)" do
user = create_user()
{:ok, _result} = create_company_with_version(@create_company_params, [originator: user])
{:ok, _result} = create_company_with_version(@create_company_params, originator: user)

new_create_company_params = @create_company_params |> Map.replace!(:city, "Barcelona")

ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name]
{:ok, result} = create_company_with_version(new_create_company_params, [originator: user, ecto_options: ecto_options])
ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name]

{:ok, result} =
create_company_with_version(new_create_company_params,
originator: user,
ecto_options: ecto_options
)

assert Company.count() == 1
assert Version.count() == 2

assert Map.take(serialize(result[:model]), [:name, :city]) == %{name: "Acme LLC", city: "Barcelona"}
assert Map.take(serialize(result[:model]), [:name, :city]) == %{
name: "Acme LLC",
city: "Barcelona"
}
end

test "updating a company with originator creates a correct company version" do
Expand Down
16 changes: 12 additions & 4 deletions test/paper_trail/strict_mode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,25 @@ defmodule PaperTrailStrictModeTest do

test "PaperTrail.insert/2 passes ecto options through (e.g. upsert options)" do
user = create_user()
{:ok, _result} = create_company_with_version(@create_company_params, [originator: user])
{:ok, _result} = create_company_with_version(@create_company_params, originator: user)

new_create_company_params = @create_company_params |> Map.replace!(:city, "Barcelona")

ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name]
{:ok, result} = create_company_with_version(new_create_company_params, [originator: user, ecto_options: ecto_options])
ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name]

{:ok, result} =
create_company_with_version(new_create_company_params,
originator: user,
ecto_options: ecto_options
)

assert Company.count() == 1
assert Version.count() == 2

assert Map.take(serialize(result[:model]), [:name, :city]) == %{name: "Acme LLC", city: "Barcelona"}
assert Map.take(serialize(result[:model]), [:name, :city]) == %{
name: "Acme LLC",
city: "Barcelona"
}
end

test "updating a company creates a company version with correct item_changes" do
Expand Down
1 change: 0 additions & 1 deletion test/support/simple_models.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

defmodule LocationType do
use Ecto.Type

Expand Down

0 comments on commit a294a31

Please sign in to comment.