Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/events/transformers/wrap_actions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ defmodule AshEvents.Events.Transformers.WrapActions do
end
end)

# Copy manage_relationship changes with ignore?: true so that AshPhoenix
# can detect them for nested form generation. The actual relationship
# management is handled by the wrapped version above.
ignored_manage_relationship_changes =
action.changes
|> Enum.filter(fn
%Ash.Resource.Change{change: {Ash.Resource.Change.ManageRelationship, _}} -> true
_ -> false
end)
|> Enum.map(fn %Ash.Resource.Change{change: {mod, opts}} = change ->
updated_opts =
Keyword.update(opts, :opts, [ignore?: true], &Keyword.put(&1, :ignore?, true))

%{change | change: {mod, updated_opts}}
end)

manual_module =
case action.type do
:create -> AshEvents.CreateActionWrapper
Expand All @@ -167,7 +183,8 @@ defmodule AshEvents.Events.Transformers.WrapActions do
arguments: action.arguments,
changes:
[store_changeset_params, apply_replay_primary_key | wrapped_changes] ++
[apply_changed_attributes]
[apply_changed_attributes] ++
ignored_manage_relationship_changes
}
|> then(fn action ->
case action.type do
Expand Down
28 changes: 28 additions & 0 deletions test/ash_events/manage_relationship_forms_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# SPDX-FileCopyrightText: 2023 ash_events contributors <https://github.com/ash-project/ash_events/graphs/contributors>
#
# SPDX-License-Identifier: MIT

defmodule AshEvents.ManageRelationshipFormsTest do
use ExUnit.Case, async: true

# Regression test for https://github.com/ash-project/ash_events/issues/87
# AshPhoenix.Form.Auto needs to detect manage_relationship changes on actions
# wrapped by AshEvents, so that nested forms (inputs_for) work correctly.

test "AshPhoenix auto forms detect manage_relationship on AshEvents-wrapped actions" do
auto_forms = AshPhoenix.Form.Auto.auto(AshEvents.Accounts.User, :create_with_roles)

assert Keyword.has_key?(auto_forms, :user_role),
"Expected auto forms to include :user_role, got: #{inspect(Keyword.keys(auto_forms))}"
end

test "AshPhoenix.Form.for_create works with forms: [auto?: true] on wrapped actions" do
form =
AshPhoenix.Form.for_create(AshEvents.Accounts.User, :create_with_roles,
forms: [auto?: true]
)

assert Keyword.has_key?(form.form_keys, :user_role),
"Expected :user_role in form_keys, got: #{inspect(Keyword.keys(form.form_keys))}"
end
end
8 changes: 8 additions & 0 deletions test/support/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ defmodule AshEvents.Accounts.User do
change set_attribute(:confirmed_at, &DateTime.utc_now/0)
end

create :create_with_roles do
accept [:id, :created_at, :updated_at, :email, :given_name, :family_name, :hashed_password]
argument :user_role, :map

change manage_relationship(:user_role, type: :direct_control)
change set_attribute(:confirmed_at, &DateTime.utc_now/0)
end

update :update do
require_atomic? false
accept [:given_name, :family_name, :created_at, :updated_at, :hashed_password]
Expand Down