View Source Unleash.Plug (Unleash v1.10.0)

An extra fancy Plug and utility functions to help when developing Plug or Phoenix-based applications. It automatically puts together a Unleash.context/0 under the Plug.Conn's Plug.assigns/0.

To use, call plug Unleash.Plug in your plug pipeline. It depends on the session, and requires being after :fetch_session to work. It accepts the following options:

  • :user_id: The key under which the user's ID is found in the session.
  • :session_id: The key under wwhich the session ID is found in the session.

After which, enabled?/3 is usable.

If you are also using PhoenixGon in your application, you can call put_feature/3 to put a specific feature flag into the gon object for using on the client side.

Summary

Functions

Given a t:Plug.Conn/0, a feature, and (optionally) a boolean, return whether or not a feature is enabled. This requires this plug to be a part of the plug pipeline, as it will construct an Unleash.context()/0 out of the session.

If you are using PhoenixGon, you can call this to put a feature in the gon object to be used on the client side. It will be available under window.Gon.getAssets('features'). It listens to the options that are configured by PhoenixGon.Pipeline.

Functions

Link to this function

enabled?(conn, feature, default \\ false)

View Source
@spec enabled?(Plug.Conn.t(), String.t() | atom(), boolean()) :: boolean()

Given a t:Plug.Conn/0, a feature, and (optionally) a boolean, return whether or not a feature is enabled. This requires this plug to be a part of the plug pipeline, as it will construct an Unleash.context()/0 out of the session.

Examples

iex> Unleash.Plug.enabled?(conn, :test)
false

iex> Unleash.Plug.enabled?(conn, :test, true)
true
Link to this function

put_feature(conn, feature, default \\ false)

View Source
@spec put_feature(Plug.Conn.t(), String.t() | atom(), boolean()) :: Plug.Conn.t()

If you are using PhoenixGon, you can call this to put a feature in the gon object to be used on the client side. It will be available under window.Gon.getAssets('features'). It listens to the options that are configured by PhoenixGon.Pipeline.

Examples

iex> Unleash.Plug.put_feature(conn, :test)
%Plug.Conn{}

iex> Unleash.Plug.enabled?(conn, :test, true)
%Plug.Conn{}