Skip to content

Commit

Permalink
feat: adedd old_unit as parameter in on_update function handler
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianmercatante committed Dec 12, 2023
1 parent 51a488b commit 766c0c6
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ arke-*.tar
/tmp/
.elixir_ls/
.idea/
.idea
5 changes: 3 additions & 2 deletions lib/arke/core/unit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ defmodule Arke.Core.Unit do
def load(arke, opts, persistence_fn) when is_list(opts),
do: load(arke, Enum.into(opts, %{}), persistence_fn)

def load(%{metadata: %{project: project}} = arke, %{metadata: nil} = opts, persistence_fn),
do: load(arke, Map.replace(opts, :metadata, %{project: project}), persistence_fn)
def load(%{metadata: %{project: project}} = arke, %{metadata: nil} = opts, persistence_fn) do
load(arke, Map.put(opts, :metadata, %{project: project}), persistence_fn)
end

def load(arke, opts, persistence_fn) do
{id, opts} = Map.pop(opts, :id, nil)
Expand Down
7 changes: 3 additions & 4 deletions lib/arke/query_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ defmodule Arke.QueryManager do
@spec create(project :: atom(), arke :: Arke.t(), args :: list()) :: func_return()
def create(project, arke, args) do
persistence_fn = @persistence[:arke_postgres][:create]

with %Unit{} = unit <- Unit.load(arke, args, :create),
{:ok, unit} <- Validator.validate(unit, :create, project),
{:ok, unit} <- ArkeManager.call_func(arke, :before_create, [arke, unit]),
Expand Down Expand Up @@ -252,16 +251,16 @@ defmodule Arke.QueryManager do
"""
@spec update(Unit.t(), args :: list()) :: func_return()
def update(%{arke_id: arke_id, metadata: %{project: project}, data: data} = unit, args) do
def update(%{arke_id: arke_id, metadata: %{project: project}, data: data} = current_unit, args) do
persistence_fn = @persistence[:arke_postgres][:update]
arke = ArkeManager.get(arke_id, project)

with %Unit{} = unit <- Unit.update(unit, args),
with %Unit{} = unit <- Unit.update(current_unit, args),
{:ok, unit} <- Validator.validate(unit, :update, project),
{:ok, unit} <- ArkeManager.call_func(arke, :before_update, [arke, unit]),
{:ok, unit} <- handle_link_parameters_unit(arke, unit),
{:ok, unit} <- persistence_fn.(project, unit),
{:ok, unit} <- ArkeManager.call_func(arke, :on_update, [arke, unit]),
{:ok, unit} <- ArkeManager.call_func(arke, :on_update, [arke, current_unit, unit]),
{:ok, unit} <- handle_link_parameters(unit, data),
do: {:ok, unit},
else: ({:error, errors} -> {:error, errors})
Expand Down
4 changes: 2 additions & 2 deletions lib/arke/system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defmodule Arke.System do
def before_create(arke, unit), do: {:ok, unit}
def on_struct_encode(_, _, data, opts), do: {:ok, data}
def before_struct_encode(_, unit), do: {:ok, unit}
def on_update(arke, unit), do: {:ok, unit}
def on_update(arke, old_unit, unit), do: {:ok, unit}
def before_update(arke, unit), do: {:ok, unit}
def on_delete(arke, unit), do: {:ok, unit}
def before_delete(arke, unit), do: {:ok, unit}
Expand All @@ -61,7 +61,7 @@ defmodule Arke.System do
before_create: 2,
before_struct_encode: 2,
on_struct_encode: 4,
on_update: 2,
on_update: 3,
before_update: 2,
on_delete: 2,
before_delete: 2,
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Arke.MixProject do
use Mix.Project

@version "0.1.24"
@version "0.1.25"
@scm_url "https://github.com/arkemishub/arke"
@site_url "https://arkehub.com"

Expand Down

0 comments on commit 766c0c6

Please sign in to comment.