By Leonard Garvey

Feature Flags on Rails

During the previous post I looked at what feature flags are and why you'd want to use them. In this post I will explore how I've implemented them in the current web-application I'm working on. It's a very simple process which means there are no excuses not to use a feature flag in your application.

on rails

everything is easier on Rails

The Rollout gem

I use the Rollout gem as a way of implementing Feature flags. It's pretty easy to use although there are a few things I don't like about it. The important things are:

  1. It relies on redis which I already have in my stack.
  2. It doesn't have a user-interface so you must enable features with the production console.

Here's how you'd use rollout:

if $rollout.active? :my_cool_feature
  my_cool_feature
end

Then you'd be able to activate that feature on the console by typing:

$rollout.activate :my_cool_feature

Activating a single user or a group

Often you'll want to activate a single user or a group of users. To do this we need to modify our feature flag invocation as follows:

if $rollout.active? :my_cool_feature, current_user
  my_cool_feature
end

Then you could activate it for a specific user as follows:

$rollout.activate_user :my_cool_feature, User.find(1)

Alternatively you can define a group of users:

# config/initializers/rollout.rb

$rollout.define_group(:staff) do |user|
  user.staff?
end

Then you can specify a group to activate in the console instead:

$rollout.activate_group :my_cool_feature, :staff

Global variables are yucky

Personally I don't like litering my codebase with $global $variables. Also it's normally useful that feature flags are always enabled in development and test environments. What I do is wrap Rollout in a very small class:

class Feature
  def self.active?(*args)
    (!feature_flags_enabled?) || $rollout.active?(*args) rescue true
  end

  def self.feature_flags_enabled?
    Rails.env.production? || Rails.env.staging?
  end
end

This lets me use Feature.active? in my code which I believe is more intention-revealing than $rollout.active? and more aesthetically pleasing.

Ready to Rollout?

rolling out

Rollout has other features (like enabling flags based on percentages) and the README has links detailing how to automatically disable features based on errors. The reason I like Rollout is that it's very simple to get going, and integrates easily into your own stack (assuming you have Redis).

There are alternative libraries you can use such as Flip which have different features such as ActiveRecord support, or a web-dashboard for enabling/disabling flags.

Latest Articles by Our Team

Our expert team of designers and developers love what the do and enjoy sharing their knowledge with the world.

We Hire Only the Best

reinteractive is Australia’s largest dedicated Ruby on Rails development company. We don’t cut corners and we know what we are doing.

We are an organisation made up of amazing individuals and we take pride in our team. We are 100% remote work enabling us to choose the best talent no matter which part of the country they live in. reinteractive is dedicated to making it a great place for any developer to work.

Free Community Workshops

We created the Ruby on Rails InstallFest and Ruby on Rails Development Hub to help introduce new people to software development and to help existing developers hone their skills. These workshops provide invaluable mentorship to train developers, addressing key skills shortages in the industry. Software development is a great career choice for all ages and these events help you get started and skilled up.

  • Installfest

    Installfest

    The Ruby on Rails Installfest includes a full setup of your development environment and step-by-step instructions on how to build your first app hosted on Heroku. Over 1,800 attendees to date and counting.

    Learn more about Installfest

  • Development Hub

    Development Hub

    The Ruby on Rails Development Hub is a monthly event where you will get the chance to spend time with our team and others in the community to improve and hone your Ruby on Rails skills.

    Learn more about Development Hub

  • Webinars

    Webinars

    Webinars are our online portal for tips, tricks and lessons learned in everything we do. Make the most of this free resource to help you become a better developer.

    Learn more about webinars

Next Community Events

Find more events

Get the “reinteractive Review” Monthly Email