Skip to content

Feature Flags

Kevin edited this page May 21, 2024 · 4 revisions

Feature flags allow us to enable/disable features on the site.

Adding a feature flag

To add a feature flag, use this code:

Feature.enabled?(:my_feature)

When this code is executed, a new feature will be added to Flipper, the library we use for feature flags. The feature will be disabled by default.

Using Feature flags

Feature flags can be used anywhere, but you'll find yourself mostly using them in controllers and views.

Controller example:

class MyController < ApplicationController
  def show
   if Feature.enabled?(:my_feature)
     # do something
   else
     redirect_to home_path, notice: "Feature not enabled"
   end
end

View example:

<% if Feature.enabled?(:my_feature) do %>
  <p>My feature is enabled!</p>
<% end %>

Enabling/Disabling

As mentioned previously, feature flags are disabled by default. You need to manually enable them though the admin section of the site.

On your local environment, do the following:

  1. Log in using our dummy admin account
  2. Visit http://localhost:3000/admin
  3. Hover over the user icon in the top right hand corner of the navbar and click "FEATURE FLAGS" on the dropdown that appears
  4. Click the "Add feature" button
  5. In the input, enter the same name as you used for the feature in the code and click "Add feature".
    • Given Feature.enabled?(:my_feature)
    • Enter "my_feature"

From here, you have a couple of options...

Enabling the feature for everyone

Clicking "Fully enable" will enable the feature for everyone on the site

Screenshot 2024-05-21 at 18 28 51

After it is fully enabled, you can toggle it off again at any point:

Screenshot 2024-05-21 at 18 30 17

Enabling the feature for a percentage of users

If you want to roll your feature out slowly to gauge feedback without affecting everyone on the site. You can enable it for a percentage of users using "Enabled for X% of actors".

Screenshot 2024-05-21 at 18 32 06