-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
has_one.ex
83 lines (77 loc) · 2.41 KB
/
has_one.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
defmodule Ash.Resource.Relationships.HasOne do
@moduledoc "Represents a has_one relationship on a resource"
defstruct [
:name,
:source,
:destination,
:destination_attribute,
:private?,
:source_attribute,
:allow_orphans?,
:context,
:description,
:filter,
:api,
:sort,
:read_action,
:not_found_message,
:violation_message,
:manual,
:writable?,
from_many?: false,
no_attributes?: false,
could_be_related_at_creation?: false,
validate_destination_attribute?: true,
cardinality: :one,
type: :has_one,
allow_nil?: false,
filterable?: true
]
@type t :: %__MODULE__{
type: :has_one,
cardinality: :one,
source: Ash.Resource.t(),
name: atom,
filterable?: boolean,
from_many?: boolean,
read_action: atom,
no_attributes?: boolean,
writable?: boolean,
type: Ash.Type.t(),
filter: Ash.Filter.t() | nil,
destination: Ash.Resource.t(),
destination_attribute: atom,
private?: boolean,
source_attribute: atom,
allow_orphans?: boolean,
description: String.t(),
manual: atom | {atom, Keyword.t()} | nil
}
import Ash.Resource.Relationships.SharedOptions
alias Spark.OptionsHelpers
@global_opts shared_options()
|> OptionsHelpers.set_default!(:source_attribute, :id)
@opt_schema Spark.OptionsHelpers.merge_schemas(
[manual(), no_attributes()] ++
[
allow_nil?: [
type: :boolean,
default: true,
doc: """
Marks the relationship as required. Has no effect on validations, but can inform extensions that there will always be a related entity.
"""
],
from_many?: [
type: :boolean,
default: false,
doc: """
Signal that this relationship is actually a `has_many` where the first record is given via the `sort`. This will allow data layers to properly deduplicate when necessary.
"""
]
],
@global_opts,
"Relationship Options"
)
@doc false
def opt_schema, do: @opt_schema
end