Skip to content

Commit

Permalink
fix: set resource option in bulk interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Apr 28, 2024
1 parent 69f844d commit 2f1a0fe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/ash/code_interface.ex
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ defmodule Ash.CodeInterface do
|> Keyword.drop([:bulk_options, :atomic_upgrade?])
|> Keyword.merge(Keyword.get(opts, :bulk_options, []))
|> Enum.concat(changeset_opts)
|> Keyword.put(:resource, unquote(resource))
|> then(fn bulk_opts ->
if method == :id do
bulk_opts
Expand All @@ -841,7 +842,7 @@ defmodule Ash.CodeInterface do
end)

bulk_opts =
if method == :stream do
if method in [:stream, :query] do
Keyword.put(bulk_opts, :filter, filters)
else
bulk_opts
Expand All @@ -853,7 +854,7 @@ defmodule Ash.CodeInterface do
|> Ash.bulk_update(unquote(action.name), params, bulk_opts)
|> case do
%Ash.BulkResult{} = result
when method == :stream and unquote(Enum.empty?(filter_keys)) ->
when method in [:stream, :query] ->
result

%Ash.BulkResult{status: :success, records: [record]} = result ->
Expand Down Expand Up @@ -892,6 +893,7 @@ defmodule Ash.CodeInterface do
|> Keyword.drop([:bulk_options, :atomic_upgrade?])
|> Keyword.merge(Keyword.get(opts, :bulk_options, []))
|> Enum.concat(changeset_opts)
|> Keyword.put(:resource, unquote(resource))
|> then(fn bulk_opts ->
if method == :id do
bulk_opts
Expand All @@ -903,7 +905,7 @@ defmodule Ash.CodeInterface do
end)

bulk_opts =
if method == :stream do
if method in [:stream] do
Keyword.put(bulk_opts, :filter, filters)
else
bulk_opts
Expand All @@ -915,7 +917,7 @@ defmodule Ash.CodeInterface do
|> Ash.bulk_update!(unquote(action.name), params, bulk_opts)
|> case do
%Ash.BulkResult{} = result
when method == :stream and unquote(Enum.empty?(filter_keys)) ->
when method in [:stream, :query] ->
result

%Ash.BulkResult{status: :success, records: [record]} = result ->
Expand Down Expand Up @@ -1029,6 +1031,7 @@ defmodule Ash.CodeInterface do
|> Keyword.drop([:bulk_options, :return_destroyed?])
|> Keyword.merge(Keyword.get(opts, :bulk_options, []))
|> Enum.concat(changeset_opts)
|> Keyword.put(:resource, unquote(resource))
|> then(fn bulk_opts ->
if method == :id do
bulk_opts
Expand All @@ -1040,7 +1043,7 @@ defmodule Ash.CodeInterface do
end)

bulk_opts =
if method == :stream do
if method in [:stream, :query] do
Keyword.put(bulk_opts, :filter, filters)
else
bulk_opts
Expand All @@ -1052,7 +1055,7 @@ defmodule Ash.CodeInterface do
|> Ash.bulk_destroy(unquote(action.name), params, bulk_opts)
|> case do
%Ash.BulkResult{} = result
when method == :stream and unquote(Enum.empty?(filter_keys)) ->
when method in [:stream, :query] ->
result

%Ash.BulkResult{status: :success, records: [record]} = result ->
Expand Down Expand Up @@ -1096,6 +1099,7 @@ defmodule Ash.CodeInterface do
|> Keyword.drop([:bulk_options, :return_destroyed?])
|> Keyword.merge(Keyword.get(opts, :bulk_options, []))
|> Enum.concat(changeset_opts)
|> Keyword.put(:resource, unquote(resource))
|> then(fn bulk_opts ->
if method == :id do
bulk_opts
Expand All @@ -1107,7 +1111,7 @@ defmodule Ash.CodeInterface do
end)

bulk_opts =
if method == :stream do
if method in [:stream, :query] do
Keyword.put(bulk_opts, :filter, filters)
else
bulk_opts
Expand All @@ -1119,7 +1123,7 @@ defmodule Ash.CodeInterface do
|> Ash.bulk_destroy!(unquote(action.name), params, bulk_opts)
|> case do
%Ash.BulkResult{} = result
when method == :stream and unquote(Enum.empty?(filter_keys)) ->
when method in [:stream, :query] ->
result

%Ash.BulkResult{status: :success, records: [record]} = result ->
Expand Down
9 changes: 9 additions & 0 deletions test/code_interface_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ defmodule Ash.Test.CodeInterfaceTest do
assert _record = User.update!(bob, %{first_name: "bob_updated"}, context: @context)
end

test "can take a query" do
bob = User.create!("bob", context: @context)
require Ash.Query

User
|> Ash.Query.filter(id == ^bob.id)
|> User.update!(%{first_name: "bob_updated"})
end

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

0 comments on commit 2f1a0fe

Please sign in to comment.