Skip to content

Commit

Permalink
fix: code_interface actions accept @context (#1016)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasender authored Apr 12, 2024
1 parent 8ea51ec commit 10e3722
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/ash/changeset/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,10 @@ defmodule Ash.Changeset do
type: {:list, {:or, [:atom, :string]}},
doc:
"A list of inputs that, if provided, will be ignored if they are not recognized by the action."
],
context: [
type: :map,
doc: "Context to set on the query, changeset, or input"
]
]

Expand Down
10 changes: 5 additions & 5 deletions lib/ash/code_interface.ex
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ defmodule Ash.CodeInterface do
resolve_subject =
quote do
{query_opts, opts} =
Keyword.split(opts, [:query, :actor, :tenant, :authorize?, :tracer])
Keyword.split(opts, [:query, :actor, :tenant, :authorize?, :tracer, :context])

{query, query_opts} = Keyword.pop(query_opts, :query)

Expand Down Expand Up @@ -658,7 +658,7 @@ defmodule Ash.CodeInterface do
{changeset, opts} = Keyword.pop(opts, :changeset)

{changeset_opts, opts} =
Keyword.split(opts, [:changeset, :actor, :tenant, :authorize?, :tracer])
Keyword.split(opts, [:actor, :tenant, :authorize?, :tracer, :context])

changeset_opts = Keyword.put(changeset_opts, :domain, unquote(domain))

Expand Down Expand Up @@ -699,7 +699,7 @@ defmodule Ash.CodeInterface do
|> Enum.concat(changeset_opts)

Ash.bulk_create(
Stream.map(inputs, &Map.merge(&1, params)),
inputs,
unquote(resource),
unquote(action.name),
bulk_opts
Expand Down Expand Up @@ -741,7 +741,7 @@ defmodule Ash.CodeInterface do
resolve_subject =
quote do
{changeset_opts, opts} =
Keyword.split(opts, [:actor, :tenant, :authorize?, :tracer])
Keyword.split(opts, [:actor, :tenant, :authorize?, :tracer, :context])

changeset_opts = Keyword.put(changeset_opts, :domain, unquote(domain))

Expand Down Expand Up @@ -901,7 +901,7 @@ defmodule Ash.CodeInterface do
resolve_subject =
quote do
{changeset_opts, opts} =
Keyword.split(opts, [:actor, :tenant, :authorize?, :tracer])
Keyword.split(opts, [:actor, :tenant, :authorize?, :tracer, :context])

changeset_opts = Keyword.put(changeset_opts, :domain, unquote(domain))

Expand Down
86 changes: 86 additions & 0 deletions test/code_interface_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ defmodule Ash.Test.CodeInterfaceTest do
define :create, args: [{:optional, :first_name}]
define :hello, args: [:name]

define :bulk_create, action: :create
define :update, action: :update
define :destroy, action: :destroy

define_calculation(:full_name, args: [:first_name, :last_name])

Expand All @@ -43,6 +45,8 @@ defmodule Ash.Test.CodeInterfaceTest do

update :update

destroy :destroy

read :by_id do
argument :id, :uuid, allow_nil?: false

Expand Down Expand Up @@ -91,6 +95,8 @@ defmodule Ash.Test.CodeInterfaceTest do
end
end

@context %{test: "value"}

describe "generic actions" do
test "can be invoked" do
assert "Hello fred" == User.hello!("fred")
Expand Down Expand Up @@ -148,6 +154,14 @@ defmodule Ash.Test.CodeInterfaceTest do
User.get_user_safely!(Ash.UUID.generate(), not_found_error?: true)
end
end

test "can take a @context options" do
assert {:ok, nil} ==
User.get_user(Ash.UUID.generate(), not_found_error?: false, context: @context)

assert nil ==
User.get_user!(Ash.UUID.generate(), not_found_error?: false, context: @context)
end
end

describe "create actions" do
Expand All @@ -165,6 +179,78 @@ defmodule Ash.Test.CodeInterfaceTest do
assert User.can_create?(nil)
assert User.can_create?(nil, "bob")
end

test "can take a @context options" do
assert {:ok, _record} = User.create("bob", context: @context)
assert _record = User.create!("bob", context: @context)
end

test "bulk_create can take a @context options" do
assert %Ash.BulkResult{status: :success} =
User.bulk_create([%{first_name: "bob"}, %{first_name: "other_bob"}],
context: @context
)
end
end

describe "update actions" do
test "have a helper methods to produce changeset" do
bob = User.create!("bob", context: @context)

assert %Ash.Changeset{action: %{name: :update}, attributes: %{first_name: "fred"}} =
User.changeset_to_update(bob, %{first_name: "fred"})
end

test "can take a @context options" do
bob = User.create!("bob", context: @context)

assert {:ok, _record} = User.update(bob, %{first_name: "bob_updated"}, context: @context)
assert _record = User.update!(bob, %{first_name: "bob_updated"}, context: @context)
end

test "bulk update can take a @context options" do
bob1 = User.create!("bob", context: @context)
bob2 = User.create!("bob", context: @context)

assert %Ash.BulkResult{status: :success} =
User.update([bob1, bob2], %{first_name: "other_bob"}, context: @context)

assert result =
User.update!([bob1, bob2], %{first_name: "different_bob"},
context: @context,
bulk_options: [return_records?: true]
)

Enum.map(result.records, &assert(&1.first_name == "different_bob"))
end
end

describe "destroy actions" do
test "have a helper methods to produce changeset" do
bob = User.create!("bob", context: @context)

assert %Ash.Changeset{action: %{name: :destroy}} = User.changeset_to_destroy(bob)
end

test "can take a @context options" do
bob1 = User.create!("bob", context: @context)
bob2 = User.create!("bob", context: @context)

assert :ok = User.destroy(bob1, context: @context)
assert :ok = User.destroy!(bob2, context: @context)
end

test "bulk destroy can take a @context options" do
bob1 = User.create!("bob", context: @context)
bob2 = User.create!("bob", context: @context)

assert %Ash.BulkResult{status: :success} = User.destroy([bob1, bob2], context: @context)

bob3 = User.create!("bob", context: @context)
bob4 = User.create!("bob", context: @context)

assert %Ash.BulkResult{status: :success} = User.destroy!([bob3, bob4], context: @context)
end
end

describe "calculations" do
Expand Down

0 comments on commit 10e3722

Please sign in to comment.