-
Notifications
You must be signed in to change notification settings - Fork 649
Feature Flags
CartoDB has a built-in mechanism for feature toggling that you can use to enable/disable in a user basis, which is useful for development. Some inspirational links:
- http://martinfowler.com/bliki/FeatureToggle.html
- http://techblog.outbrain.com/tag/feature-flags/
- http://code.flickr.net/2009/12/02/flipping-out/
- http://stackoverflow.com/questions/7707383/what-is-a-feature-flag
- http://www.swig.org/Doc1.3/Customization.html#Customization_feature_flags
-
Normal usage:
- JS:
user.data
returns an object with a new property calledfeature_flags
that is an array of feature flags names. Example:
<%=raw current_user.data.to_json.html_safe %>
outputs
{"id":"fca9 (...), "feature_flags":['ghost_tables']}
-
JS: the user's function
featureEnabled(name)
(ref. source code) returns true if the user has the flag passed by parameter enabled; else, it returns false. -
Ruby backend:
user.has_feature_flag?('new_dashboard')
(nil safe).
- JS:
-
Model
-
user.feature_flags
: feature flag names array. -
user.feature_flags_user
: Rails collection ofFeatureFlagsUser
(but you should not use directly, use the previous array).
-
Developers can benefit from configuration just by adding feature flags to the table feature_flags
and activating it to specific users though feature_flag_users
. And remember you can make feature flags available for everybody just by setting restricted
to f
.
Example for Rails console:
Enter Rails console at CartoDB root with bundle exec rails c
.
- Add new feature flag:
1.9.3-p551 :011 > ff = FeatureFlag.new(name: 'my_feature_flag_101', restricted: true)
=> #<FeatureFlag @values={:name=>"my_feature_flag_101", :restricted=>true}>
1.9.3-p551 :012 > ff.id = 101
=> 101
1.9.3-p551 :013 > ff.save
=> #<FeatureFlag @values={:id=>101, :name=>"my_feature_flag_101", :restricted=>true, :created_at=>2014-12-11 12:49:13 +0000, :updated_at=>2014-12-11 12:49:13 +0000}>
- Assign the new feature flag to a user:
1.9.3-p551 :014 > user = User.all.first
=> #<User @values={:email=>"[email protected]", ...
1.9.3-p551 :016 > ffu = FeatureFlagsUser.new(feature_flag_id: ff.id, user_id: user.id)
=> #<FeatureFlagsUser @values={:feature_flag_id=>101, :user_id=>"2e1dc763-4501-426d-8a1d-ea8c6195ece1"}>
1.9.3-p551 :017 > ffu.save
=> #<FeatureFlagsUser @values={:id=>16, :feature_flag_id=>101, :user_id=>"2e1dc763-4501-426d-8a1d-ea8c6195ece1", :created_at=>2014-12-11 12:51:14 +0000, :updated_at=>2014-12-11 12:51:14 +0000}>
SQL example:
Connect to your local PostgreSQL with psql -U postgres -d carto_db_development
.
insert into feature_flags (id, name, restricted) values (666, 'hell_of_feature', true);
insert into feature_flags_users (feature_flag_id, user_id) values (666, 'd73806fd-cc7e-41bb-9fd8-d0d8d1579196');