diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2018-10-08 16:24:40 +0200 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2018-10-11 17:06:19 +0200 |
commit | 21940d1edf1604f192957691e99677d191380543 (patch) | |
tree | f9760a1263c9128aca629df81264ea98946055e7 /lib | |
parent | a5ca102aa1440150717a470059f707d5e5842782 (diff) | |
download | gitlab-ce-21940d1edf1604f192957691e99677d191380543.tar.gz |
Support pushing of feature flags to the frontend
This adds a method to Gitlab::GonHelper called
`push_frontend_feature_flag`. This method can be used to easily expose
the state of a feature flag to Javascript code. For example, using this
method we may write the following controller code:
before_action do
push_frontend_feature_flag(:vim_bindings)
end
def index
# ...
end
def edit
# ...
end
In Javascript we can then check the state of the flag as follows:
if ( gon.features.vimBindings ) {
// ...
}
Fixes https://gitlab.com/gitlab-org/release/framework/issues/17
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/gon_helper.rb | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/gitlab/gon_helper.rb b/lib/gitlab/gon_helper.rb index deaa14c8434..c1726659a90 100644 --- a/lib/gitlab/gon_helper.rb +++ b/lib/gitlab/gon_helper.rb @@ -30,5 +30,20 @@ module Gitlab gon.current_user_avatar_url = current_user.avatar_url end end + + # Exposes the state of a feature flag to the frontend code. + # + # name - The name of the feature flag, e.g. `my_feature`. + # args - Any additional arguments to pass to `Feature.enabled?`. This allows + # you to check if a flag is enabled for a particular user. + def push_frontend_feature_flag(name, *args) + var_name = name.to_s.camelize(:lower) + enabled = Feature.enabled?(name, *args) + + # Here the `true` argument signals gon that the value should be merged + # into any existing ones, instead of overwriting them. This allows you to + # use this method to push multiple feature flags. + gon.push({ features: { var_name => enabled } }, true) + end end end |