Implementing Feature Flags in Ruby on Rails with Flipper

Photo by Kevin Ku on Unsplash

Implementing Feature Flags in Ruby on Rails with Flipper

Learn How to Use Feature Flags in Ruby on Rails for Better Feature Management with Flipper

Please visit Feature Flags if you want to know what a feature flag is and what are the benefits of using a feature flag.

This article will explain how to implement Feature Flags in Ruby on Rails using Flipper gem. It will cover different use cases for feature flags, as well as best practices for managing and testing them.

Setting up flipper

If you don't have a Rails app, you can generate one quickly using rails new my_app. Now open the Gemfile and add the following line to the setup:

gem "flipper-cloud"
gem "flipper-active_record"

Run bundle to install them

bundle install

If you're using the ActiveRecord adapter, you'll also need to generate the migrations and run them.

bin/rails g flipper:active_record
bin/rails db:migrate

This will generate a migration file that includes the required database table so that Flipper can work.

How to use Flipper in your project

After the setup is completed you can now use Flipper as a feature flag for your features. Some of the examples of features we have used Flipper with are:

  • Sync Invoices To QuickBooks: We used Flipper to enable our Invoice sync with QuickBooks invoices as a beta feature for our certain beta customers. This way other customers who don't want to sync invoices will still use the app.

  • Multiple Version of Dashboards for A/B testing: Flipper also has a great way to enable certain feature flags to a certain percentage of users.

  • Feature Flags for Custom Reporting: Some customer wants their reports in a specific format and those reports should be only accessible to them.

  • Continuous Deployment of new big feature: If your company follows a continuous deployment and DORA metrics, feature flags are very important to do risk-free continuous delivery of your app without breaking them in production. And you can allow your QA team or stakeholders to test them before even your full feature is completed.

These are mostly our use cases but they can differ based on how your team delivers the product to the end users.

Flipper makes it dead simple to enable/disable features for your app, for example:

class InvoiceSyncFeature
    def run
        return unless Flipper.enabled?(:qb_sync, current_user)


How to enable the Flipper feature flags?

You can enable Flipper feature flags for different actors, most of the rails applications will have user as an actor.

Here is how you can enable it:

# Enable a feature for everyone
Flipper.enable :qb_sync

# Enable a feature for a specific actor
Flipper.enable_actor :qb_sync, current_user

# Enable a feature for a group of actors
Flipper.enable_group :qb_sync, :admin

# Enable a feature for a percentage of actors
Flipper.enable_percentage_of_actors :qb_sync, 2

Flipper UI

If you want to manage your feature flags from UI you can use:

gem 'flipper-ui'

Install it with bundle install

And mount it into your config/routes.rb file

YourRailsApp::Application.routes.draw do
  mount => '/flipper'

Securing your flipper routes

Obviously, we don't want to leave the flipper open for everyone and mess around with it. For that, we are using the Devise gem and we only allow admins to access it. In our case, admins are our site owners.

authenticated do
    mount => '/flipper', constraints: lambda { |req|
      current_user = req.env['warden'].user
      current_user.present? && current_user.admin?

Or you can use basic HTTP authentication via Rack

# config/routes.rb

flipper_app = do |builder|
  builder.use Rack::Auth::Basic do |username, password|
    # Verify credentials
mount flipper_app, at: '/flipper'

Happy Hacking ;)