Skip to content

Commit

Permalink
improvement: support Ash 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Mar 30, 2024
1 parent 74f3085 commit 563d016
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 97 deletions.
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
5 changes: 3 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Config

config :ash, :validate_api_resource_inclusion?, false
config :ash, :validate_api_config_inclusion?, false
config :ash, :validate_domain_resource_inclusion?, false
config :ash, :validate_domain_config_inclusion?, false
config :ash, :missed_notifications, :ignore

if Mix.env() == :dev do
config :git_ops,
Expand Down
18 changes: 9 additions & 9 deletions lib/ash_csv/data_layer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ defmodule AshCsv.DataLayer do

defmodule Query do
@moduledoc false
defstruct [:resource, :sort, :filter, :limit, :offset, :api]
defstruct [:resource, :sort, :filter, :limit, :offset, :domain]
end

@impl true
Expand All @@ -130,8 +130,8 @@ defmodule AshCsv.DataLayer do
{:ok, results} ->
offset_records =
results
|> filter_matches(query.filter, query.api)
|> Sort.runtime_sort(query.sort, api: query.api)
|> filter_matches(query.filter, query.domain)
|> Sort.runtime_sort(query.sort, domain: query.domain)
|> Enum.drop(query.offset || 0)

if query.limit do
Expand Down Expand Up @@ -181,7 +181,7 @@ defmodule AshCsv.DataLayer do
query = Ash.Query.do_filter(resource, and: [key_filters])

resource
|> resource_to_query(changeset.api)
|> resource_to_query(changeset.domain)
|> Map.put(:filter, query.filter)
|> Map.put(:tenant, changeset.tenant)
|> run_query(resource)
Expand Down Expand Up @@ -248,8 +248,8 @@ defmodule AshCsv.DataLayer do
end

@impl true
def resource_to_query(resource, api) do
%Query{resource: resource, api: api}
def resource_to_query(resource, domain) do
%Query{resource: resource, domain: domain}
end

@impl true
Expand Down Expand Up @@ -287,10 +287,10 @@ defmodule AshCsv.DataLayer do
Process.get({:csv_in_transaction, file(resource)}, false) == true
end

def filter_matches(records, nil, _api), do: records
def filter_matches(records, nil, _domain), do: records

def filter_matches(records, filter, api) do
{:ok, records} = Ash.Filter.Runtime.filter_matches(api, records, filter)
def filter_matches(records, filter, domain) do
{:ok, records} = Ash.Filter.Runtime.filter_matches(domain, records, filter)
records
end

Expand Down
14 changes: 6 additions & 8 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,15 @@ defmodule AshCsv.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:ash, ash_version("~> 2.6")},
{:csv, "~> 2.4"},
{:ash, ash_version("~> 3.0.0-rc.0")},
{:csv, "~> 3.0"},
{:ex_doc, github: "elixir-lang/ex_doc", only: [:dev, :test], runtime: false},
{:ex_check, "~> 0.12.0", only: [:dev, :test]},
{:ex_check, "~> 0.12", only: [:dev, :test]},
{:credo, ">= 0.0.0", only: [:dev, :test], runtime: false},
{:dialyxir, ">= 0.0.0", only: [:dev, :test], runtime: false},
{:sobelow, ">= 0.0.0", only: [:dev, :test], runtime: false},
{:git_ops, "~> 2.0.1", only: [:dev, :test]},
{:excoveralls, "~> 0.13.0", only: [:dev, :test]}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
{:git_ops, "~> 2.5", only: [:dev, :test]},
{:excoveralls, "~> 0.13", only: [:dev, :test]}
]
end

Expand All @@ -124,7 +122,7 @@ defmodule AshCsv.MixProject do
docs: [
"spark.cheat_sheets",
"docs",
"ash.replace_doc_links",
"spark.replace_doc_links",
"spark.cheat_sheets_in_search"
],
"spark.formatter": "spark.formatter --extensions AshCsv.DataLayer",
Expand Down
51 changes: 21 additions & 30 deletions mix.lock

Large diffs are not rendered by default.

50 changes: 25 additions & 25 deletions test/ash_csv_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule AshCsvTest do
use ExUnit.Case, async: false
alias AshCsv.Test.{Api, Post}
alias AshCsv.Test.Post
require Ash.Query

setup do
Expand All @@ -11,66 +11,66 @@ defmodule AshCsvTest do

test "resources can be created" do
Post
|> Ash.Changeset.new(%{title: "title"})
|> Api.create!()
|> Ash.Changeset.for_create(:create, %{title: "title"})
|> Ash.create!()

assert [%{title: "title"}] = Api.read!(Post)
assert [%{title: "title"}] = Ash.read!(Post)
end

test "resources can be upserted" do
Post
|> Ash.Changeset.new(%{title: "title", unique: "foo"})
|> Api.create!()
|> Ash.Changeset.for_create(:create, %{title: "title", unique: "foo"})
|> Ash.create!()

Post
|> Ash.Changeset.new(%{title: "new_title", unique: "foo"})
|> Api.create!(upsert?: true, upsert_identity: :unique_unique)
|> Ash.Changeset.for_create(:create, %{title: "new_title", unique: "foo"})
|> Ash.create!(upsert?: true, upsert_identity: :unique_unique)

assert [%{title: "new_title"}] = Api.read!(Post)
assert [%{title: "new_title"}] = Ash.read!(Post)
end

test "a resource can be updated" do
post =
Post
|> Ash.Changeset.new(%{title: "title"})
|> Api.create!()
|> Ash.Changeset.for_create(:create, %{title: "title"})
|> Ash.create!()

post
|> Ash.Changeset.new(%{title: "new_title"})
|> Api.update!()
|> Ash.Changeset.for_update(:update, %{title: "new_title"})
|> Ash.update!()

assert [%{title: "new_title"}] = Api.read!(Post)
assert [%{title: "new_title"}] = Ash.read!(Post)
end

test "a resource can be deleted" do
post =
Post
|> Ash.Changeset.new(%{title: "title"})
|> Api.create!()
|> Ash.Changeset.for_create(:create, %{title: "title"})
|> Ash.create!()

Api.destroy!(post)
Ash.destroy!(post)

assert [] = Api.read!(Post)
assert [] = Ash.read!(Post)
end

test "filters/sorts can be applied" do
Post
|> Ash.Changeset.new(%{title: "title1"})
|> Api.create!()
|> Ash.Changeset.for_create(:create, %{title: "title1"})
|> Ash.create!()

Post
|> Ash.Changeset.new(%{title: "title2"})
|> Api.create!()
|> Ash.Changeset.for_create(:create, %{title: "title2"})
|> Ash.create!()

Post
|> Ash.Changeset.new(%{title: "title3"})
|> Api.create!()
|> Ash.Changeset.for_create(:create, %{title: "title3"})
|> Ash.create!()

results =
Post
|> Ash.Query.filter(title in ["title1", "title2"])
|> Ash.Query.sort(:title)
|> Api.read!()
|> Ash.read!()

assert [%{title: "title1"}, %{title: "title2"}] = results
end
Expand Down
7 changes: 4 additions & 3 deletions test/support/api.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
defmodule AshCsv.Test.Api do
defmodule AshCsv.Test.Domain do
@moduledoc false
use Ash.Api
use Ash.Domain

resources do
registry(AshCsv.Test.Registry)
resource(AshCsv.Test.Post)
resource(AshCsv.Test.Comment)
end
end
9 changes: 0 additions & 9 deletions test/support/registry.ex

This file was deleted.

6 changes: 4 additions & 2 deletions test/support/resources/comment.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule AshCsv.Test.Comment do
@moduledoc false
use Ash.Resource,
domain: AshCsv.Test.Domain,
data_layer: AshCsv.DataLayer

csv do
Expand All @@ -10,15 +11,16 @@ defmodule AshCsv.Test.Comment do
end

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

attributes do
uuid_primary_key(:id)
attribute(:title, :string)
attribute(:title, :string, public?: true)
end

relationships do
belongs_to(:post, AshCsv.Test.Post)
belongs_to(:post, AshCsv.Test.Post, public?: true)
end
end
12 changes: 7 additions & 5 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule AshCsv.Test.Post do
@moduledoc false
use Ash.Resource,
domain: AshCsv.Test.Domain,
data_layer: AshCsv.DataLayer

csv do
Expand All @@ -10,22 +11,23 @@ defmodule AshCsv.Test.Post do
end

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

attributes do
uuid_primary_key(:id)
attribute(:title, :string)
attribute(:score, :integer)
attribute(:public, :boolean)
attribute(:unique, :string)
attribute(:title, :string, public?: true)
attribute(:score, :integer, public?: true)
attribute(:public, :boolean, public?: true)
attribute(:unique, :string, public?: true)
end

identities do
identity(:unique_unique, [:unique])
end

relationships do
has_many(:comments, AshCsv.Test.Comment, destination_attribute: :post_id)
has_many(:comments, AshCsv.Test.Comment, destination_attribute: :post_id, public?: true)
end
end

0 comments on commit 563d016

Please sign in to comment.