View Source Ash.PlugHelpers (ash v3.4.46)

Helpers for working with the Plug connection.

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.

Functions

get_actor(arg1)

@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

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

get_context(arg1)

@spec get_context(Plug.Conn.t()) :: nil | map()

Retrieves the context from the Plug connection.

The context is stored inside the connection's private fields.

Example

iex> context = %{fraud_score: 0.427}
...> conn = build_conn() |> put_private(:ash, %{context: context})
...> context = get_context(conn)
%{fraud_score: 0.427}

get_tenant(arg1)

@spec get_tenant(Plug.Conn.t()) :: term()

Retrieves the tenant from the Plug connection.

The tenant is stored inside the connection's 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> conn = build_conn() |> put_private(:ash, %{tenant: "my-tenant"})
...> tenant = get_tenant(conn)
"my_tenant" = tenant

iex> conn = build_conn() |> assign(:tenant, "my-tenant")
...> tenant = get_tenant(conn)
"my_tenant" = tenant

set_actor(conn, actor)

@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

iex> actor = build_actor(%{email: "marty@1985.retro"})
...> conn = build_conn() |> set_actor(actor)
%Plug.Conn{private: %{ash: %{actor: %{email: "marty@1985.retro"}}}} = conn

set_context(conn, context)

@spec set_context(Plug.Conn.t(), map()) :: 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

iex> context = %{fraud_score: 0.427}
...> conn = build_conn() |> set_context(context)
%Plug.Conn{private: %{ash: %{context: %{fraud_score: 0.427}}}}

set_tenant(conn, tenant)

@spec set_tenant(Plug.Conn.t(), Ash.ToTenant.t()) :: Plug.Conn.t()

Sets the tenant inside the Plug connection.

The tenant is stored inside the connection's private fields.

Example

iex> conn = build_conn() |> set_tenant("my-tenant")
%Plug.Conn{private: %{ash: %{tenant: "my-tenant}}} = conn

update_actor(conn, callback)

@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

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

update_context(conn, callback)

@spec update_context(Plug.Conn.t(), (nil | map() -> nil | map())) :: Plug.Conn.t()

Updates the context inside the Plug connection.

The context is stored inside the connection's private fields.

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"}