Skip to content

tienle/feature_toggle

Repository files navigation

Feature Toggle

Introduction

This gem allows to manage (enable/disable) application features on the fly depending on predefined states (e.g beta, paid user). There is a UI for toggling the relation (feature matrix) between features and states.

![Image of UI] (http://i.imgur.com/FC8zrvC.png)

Terms:

feature: Define a feature

state: Define a state (beta, paid user, etc...)

Usage

1) Install the Gem

Add to your Gemfile:

  gem 'feature_toggle', github: 'tienle/feature_toggle'

2) Configuration

Mount the feature management page to routes.rb

  mount FeatureToggle::Engine => "/feature_toggle"

Add an "feature_toggle.rb" initializer

Setup the feature toggle page

  FeatureToggle.setup do |config|
    config.layout = 'admin'
    config.authentication_method = :authenticate_admin!
  end

Describe the different features and states

  FeatureToggle.define do
    feature :employment_settings do
      desc "Employment settings"
    end

    feature :employee_file do
      desc "Access employee file"

      feature :create
      feature :destroy
    end

    state :premium do
      desc "Premium plan"
      value { current_user.premium? }
    end

    state :free do
      desc "Trial user"
      value { current_user.free? }
    end
  end

3) Usage

Include this mixins into your application_controller.rb

class ApplicationController < ActionController::Base
  include FeatureToggle::Controller
end

From the view (eg. Haml)

  - if feature? :employee_file
    = render 'employee_file_partial'

Or

FeatureToggle.on?(:employee_file, state_context)

With state_context is the self binding for state's value block.

So, the feature will be enabled if we have turned on it for one of the states that meet the value condition.

From the controller:

  class EmployeesController < ApplicationController
    require_feature! :employee_file
  end

It will raise an exception FeatureToggle::NotAuthorizedError if there are no states matched.

You could toggle feature programmatically by:

  FeatureToggle::Feature.create(feature: 'employee_file', state: 'premium', enable: true)

Happy coding!


This project rocks and uses MIT-LICENSE.

About

Declare and manage feature flags easily

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published