-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
many_to_many.ex
91 lines (85 loc) · 3.03 KB
/
many_to_many.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
defmodule Ash.Resource.Relationships.ManyToMany do
@moduledoc "Represents a many_to_many relationship on a resource"
defstruct [
:name,
:source,
:through,
:destination,
:source_attribute,
:destination_attribute,
:source_attribute_on_join_resource,
:destination_attribute_on_join_resource,
:join_relationship,
:not_found_message,
:violation_message,
:api,
:private?,
:sort,
:read_action,
:description,
:context,
:filter,
:has_many,
filterable?: true,
could_be_related_at_creation?: false,
validate_destination_attribute?: true,
cardinality: :many,
type: :many_to_many
]
@type t :: %__MODULE__{
type: :many_to_many,
cardinality: :many,
source: Ash.Resource.t(),
has_many: boolean,
private?: boolean,
filter: Ash.Filter.t() | nil,
read_action: atom,
name: atom,
through: Ash.Resource.t(),
destination: Ash.Resource.t(),
filterable?: boolean,
join_relationship: atom,
source_attribute: atom,
destination_attribute: atom,
source_attribute_on_join_resource: atom,
destination_attribute_on_join_resource: atom,
description: String.t()
}
import Ash.Resource.Relationships.SharedOptions
alias Spark.OptionsHelpers
@global_opts shared_options()
|> OptionsHelpers.set_default!(:destination_attribute, :id)
|> OptionsHelpers.set_default!(:source_attribute, :id)
@opt_schema Spark.OptionsHelpers.merge_schemas(
[
source_attribute_on_join_resource: [
type: :atom,
doc:
"The attribute on the join resource that should line up with `source_attribute` on this resource. Defaults to `<snake_cased_last_part_of_source_module_name>_id`."
],
destination_attribute_on_join_resource: [
type: :atom,
doc:
"The attribute on the join resource that should line up with `destination_attribute` on the related resource. Defaults to `<snake_cased_last_part_of_destination_module_name>_id`."
],
through: [
type: Ash.OptionsHelpers.ash_resource(),
doc: "The resource to use as the join resource."
],
join_relationship: [
type: :atom,
doc:
"The has_many relationship to the join resource. Defaults to `<relationship_name>_join_assoc`."
]
],
@global_opts,
"Relationship Options"
)
@doc false
def opt_schema, do: @opt_schema
@doc false
# sobelow_skip ["DOS.BinToAtom"]
def transform(%{join_relationship: join_relationship, name: name} = relationship) do
{:ok, %{relationship | join_relationship: join_relationship || :"#{name}_join_assoc"}}
end
end