View Source Ash.PlugHelpers (ash v2.11.8)
Helpers for working with the Plug connection.
Link to this section Summary
Functions
Retrieves the actor from the Plug connection.
Retrieves the context from the Plug connection.
Retrieves the tenant from the Plug connection.
Sets the actor inside the Plug connection.
Sets the context inside the Plug connection.
Sets the tenant inside the Plug connection.
Updates the actor inside the Plug connection.
Updates the context inside the Plug connection.
Link to this section Functions
@spec get_actor(Plug.Conn.t()) :: nil | Ash.Resource.record()
Retrieves the actor from the Plug connection.
The actor is stored inside the connection's private fields.
deprecation-warning
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
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_context(Plug.Conn.t()) :: nil | Ash.Resource.record()
Retrieves the context from the Plug connection.
The context is stored inside the connection's private fields.
example
Example
iex> context = %{fraud_score: 0.427}
...> conn = build_conn() |> put_private(:ash, %{context: context})
...> context = get_context(conn)
%{fraud_score: 0.427}
@spec get_tenant(Plug.Conn.t()) :: nil | Ash.Resource.record()
Retrieves the tenant from the Plug connection.
The tenant is stored inside the connection's private fields.
deprecation-warning
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
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 set_actor(Plug.Conn.t(), Ash.Resource.record()) :: Plug.Conn.t()
Sets the actor inside the Plug connection.
The actor is stored inside the connection's private fields.
example
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_context(Plug.Conn.t(), Ash.Resource.record()) :: Plug.Conn.t()
Sets the context inside the Plug connection.
Context can be used to store abitrary data about the user, connection, or anything else you like that doesn't belong as part of the actor or tenant.
The context is stored inside the connection's private fields.
example
Example
iex> context = %{fraud_score: 0.427}
...> conn = build_conn() |> set_context(context)
%Plug.Conn{private: %{ash: %{context: %{fraud_score: 0.427}}}}
@spec set_tenant(Plug.Conn.t(), Ash.Resource.record()) :: Plug.Conn.t()
Sets the tenant inside the Plug connection.
The tenant is stored inside the connection's private fields.
example
Example
iex> tenant = build_tenant(%{name: "Deliver-yesterday"})
...> conn = build_conn() |> set_tenant(tenant)
%Plug.Conn{private: %{ash: %{tenant: %{name: "Deliver-yesterday"}}}} = conn
@spec update_actor( Plug.Conn.t(), (nil | Ash.Resource.record() -> nil | Ash.Resource.record()) ) :: Plug.Conn.t()
Updates the actor inside the Plug connection.
The actor is stored inside the connection's private fields.
example
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
...> conn = update_actor(conn, fn actor -> Map.put(actor, :name, "Marty Retro") end)
...> actor = get_actor(conn)
%{email: "marty@1985.retro", name: "Marty Retro"} = actor
...> conn = update_actor(conn, fn actor -> Map.delete(actor, :email) end)
...> actor = get_actor(conn)
%{name: "Marty Retro"} = actor
@spec update_context( Plug.Conn.t(), (nil | Ash.Resource.record() -> nil | Ash.Resource.record()) ) :: Plug.Conn.t()
Updates the context inside the Plug connection.
The context is stored inside the connection's private fields.
example
Example
iex> context = %{species: "Fythetropozoat"}
...> conn = build_conn() |> put_private(:ash, %{context: context})
...> context = get_context(conn)
%{fraud_score: 0.427}
...> conn = update_context(conn, fn context -> Map.put(context, :location, "Barnard's Loop") end)
...> context = get_context(conn)
%{species: "Fythetropozoat", location: "Barnard's Loop"}
...> conn = update_context(conn, fn context -> Map.delete(context, :fraud_score) end)
...> context = get_context(conn)
%{location: "Barnard's Loop"}