Skip to content

Commit

Permalink
fix: properly set changed? for all changesets
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Dec 17, 2023
1 parent ca79254 commit 5f3556e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
10 changes: 2 additions & 8 deletions lib/ash/actions/update.ex
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,9 @@ defmodule Ash.Actions.Update do
authorize?: opts[:authorize?]
)

Ash.Changeset.changing_attributes?(changeset) ||
!Enum.empty?(changeset.atomics) ->
changeset.context.changed? ->
changeset =
changeset
|> Ash.Changeset.set_defaults(:update, true)
|> Ash.Changeset.put_context(:changed?, true)
Ash.Changeset.set_defaults(changeset, :update, true)

changeset.resource
|> Ash.DataLayer.update(changeset)
Expand All @@ -283,9 +280,6 @@ defmodule Ash.Actions.Update do
)

true ->
changeset =
Ash.Changeset.put_context(changeset, :changed?, false)

{:ok, changeset.data}
|> add_tenant(changeset)
|> manage_relationships(api, changeset,
Expand Down
9 changes: 8 additions & 1 deletion lib/ash/changeset/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2406,14 +2406,21 @@ defmodule Ash.Changeset do
{:error, error}

{changeset, %{notifications: before_action_notifications}} ->
changed? =
Ash.Changeset.changing_attributes?(changeset) or
not Enum.empty?(changeset.atomics)

changeset =
Ash.Changeset.put_context(changeset, :changed?, changed?)

changeset
|> clear_phase()
|> func.()
|> case do
{:ok, result, instructions} ->
run_after_actions(
result,
instructions[:new_changeset] || changeset,
changeset,
List.wrap(instructions[:notifications]) ++ before_action_notifications
)

Expand Down
27 changes: 11 additions & 16 deletions lib/ash/error/exception.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Ash.Error.Exception do
end

defmacro def_ash_error(fields, opts \\ []) do
quote do
quote location: :keep, generated: true do
defexception unquote(fields) ++
[
:changeset,
Expand Down Expand Up @@ -45,22 +45,17 @@ defmodule Ash.Error.Exception do
end

def exception(opts) do
if opts[:stacktrace] do
super(opts)
else
case Process.info(self(), :current_stacktrace) do
{:current_stacktrace, stacktrace} ->
super(
Keyword.put_new(opts, :stacktrace, %{
__struct__: Ash.Error.Stacktrace,
stacktrace: Enum.drop(stacktrace, 2)
})
)
opts =
Keyword.put_new_lazy(opts, :stacktrace, fn ->
{:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)

_ ->
super(opts)
end
end
%{
__struct__: Ash.Error.Stacktrace,
stacktrace: Enum.drop(stacktrace, 2)
}
end)

super(opts)
end

defoverridable exception: 1, message: 1
Expand Down

0 comments on commit 5f3556e

Please sign in to comment.