-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow passing of arguments to related resources (#93)
- Loading branch information
Showing
14 changed files
with
370 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defmodule AshArchival.ArchiveRelatedArguments do | ||
@moduledoc """ | ||
The behaviour for specifiying arguments for related resources | ||
""" | ||
@callback arguments( | ||
original_arguments :: map(), | ||
relationship :: atom(), | ||
opts :: Keyword.t() | ||
) :: map() | ||
end |
15 changes: 15 additions & 0 deletions
15
lib/ash_archival/resource/archive_related_arguments/function.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule AshArchival.ArchiveRelatedArguments.Function do | ||
@moduledoc false | ||
|
||
@behaviour AshArchival.ArchiveRelatedArguments | ||
|
||
@impl true | ||
def arguments(arguments, relationship, [{:fun, {m, f, a}}]) do | ||
apply(m, f, [arguments, relationship, a]) | ||
end | ||
|
||
@impl true | ||
def arguments(arguments, relationship, [{:fun, fun}]) do | ||
fun.(arguments, relationship) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
priv/resource_snapshots/test_repo/with_args_children/20250121130028.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
{ | ||
"attributes": [ | ||
{ | ||
"allow_nil?": false, | ||
"default": "fragment(\"gen_random_uuid()\")", | ||
"generated?": false, | ||
"primary_key?": true, | ||
"references": null, | ||
"size": null, | ||
"source": "id", | ||
"type": "uuid" | ||
}, | ||
{ | ||
"allow_nil?": true, | ||
"default": "nil", | ||
"generated?": false, | ||
"primary_key?": false, | ||
"references": null, | ||
"size": null, | ||
"source": "arg1", | ||
"type": "text" | ||
}, | ||
{ | ||
"allow_nil?": true, | ||
"default": "nil", | ||
"generated?": false, | ||
"primary_key?": false, | ||
"references": { | ||
"deferrable": false, | ||
"destination_attribute": "id", | ||
"destination_attribute_default": null, | ||
"destination_attribute_generated": null, | ||
"index?": false, | ||
"match_type": null, | ||
"match_with": null, | ||
"multitenancy": { | ||
"attribute": null, | ||
"global": null, | ||
"strategy": null | ||
}, | ||
"name": "with_args_children_parent_id_fkey", | ||
"on_delete": null, | ||
"on_update": null, | ||
"primary_key?": true, | ||
"schema": "public", | ||
"table": "with_args_parents" | ||
}, | ||
"size": null, | ||
"source": "parent_id", | ||
"type": "uuid" | ||
}, | ||
{ | ||
"allow_nil?": true, | ||
"default": "nil", | ||
"generated?": false, | ||
"primary_key?": false, | ||
"references": null, | ||
"size": null, | ||
"source": "archived_at", | ||
"type": "utc_datetime_usec" | ||
} | ||
], | ||
"base_filter": null, | ||
"check_constraints": [], | ||
"custom_indexes": [], | ||
"custom_statements": [], | ||
"has_create_action": true, | ||
"hash": "00ABDD1E513897CE70121C43F7DD565B216E05F18C3C8325864604E5A71EBC94", | ||
"identities": [], | ||
"multitenancy": { | ||
"attribute": null, | ||
"global": null, | ||
"strategy": null | ||
}, | ||
"repo": "Elixir.AshArchival.TestRepo", | ||
"schema": null, | ||
"table": "with_args_children" | ||
} |
49 changes: 49 additions & 0 deletions
49
priv/resource_snapshots/test_repo/with_args_parents/20250121130028.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"attributes": [ | ||
{ | ||
"allow_nil?": false, | ||
"default": "fragment(\"gen_random_uuid()\")", | ||
"generated?": false, | ||
"primary_key?": true, | ||
"references": null, | ||
"size": null, | ||
"source": "id", | ||
"type": "uuid" | ||
}, | ||
{ | ||
"allow_nil?": true, | ||
"default": "nil", | ||
"generated?": false, | ||
"primary_key?": false, | ||
"references": null, | ||
"size": null, | ||
"source": "arg1", | ||
"type": "text" | ||
}, | ||
{ | ||
"allow_nil?": true, | ||
"default": "nil", | ||
"generated?": false, | ||
"primary_key?": false, | ||
"references": null, | ||
"size": null, | ||
"source": "archived_at", | ||
"type": "utc_datetime_usec" | ||
} | ||
], | ||
"base_filter": null, | ||
"check_constraints": [], | ||
"custom_indexes": [], | ||
"custom_statements": [], | ||
"has_create_action": true, | ||
"hash": "0BBBBD5986B09FEED6E467320D9B2A32E2F63FE1297E1E47F97155400F7B113D", | ||
"identities": [], | ||
"multitenancy": { | ||
"attribute": null, | ||
"global": null, | ||
"strategy": null | ||
}, | ||
"repo": "Elixir.AshArchival.TestRepo", | ||
"schema": null, | ||
"table": "with_args_parents" | ||
} |
42 changes: 42 additions & 0 deletions
42
priv/test_repo/migrations/20250121130028_add_arg_test_resources.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule AshArchival.TestRepo.Migrations.AddArgTestResources do | ||
@moduledoc """ | ||
Updates resources based on their most recent snapshots. | ||
This file was autogenerated with `mix ash_postgres.generate_migrations` | ||
""" | ||
|
||
use Ecto.Migration | ||
|
||
def up do | ||
create table(:with_args_parents, primary_key: false) do | ||
add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true) | ||
add(:arg1, :text) | ||
add(:archived_at, :utc_datetime_usec) | ||
end | ||
|
||
create table(:with_args_children, primary_key: false) do | ||
add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true) | ||
add(:arg1, :text) | ||
|
||
add( | ||
:parent_id, | ||
references(:with_args_parents, | ||
column: :id, | ||
name: "with_args_children_parent_id_fkey", | ||
type: :uuid, | ||
prefix: "public" | ||
) | ||
) | ||
|
||
add(:archived_at, :utc_datetime_usec) | ||
end | ||
end | ||
|
||
def down do | ||
drop(constraint(:with_args_children, "with_args_children_parent_id_fkey")) | ||
|
||
drop(table(:with_args_children)) | ||
|
||
drop(table(:with_args_parents)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule AshArchival.Test.ArgumentTest do | ||
use AshArchival.RepoCase | ||
|
||
alias AshArchival.Test.WithArgsParent | ||
alias AshArchival.Test.WithArgsChild | ||
|
||
test "can pass arguments when archiving related resources" do | ||
parent = | ||
WithArgsParent | ||
|> Ash.Changeset.for_create(:create) | ||
|> Ash.create!() | ||
|
||
WithArgsChild | ||
|> Ash.Changeset.for_create(:create, %{parent_id: parent.id}) | ||
|> Ash.create!() | ||
|
||
parent | ||
|> Ash.Changeset.for_destroy(:archive, %{arg: "test"}) | ||
|> Ash.destroy!() | ||
|
||
parent = | ||
WithArgsParent | ||
|> Ash.Query.for_read(:read) | ||
|> Ash.read_one!() | ||
|
||
assert parent.arg1 == "test" | ||
|
||
child = | ||
WithArgsChild | ||
|> Ash.Query.for_read(:read) | ||
|> Ash.read_one!() | ||
|
||
assert child.arg1 == "test" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.