Skip to content

Commit

Permalink
fix: fix bulk destroy handling
Browse files Browse the repository at this point in the history
improvement: pick new values off of result
  • Loading branch information
zachdaniel committed Jul 10, 2024
1 parent 3a36a87 commit 09a9092
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 24 deletions.
8 changes: 4 additions & 4 deletions lib/change_builders/changes_only.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule AshPaperTrail.ChangeBuilders.ChangesOnly do
@moduledoc false
def build_changes(attributes, changeset) do
Enum.reduce(attributes, %{}, &build_attribute_change(&1, changeset, &2))
def build_changes(attributes, changeset, result) do
Enum.reduce(attributes, %{}, &build_attribute_change(&1, changeset, result, &2))
end

def build_attribute_change(attribute, changeset, changes) do
def build_attribute_change(attribute, changeset, result, changes) do
if Ash.Changeset.changing_attribute?(changeset, attribute.name) do
value = Ash.Changeset.get_attribute(changeset, attribute.name)
value = Map.get(result, attribute.name)

{:ok, dumped_value} =
Ash.Type.dump_to_embedded(attribute.type, value, attribute.constraints)
Expand Down
2 changes: 1 addition & 1 deletion lib/change_builders/full_diff/full_diff.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule AshPaperTrail.ChangeBuilders.FullDiff do
}
"""
def build_changes(attributes, changeset) do
def build_changes(attributes, changeset, _result) do
Enum.reduce(attributes, %{}, fn attribute, changes ->
Map.put(
changes,
Expand Down
8 changes: 4 additions & 4 deletions lib/change_builders/snapshot.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule AshPaperTrail.ChangeBuilders.Snapshot do
@moduledoc false
def build_changes(attributes, changeset) do
Enum.reduce(attributes, %{}, &build_attribute_change(&1, changeset, &2))
def build_changes(attributes, changeset, result) do
Enum.reduce(attributes, %{}, &build_attribute_change(&1, changeset, result, &2))
end

def build_attribute_change(attribute, changeset, changes) do
value = Ash.Changeset.get_attribute(changeset, attribute.name)
def build_attribute_change(attribute, _changeset, result, changes) do
value = Map.get(result, attribute.name)
{:ok, dumped_value} = Ash.Type.dump_to_embedded(attribute.type, value, attribute.constraints)
Map.put(changes, attribute.name, dumped_value)
end
Expand Down
32 changes: 19 additions & 13 deletions lib/resource/changes/create_new_version.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ defmodule AshPaperTrail.Resource.Changes.CreateNewVersion do

@impl true
def atomic(changeset, opts, context) do
{:ok, change(changeset, opts, context)}
change_tracking_mode = AshPaperTrail.Resource.Info.change_tracking_mode(changeset.resource)

if change_tracking_mode == :full_diff do
{:not_atomic, "Cannot perform full_diff change tracking with AshPaperTrail atomically."}
else
{:ok, change(changeset, opts, context)}
end
end

defp create_new_version(changeset) do
Expand Down Expand Up @@ -61,12 +67,12 @@ defmodule AshPaperTrail.Resource.Changes.CreateNewVersion do
{input, private} =
resource_attributes
|> Enum.filter(&(&1.name in attributes_as_attributes))
|> Enum.reduce({%{}, %{}}, &build_inputs(changeset, &1, &2))
|> Enum.reduce({%{}, %{}}, &build_inputs(&1, &2, result))

changes =
resource_attributes
|> Enum.reject(&(&1.name in to_skip))
|> build_changes(change_tracking_mode, changeset)
|> build_changes(change_tracking_mode, changeset, result)

input =
Enum.reduce(belongs_to_actors, input, fn belongs_to_actor, input ->
Expand Down Expand Up @@ -104,36 +110,36 @@ defmodule AshPaperTrail.Resource.Changes.CreateNewVersion do
notifications
end

defp build_inputs(changeset, %{public?: true} = attribute, {input, private}) do
defp build_inputs(%{public?: true} = attribute, {input, private}, result) do
{
Map.put(
input,
attribute.name,
Ash.Changeset.get_attribute(changeset, attribute.name)
Map.get(result, attribute.name)
),
private
}
end

defp build_inputs(changeset, attribute, {input, private}) do
defp build_inputs(attribute, {input, private}, result) do
{input,
Map.put(
private,
attribute.name,
Ash.Changeset.get_attribute(changeset, attribute.name)
Map.get(result, attribute.name)
)}
end

defp build_changes(attributes, :changes_only, changeset) do
AshPaperTrail.ChangeBuilders.ChangesOnly.build_changes(attributes, changeset)
defp build_changes(attributes, :changes_only, changeset, result) do
AshPaperTrail.ChangeBuilders.ChangesOnly.build_changes(attributes, changeset, result)
end

defp build_changes(attributes, :snapshot, changeset) do
AshPaperTrail.ChangeBuilders.Snapshot.build_changes(attributes, changeset)
defp build_changes(attributes, :snapshot, changeset, result) do
AshPaperTrail.ChangeBuilders.Snapshot.build_changes(attributes, changeset, result)
end

defp build_changes(attributes, :full_diff, changeset) do
AshPaperTrail.ChangeBuilders.FullDiff.build_changes(attributes, changeset)
defp build_changes(attributes, :full_diff, changeset, result) do
AshPaperTrail.ChangeBuilders.FullDiff.build_changes(attributes, changeset, result)
end

defp authorize?(domain), do: Ash.Domain.Info.authorize(domain) == :always
Expand Down
2 changes: 1 addition & 1 deletion lib/resource/transformers/create_version_resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule AshPaperTrail.Resource.Transformers.CreateVersionResource do
attributes |> Enum.map(& &1.name),
:version_source_id,
:changes,
belongs_to_actors |> Enum.map(&:"#{&1.name}_id")
belongs_to_actors |> Enum.map( &String.to_existing_atom("#{&1.name}_id"))
]
|> List.flatten()
|> Enum.reject(&is_nil/1)
Expand Down
2 changes: 1 addition & 1 deletion test/ash_paper_trail_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ defmodule AshPaperTrailTest do
post = Posts.Post.create!(@valid_attrs, tenant: "acme")

Ash.bulk_destroy!([post], :destroy, %{},
strategy: [:stream, :atomic, :atomic_batches],
strategy: [:atomic],
return_errors?: true,
tenant: "acme"
)
Expand Down

0 comments on commit 09a9092

Please sign in to comment.