8000 Fix Ash.Resource.Info.public_relationship/2 by adonig · Pull Request #1000 · ash-project/ash · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix Ash.Resource.Info.public_relationship/2 #1000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/ash/resource/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ defmodule Ash.Resource.Info do
end

@doc "Get a public relationship by name or path"
def public_relationship(resource, [name]) do
public_relationship(resource, name)
end

def public_relationship(resource, [name | rest]) do
case public_relationship(resource, name) do
nil ->
Expand Down
32 changes: 26 additions & 6 deletions test/resource/info_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule Ash.Test.Resource.InfoTest do
@moduledoc false
use ExUnit.Case, async: true

require Spark.Dsl
require Spark.Dsl.Extension
alias Ash.Resource
alias Ash.Resource.Info
alias Ash.Test.Domain, as: Domain
Expand Down Expand Up @@ -39,15 +41,20 @@ defmodule Ash.Test.Resource.InfoTest do
end

relationships do
many_to_many :tags, Tag do
through TagOnPost
many_to_many :tags, Ash.Test.Resource.InfoTest.Tag do
through Ash.Test.Resource.InfoTest.TagOnPost
public?(true)
source_attribute_on_join_resource(:vendor_id)
destination_attribute_on_join_resource(:shipping_method_id)
source_attribute_on_join_resource(:tag_id)
destination_attribute_on_join_resource(:post_id)
end

has_many :comments, Comment, destination_attribute: :post_id, public?: true
has_one :private, PostPrivate, destination_attribute: :post_id, public?: true
has_many :comments, Ash.Test.Resource.InfoTest.Comment,
destination_attribute: :post_id,
public?: true

has_one :private, Ash.Test.Resource.InfoTest.PostPrivate,
destination_attribute: :post_id,
public?: true
end
end

Expand Down Expand Up @@ -199,5 +206,18 @@ defmodule Ash.Test.Resource.InfoTest do
test "get datalayer from resource" do
assert Ash.DataLayer.Ets == Info.data_layer(Post)
end

test "get relationship fields" do
assert Spark.Dsl.Extension.get_entities(Comment, [:post])

comment = Info.public_relationship(Post, [:comments]).destination
assert Spark.Dsl.Extension.get_entities(comment, [:post])

assert %Resource.Relationships.ManyToMany{name: :tags} =
Info.public_relationship(Post, :tags)

assert %Resource.Relationships.BelongsTo{name: :post} =
Info.public_relationship(Post, [:comments, :post])
end
end
end
0