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...)
Add to your Gemfile:
gem'feature_toggle',github: 'tienle/feature_toggle'Mount the feature management page to routes.rb
mountFeatureToggle::Engine=>"/feature_toggle"Add an "feature_toggle.rb" initializer
Setup the feature toggle page
FeatureToggle.setupdo |config|
config.layout='admin'config.authentication_method=:authenticate_admin!endDescribe the different features and states
FeatureToggle.definedofeature:employment_settingsdodesc"Employment settings"endfeature:employee_filedodesc"Access employee file"feature:createfeature:destroyendstate:premiumdodesc"Premium plan"value{current_user.premium?}endstate:freedodesc"Trial user"value{current_user.free?}endendInclude this mixins into your application_controller.rb
classApplicationController < ActionController::BaseincludeFeatureToggle::ControllerendFrom 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:
classEmployeesController < ApplicationControllerrequire_feature!:employee_fileendIt 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.