/
plug_helpers.ex
148 lines (111 loc) · 4.41 KB
/
plug_helpers.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
if Code.ensure_loaded?(Plug.Conn) do
defmodule Ash.PlugHelpers do
@moduledoc """
Helpers for working with the Plug connection.
"""
alias Plug.Conn
require Logger
defmacrop emit_assign_warning(type) do
quote do
Logger.warning(fn ->
{fun, arity} = __ENV__.function
file = Path.relative_to_cwd(__ENV__.file)
"""
Storing the #{unquote(type)} in conn assigns is deprecated.
#{file}:#{__ENV__.line}: #{inspect(__ENV__.module)}.#{fun}/#{arity}
"""
end)
end
end
@doc """
Sets the actor inside the Plug connection.
The actor is stored inside the [connection's private
fields](https://hexdocs.pm/plug/Plug.Conn.html#module-private-fields).
## Example
iex> actor = build_actor(%{email: "marty@1985.retro"})
...> conn = build_conn() |> set_actor(actor)
%Plug.Conn{private: %{ash: %{actor: %{email: "marty@1985.retro"}}}} = conn
"""
@spec set_actor(Conn.t(), Ash.Resource.record()) :: Conn.t()
def set_actor(conn, actor) do
ash_private =
conn.private
|> Map.get(:ash, %{})
|> Map.put(:actor, actor)
conn
|> Conn.put_private(:ash, ash_private)
end
@doc """
Retrieves the actor from the Plug connection.
The actor is stored inside the [connection's private
fields](https://hexdocs.pm/plug/Plug.Conn.html#module-private-fields).
## Deprecation warning
This function checks to see if the actor is already set in the `@actor`
assign, and if so will emit a deprecation warning.
This is to allow apps using the previous method a chance to update.
Rather than setting the actor in the assigns, please use the `set_actor/2`
method.
## Example
iex> actor = build_actor(%{email: "marty@1985.retro"})
...> conn = build_conn() |> put_private(:ash, %{actor: actor})
...> actor = get_actor(conn)
%{email: "marty@1985.retro"} = actor
iex> actor = build_actor(%{email: "marty@1985.retro"})
...> conn = build_conn() |> assign(:actor, actor)
...> actor = get_actor(conn)
%{email: "marty@1985.retro"} = actor
"""
@spec get_actor(Conn.t()) :: nil | Ash.Resource.record()
def get_actor(%{assigns: %{actor: actor}}) when not is_nil(actor) do
emit_assign_warning(:actor)
actor
end
def get_actor(%{private: %{ash: %{actor: actor}}}), do: actor
def get_actor(_), do: nil
@doc """
Sets the tenant inside the Plug connection.
The tenant is stored inside the [connection's private
fields](https://hexdocs.pm/plug/Plug.Conn.html#module-private-fields).
## Example
iex> tenant = build_tenant(%{name: "Deliver-yesterday"})
...> conn = build_conn() |> set_tenant(tenant)
%Plug.Conn{private: %{ash: %{tenant: %{name: "Deliver-yesterday"}}}} = conn
"""
@spec set_tenant(Conn.t(), Ash.Resource.record()) :: Conn.t()
def set_tenant(conn, tenant) do
ash_private =
conn.private
|> Map.get(:ash, %{})
|> Map.put(:tenant, tenant)
conn
|> Conn.put_private(:ash, ash_private)
end
@doc """
Retrieves the tenant from the Plug connection.
The tenant is stored inside the [connection's private
fields](https://hexdocs.pm/plug/Plug.Conn.html#module-private-fields).
## Deprecation warning
This function checks to see if the tenant is already set in the `@tenant`
assign, and if so will emit a deprecation warning.
This is to allow apps using the previous method a chance to update.
Rather than setting the tenant in the assigns, please use the `set_tenant/2`
method.
## Example
iex> tenant = build_tenant(%{name: "Deliver-yesterday"})
...> conn = build_conn() |> put_private(:ash, %{tenant: tenant})
...> tenant = get_tenant(conn)
%{name: "Deliver-yesterday"} = tenant
iex> tenant = build_tenant(%{name: "Deliver-yesterday"})
...> conn = build_conn() |> assign(:tenant, tenant)
...> tenant = get_tenant(conn)
%{name: "Deliver-yesterday"} = tenant
"""
@spec get_tenant(Conn.t()) :: nil | Ash.Resource.record()
def get_tenant(%{assigns: %{tenant: tenant}}) when not is_nil(tenant) do
emit_assign_warning(:tenant)
tenant
end
def get_tenant(%{private: %{ash: %{tenant: tenant}}}), do: tenant
def get_tenant(_), do: nil
end
end