Skip to content

Commit

Permalink
fix whodoneit-map. smpallen99#18
Browse files Browse the repository at this point in the history
  • Loading branch information
linges committed Jan 25, 2017
1 parent 7d91a9a commit fbf0eed
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/mix/tasks/whatwasit.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,25 @@ defmodule Mix.Tasks.Whatwasit.Install do
config
|> gen_migration
|> gen_version_model
|> gen_repo
|> print_instructions
end

defp gen_repo(%{whodoneit_map: true, binding: binding} = config) do
Mix.Phoenix.copy_from paths(),
"priv/templates/whatwasit.install/lib/whatwasit", "", binding, [
{:eex, "repo_map.ex", "lib/whatwasit/repo.ex"},
]
config
end
defp gen_repo(%{binding: binding} = config) do
Mix.Phoenix.copy_from paths(),
"priv/templates/whatwasit.install/lib/whatwasit", "", binding, [
{:eex, "repo.ex", "lib/whatwasit/repo.ex"},
]
config
end

defp gen_version_model(%{models: true, whodoneit_map: true, boilerplate: true, binding: binding} = config) do
changeset_fields = "~w(item_type item_id object action whodoneit)a"
schema_fields = "field :whodoneit, :map\n"
Expand Down
File renamed without changes.
52 changes: 52 additions & 0 deletions priv/templates/whatwasit.install/lib/whatwasit/repo_.ex
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 priv/templates/whatwasit.install/lib/whatwasit/repo_map.ex
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

0 comments on commit fbf0eed

Please sign in to comment.