Skip to content

Commit

Permalink
fix: simplify setting attributes to avoid issue w/ private attributes…
Browse files Browse the repository at this point in the history
… returning errors (#102)
  • Loading branch information
TimisRobert authored Aug 18, 2024
1 parent cd92973 commit b4e3555
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 27 deletions.
30 changes: 5 additions & 25 deletions lib/resource/changes/create_new_version.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ defmodule AshPaperTrail.Resource.Changes.CreateNewVersion do
changeset.resource
|> Ash.Resource.Info.attributes()

{input, private} =
resource_attributes
|> Enum.filter(&(&1.name in attributes_as_attributes))
|> Enum.reduce({%{}, %{}}, &build_inputs(&1, &2, result))
input =
version_resource_attributes
|> Enum.filter(&(&1 in attributes_as_attributes))
|> Enum.map(&{&1, Map.get(result, &1)})
|> Enum.into(%{})

changes =
resource_attributes
Expand Down Expand Up @@ -104,32 +105,11 @@ defmodule AshPaperTrail.Resource.Changes.CreateNewVersion do
domain: changeset.domain,
skip_unknown_inputs: Map.keys(input)
)
|> Ash.Changeset.force_change_attributes(Map.take(private, version_resource_attributes))
|> Ash.create!(return_notifications?: true)

notifications
end

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

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

defp build_changes(attributes, :changes_only, changeset, result) do
AshPaperTrail.ChangeBuilders.ChangesOnly.build_changes(attributes, changeset, result)
end
Expand Down
4 changes: 2 additions & 2 deletions test/support/articles/article.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule AshPaperTrail.Test.Articles.Article do
end

actions do
default_accept :*
default_accept [:*, :body]
defaults [:create, :read, :update, :destroy]
end

Expand All @@ -36,7 +36,7 @@ defmodule AshPaperTrail.Test.Articles.Article do
end

attribute :body, :string do
public? true
public? false
allow_nil? false
end
end
Expand Down

0 comments on commit b4e3555

Please sign in to comment.