Skip to content

Commit

Permalink
improvement: update to Ash 3.0
Browse files Browse the repository at this point in the history
still have one failing test to address
  • Loading branch information
zachdaniel committed Mar 30, 2024
1 parent e4f1a15 commit bd0f1f8
Show file tree
Hide file tree
Showing 32 changed files with 222 additions and 258 deletions.
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spark_locals_without_parens = [
allow_nil?: 1,
api: 1,
domain: 1,
attribute_type: 1,
attributes_as_attributes: 1,
belongs_to_actor: 2,
Expand Down
7 changes: 3 additions & 4 deletions .github/ISSUE_TEMPLATE/proposal.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---
name: Proposal
about: Suggest an idea for this project
title: ''
title: ""
labels: enhancement, needs review
assignees: ''

assignees: ""
---

**Is your feature request related to a problem? Please describe.**
Expand All @@ -29,7 +28,7 @@ For example
Or

```elixir
Api.read(:resource, bar: 10) # <- Adding `bar` here would cause <x>
Ash.read(:resource, bar: 10) # <- Adding `bar` here would cause <x>
```

**Additional context**
Expand Down
28 changes: 10 additions & 18 deletions documentation/tutorials/get-started-with-paper-trail.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Then, add the `AshPaperTrail.Resource` extension to any resource you would like

```elixir
use Ash.Resource,
domain: MyDomain,
extensions: [
AshPaperTrail.Resource
]
Expand All @@ -15,39 +16,30 @@ use Ash.Resource,
end
```

This will generate the version resource automatically and add them to your api. The autogenerated resource will be named `Version` under the namespace of the original resource and will belong to the original resource. For example, if your original resource is `MyApp.Post` the autogenerated resource will be `MyApp.Post.Version`. `Post` has_many `paper_trail_versions` and `Version` belong_to `source_version`
This will generate the version resource automatically and add them to your domain. The autogenerated resource will be named `Version` under the namespace of the original resource and will belong to the original resource. For example, if your original resource is `MyApp.Post` the autogenerated resource will be `MyApp.Post.Version`. `Post` has_many `paper_trail_versions` and `Version` belong_to `source_version`

Then for each Api for versioned resources, you need the versioned resource to the Api of the original non-versioned resource's api. There are a few ways to achieve this.
Then for each Domain for versioned resources, you need the versioned resource to the Domain of the original non-versioned resource's domain. There are a few ways to achieve this.

If your Api doesn't use a registry and you're declaring your resources directly in your Api (this is strongly preferred), add the `AshPaperTrail.Api` extension to api.
Add the `AshPaperTrail.Domain` extension to your domain.

```elixir
use Ash.Api,
use Ash.Domain,
extensions: [
AshPaperTrail.Api
AshPaperTrail.Domain
]
```

This approach does not add the resources to the api, but does allow them to be accessed via the api. In most use cases, this is acceptable; however, in some use cases you'll need each resource to be a part of the api--if your versioned resources are part of a graphql. If that's the case, add each versioned resource explicitly to your api's resources.
In some use cases you'll need each resource to be a part of the domain--if your versioned resources are part of a graphql. If that's the case, add each versioned resource explicitly to your domain's resources.

```
use Ash.Api
use Ash.Domain
resources do
resource MyApp.Post
resource MyApp.Post.Version
end
```

If the Api uses a registry, consider removing the registry and going with one the options above. This third option is deprecated and only for backwards compatibility. Add the `AshPaperTrail.Registry` extension to the registry.

```elixir
use Ash.Registry,
extensions: [
AshPaperTrail.Registry
]
```

## Destroy Actions

If you are using `AshPostgres`, and you want to support destroy actions, you need to do one of two things:
Expand Down Expand Up @@ -108,8 +100,8 @@ You can record the actor who made the change by declaring one or more resources

```
paper_trail do
belongs_to_actor :user, MyApp.Accounts.User, api: MyApp.Accounts
belongs_to_actor :news_feed, MyApp.Accounts.NewsFeed, api: MyApp.Accounts
belongs_to_actor :user, MyApp.Accounts.User, domain: MyApp.Accounts
belongs_to_actor :news_feed, MyApp.Accounts.NewsFeed, domain: MyApp.Accounts
end
```

Expand Down
10 changes: 0 additions & 10 deletions lib/api/api.ex

This file was deleted.

10 changes: 10 additions & 0 deletions lib/domain/domain.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule AshPaperTrail.Domain do
@moduledoc """
Documentation for `AshPaperTrail.Domain`.
"""

use Spark.Dsl.Extension,
transformers: [
AshPaperTrail.Domain.Transformers.AllowResourceVersions
]
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule AshPaperTrail.Api.Transformers.AllowResourceVersions do
defmodule AshPaperTrail.Domain.Transformers.AllowResourceVersions do
@moduledoc """
Adds any version resources to the api for any resources. An alternative to
AshPaperTrail.Api.Transfomers.AllowResourceVersions.
Adds any version resources to the domain for any resources.
"""

use Spark.Dsl.Transformer
Expand All @@ -10,7 +9,7 @@ defmodule AshPaperTrail.Api.Transformers.AllowResourceVersions do
def before?(_), do: false

def transform(dsl_state) do
existing_allow_mfa = Ash.Api.Info.allow(dsl_state)
existing_allow_mfa = Ash.Domain.Info.allow(dsl_state)

{:ok,
Transformer.set_option(
Expand Down
7 changes: 0 additions & 7 deletions lib/registry/registry.ex

This file was deleted.

34 changes: 0 additions & 34 deletions lib/registry/transformers/add_resource_versions.ex

This file was deleted.

15 changes: 11 additions & 4 deletions lib/resource/belongs_to_actor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ defmodule AshPaperTrail.Resource.BelongsToActor do

defstruct [
:allow_nil?,
:api,
:domain,
:attribute_type,
:destination,
:define_attribute?,
:public?,
:name
]

@type t :: %__MODULE__{
allow_nil?: boolean,
api: atom,
public?: boolean,
domain: atom,
attribute_type: term,
destination: Ash.Resource.t(),
define_attribute?: boolean,
Expand All @@ -31,17 +33,22 @@ defmodule AshPaperTrail.Resource.BelongsToActor do
doc:
"Whether this relationship must always be present, e.g: must be included on creation, and never removed (it may be modified). The generated attribute will not allow nil values."
],
api: [
domain: [
type: :atom,
doc: """
The API module to use when working with the related entity.
The Domain module to use when working with the related entity.
"""
],
attribute_type: [
type: :any,
default: Application.compile_env(:ash, :default_belongs_to_type, :uuid),
doc: "The type of the generated created attribute. See `Ash.Type` for more."
],
public?: [
type: :boolean,
default: false,
doc: "Whether this relationship should be included in public interfaces"
],
define_attribute?: [
type: :boolean,
default: true,
Expand Down
6 changes: 4 additions & 2 deletions lib/resource/changes/create_new_version.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ defmodule AshPaperTrail.Resource.Changes.CreateNewVersion do
|> Ash.Changeset.for_create(:create, input,
tenant: changeset.tenant,
authorize?: false,
actor: actor
actor: actor,
domain: changeset.domain,
skip_unknown_inputs: Map.keys(input)
)
|> Ash.Changeset.force_change_attributes(Map.take(private, version_resource_attributes))
|> changeset.api.create!(return_notifications?: true)
|> Ash.create!(return_notifications?: true)

notifications
end
Expand Down
2 changes: 1 addition & 1 deletion lib/resource/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule AshPaperTrail.Resource do
The actor on the version changeset is set.
""",
examples: [
"belongs_to_actor :user, MyApp.Users.User, api: MyApp.Users"
"belongs_to_actor :user, MyApp.Users.User, domain: MyApp.Users"
],
no_depend_modules: [:destination],
target: AshPaperTrail.Resource.BelongsToActor,
Expand Down
17 changes: 13 additions & 4 deletions lib/resource/transformers/create_version_resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ defmodule AshPaperTrail.Resource.Transformers.CreateVersionResource do
quote do
use Ash.Resource,
unquote(
Keyword.put(version_extensions, :data_layer, data_layer)
|> Keyword.put(:validate_api_inclusion?, false)
version_extensions
|> Keyword.put(:data_layer, data_layer)
|> Keyword.put(:domain, Ash.Resource.Info.domain(dsl_state))
|> Keyword.put(:validate_domain_inclusion?, false)
)

def resource_version?, do: true
Expand Down Expand Up @@ -145,11 +147,13 @@ defmodule AshPaperTrail.Resource.Transformers.CreateVersionResource do
attribute :version_action_type, :atom do
constraints(one_of: [:create, :update, :destroy])
allow_nil?(false)
public? true
end

if unquote(store_action_name?) do
attribute :version_action_name, :atom do
allow_nil?(false)
public? true
end
end

Expand All @@ -158,7 +162,7 @@ defmodule AshPaperTrail.Resource.Transformers.CreateVersionResource do
allow_nil?(attr.allow_nil?)
generated?(attr.generated?)
primary_key?(attr.primary_key?)
private?(attr.private?)
public?(attr.public?)
writable?(true)
default(attr.default)
description(attr.description || "")
Expand All @@ -170,10 +174,12 @@ defmodule AshPaperTrail.Resource.Transformers.CreateVersionResource do

attribute :version_source_id, unquote(Macro.escape(destination_attribute.type)) do
constraints unquote(Macro.escape(destination_attribute.constraints))
public? true
allow_nil? false
end

attribute :changes, :map do
public? true
sensitive? unquote(sensitive_changes?)
end

Expand All @@ -182,11 +188,13 @@ defmodule AshPaperTrail.Resource.Transformers.CreateVersionResource do
end

actions do
default_accept :*
defaults([:create, :read, :update])
end

relationships do
belongs_to :version_source, unquote(module) do
public? true
destination_attribute(unquote(destination_attribute.name))
allow_nil?(false)
attribute_writable?(true)
Expand All @@ -195,10 +203,11 @@ defmodule AshPaperTrail.Resource.Transformers.CreateVersionResource do

for actor_relationship <- unquote(Macro.escape(belongs_to_actors)) do
belongs_to actor_relationship.name, actor_relationship.destination do
api(actor_relationship.api)
domain(actor_relationship.domain)
define_attribute?(actor_relationship.define_attribute?)
allow_nil?(actor_relationship.allow_nil?)
attribute_type(actor_relationship.attribute_type)
public?(actor_relationship.public?)
attribute_writable?(true)
end
end
Expand Down
9 changes: 5 additions & 4 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ defmodule AshPaperTrail.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:ash, "~> 2.18"},
# {:ash, "~> 3.0.0-rc.5"},
{:ash, path: "../ash", override: true},
{:ex_doc, "~> 0.22", only: [:dev, :test], runtime: false},
{:ex_check, "~> 0.12", only: [:dev, :test]},
{:credo, ">= 0.0.0", only: [:dev, :test], runtime: false},
Expand All @@ -107,11 +108,11 @@ defmodule AshPaperTrail.MixProject do
],
credo: "credo --strict",
"spark.formatter":
"spark.formatter --extensions AshPaperTrail.Resource,AshPaperTrail.Registry,AshPaperTrail.Api",
"spark.formatter --extensions AshPaperTrail.Resource,AshPaperTrail.Domain",
"spark.cheat_sheets_in_search":
"spark.cheat_sheets_in_search --extensions AshPaperTrail.Resource,AshPaperTrail.Registry,AshPaperTrail.Api",
"spark.cheat_sheets_in_search --extensions AshPaperTrail.Resource,AshPaperTrail.Domain",
"spark.cheat_sheets":
"spark.cheat_sheets --extensions AshPaperTrail.Resource,AshPaperTrail.Registry,AshPaperTrail.Api"
"spark.cheat_sheets --extensions AshPaperTrail.Resource,AshPaperTrail.Domain"
]
end
end
Loading

0 comments on commit bd0f1f8

Please sign in to comment.