Using Feature Flags to Manage App Updates with Rails and Mongoid

When you have a new feature for your web application, you frequently want to manage how it is rolled out. It’s always a good idea to only release the feature to a small group of customers as this will help you make sure that it is working as it should.¬†Additionally, you can use feature flags to manage access to applications capabilities at various plan levels.

The FeatureFlags Concern

It’s a good idea to create the feature flag capability as a concern so that it’s easy to use in multiple models.

[gist id=7118243]

An important detail to note is the save parameter on enable_feature and disable_feature. This lets us easily enable/disable multiple features without saving to the database after each one in enable_features and disable_features.

Adding FeatureFlags to a Model

With the concern, it’s easy to feature flags to a model.

[gist id=7118457]

Of course, this is a very simple Account model and I’m excluding any validation or error checking on account creation.

Checking for a Feature

Now that we have feature flags available, you can use them in your code to manage access to new features.

[gist id=7118561]

Checking for a Feature in a View

I’ve found it helpful to be able to check for a feature inside views to change the UI based on feature available. This is most easily handled via a helper.

[gist id=7118639]

Now, in a view, you can easily test for the feature.

[gist id=7118689]

In Closing

Feature flags give you a nice way to control access to a particular new feature or capability in your app. Next week, we’ll look at remotely updating features in staging or production with Capistrano.