-
Notifications
You must be signed in to change notification settings - Fork 4
/
assoc_model.ex
78 lines (73 loc) · 1.75 KB
/
assoc_model.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
defmodule FatEcto.AssocModel do
def has_and_many_to_many(model) do
Enum.filter(all(model), fn association ->
case association do
%Ecto.Association.Has{} -> true
%Ecto.Association.ManyToMany{} -> true
_ -> false
end
end)
end
def has_only(model) do
Enum.filter(all(model), fn association ->
case association do
%Ecto.Association.Has{} -> true
_ -> false
end
end)
end
def all(model) do
Enum.reduce(model.__schema__(:associations), [], fn relation_name, acc ->
acc ++ [model.__schema__(:association, relation_name)]
end)
end
end
# Example of each association type
# %Ecto.Association.Has{
# cardinality: :many,
# defaults: [],
# field: :users,
# on_cast: nil,
# on_delete: :nothing,
# on_replace: :raise,
# owner: HaiData.Customer,
# owner_key: :id,
# queryable: HaiData.User,
# related: HaiData.User,
# related_key: :customer_id,
# relationship: :child,
# unique: true,
# where: []
# }
# %Ecto.Association.BelongsTo{
# cardinality: :one,
# defaults: [],
# field: :business_contact,
# on_cast: nil,
# on_replace: :raise,
# owner: HaiData.Customer,
# owner_key: :business_contact_id,
# queryable: HaiData.Contact,
# related: HaiData.Contact,
# related_key: :id,
# relationship: :parent,
# unique: true,
# where: []
# }
# %Ecto.Association.ManyToMany{
# cardinality: :many,
# defaults: [],
# field: :users,
# join_keys: [facility_id: :id, user_id: :id],
# join_through: "users_facilities",
# on_cast: nil,
# on_delete: :nothing,
# on_replace: :raise,
# owner: HaiData.Facility,
# owner_key: :id,
# queryable: HaiData.User,
# related: HaiData.User,
# relationship: :child,
# unique: false,
# where: []
# }