forked from smpallen99/whatwasit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule Whatwasit.Repo do | ||
|
||
defmacro __using__(_opts \\ []) do | ||
quote do | ||
@base Mix.Project.get |> Module.split |> Enum.reverse |> Enum.at(1) | ||
@version_module Module.concat([@base, Whatwasit, Version]) | ||
alias Ecto.Multi | ||
def insert_with_version(%Ecto.Changeset{} = changeset, opts \\ []) do | ||
repo = opts[:repo] || changeset.repo || Application.get_env(:whatwasit, :repo) | ||
res = Multi.new | ||
|> Multi.insert(:insert, changeset) | ||
|> Multi.run(:version, unquote(__MODULE__), :insert_version, [@version_module, repo, opts]) | ||
|> repo.transaction | ||
|> unquote(__MODULE__).handle_result(:insert) | ||
end | ||
def update_with_version(%Ecto.Changeset{} = changeset, opts \\ []) do | ||
repo = opts[:repo] || changeset.repo || Application.get_env(:whatwasit, :repo) | ||
res = Multi.new | ||
|> Multi.update(:update, changeset) | ||
|> Multi.run(:version, unquote(__MODULE__), :insert_version, [@version_module, repo, opts]) | ||
|> repo.transaction | ||
|> unquote(__MODULE__).handle_result(:update) | ||
end | ||
def delete_with_version(changeset, opts \\ []) do | ||
repo = opts[:repo] || Application.get_env(:whatwasit, :repo) | ||
Multi.new | ||
|> Multi.delete(:delete, changeset) | ||
|> Multi.run(:version, unquote(__MODULE__), :insert_version, [@version_module, repo, opts]) | ||
|> repo.transaction | ||
|> unquote(__MODULE__).handle_result(:delete) | ||
end | ||
end | ||
end | ||
|
||
def insert_version(changes, module, repo, opts) do | ||
[{action, model}] = Map.to_list changes | ||
whodoneit = opts[:whodoneit] | ||
changeset = module.version_changeset(model, whodoneit, action) | ||
apply(repo, :insert, [changeset]) | ||
|> case do | ||
{:ok, _} -> {:ok, model} | ||
error -> error | ||
end | ||
end | ||
|
||
def handle_result(result, action) do | ||
case result do | ||
{:error, _, changeset, _} -> {:error, changeset} | ||
{:ok, %{} = res} -> {:ok, res[action]} | ||
end | ||
end | ||
end |
52 changes: 52 additions & 0 deletions
52
priv/templates/whatwasit.install/lib/whatwasit/repo_map.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule Whatwasit.Repo do | ||
|
||
defmacro __using__(_opts \\ []) do | ||
quote do | ||
@base Mix.Project.get |> Module.split |> Enum.reverse |> Enum.at(1) | ||
@version_module Module.concat([@base, Whatwasit, Version]) | ||
alias Ecto.Multi | ||
def insert_with_version(%Ecto.Changeset{} = changeset, opts \\ []) do | ||
repo = opts[:repo] || changeset.repo || Application.get_env(:whatwasit, :repo) | ||
res = Multi.new | ||
|> Multi.insert(:insert, changeset) | ||
|> Multi.run(:version, unquote(__MODULE__), :insert_version, [@version_module, repo, opts]) | ||
|> repo.transaction | ||
|> unquote(__MODULE__).handle_result(:insert) | ||
end | ||
def update_with_version(%Ecto.Changeset{} = changeset, opts \\ []) do | ||
repo = opts[:repo] || changeset.repo || Application.get_env(:whatwasit, :repo) | ||
res = Multi.new | ||
|> Multi.update(:update, changeset) | ||
|> Multi.run(:version, unquote(__MODULE__), :insert_version, [@version_module, repo, opts]) | ||
|> repo.transaction | ||
|> unquote(__MODULE__).handle_result(:update) | ||
end | ||
def delete_with_version(changeset, opts \\ []) do | ||
repo = opts[:repo] || Application.get_env(:whatwasit, :repo) | ||
Multi.new | ||
|> Multi.delete(:delete, changeset) | ||
|> Multi.run(:version, unquote(__MODULE__), :insert_version, [@version_module, repo, opts]) | ||
|> repo.transaction | ||
|> unquote(__MODULE__).handle_result(:delete) | ||
end | ||
end | ||
end | ||
|
||
def insert_version(changes, module, repo, opts) do | ||
[{action, model}] = Map.to_list changes | ||
whodoneit = opts[:whodoneit] | ||
changeset = module.version_changeset(model, whodoneit, action) | ||
apply(repo, :insert, [changeset]) | ||
|> case do | ||
{:ok, _} -> {:ok, model} | ||
error -> error | ||
end | ||
end | ||
|
||
def handle_result(result, action) do | ||
case result do | ||
{:error, _, changeset, _} -> {:error, changeset} | ||
{:ok, %{} = res} -> {:ok, res[action]} | ||
end | ||
end | ||
end |