Commit ddcf3627 authored by Sean Arnold's avatar Sean Arnold

Add push_licensed_feature to gon helper

- Add EE override
parent de8222fd
......@@ -51,7 +51,7 @@ class Projects::IssuesController < Projects::ApplicationController
real_time_feature_flag = :real_time_issue_sidebar
real_time_enabled = Gitlab::ActionCable::Config.in_app? || Feature.enabled?(real_time_feature_flag, @project)
push_to_gon_features(real_time_feature_flag, real_time_enabled)
push_to_gon_attributes(:features, real_time_feature_flag, real_time_enabled)
record_experiment_user(:invite_members_version_a)
record_experiment_user(:invite_members_version_b)
......
......@@ -21,6 +21,6 @@ class Groups::UsageQuotasController < Groups::ApplicationController
end
def push_additional_repo_storage_by_namespace_feature
push_to_gon_features(:additional_repo_storage_by_namespace, @group.additional_repo_storage_by_namespace_enabled?)
push_to_gon_attributes(:features, :additional_repo_storage_by_namespace, @group.additional_repo_storage_by_namespace_enabled?)
end
end
......@@ -13,6 +13,6 @@ class Profiles::UsageQuotasController < Profiles::ApplicationController
private
def push_additional_repo_storage_by_namespace_feature
push_to_gon_features(:additional_repo_storage_by_namespace, current_user.namespace.additional_repo_storage_by_namespace_enabled?)
push_to_gon_attributes(:features, :additional_repo_storage_by_namespace, current_user.namespace.additional_repo_storage_by_namespace_enabled?)
end
end
......@@ -4,6 +4,7 @@ module EE
module Gitlab
module GonHelper
extend ::Gitlab::Utils::Override
extend ::Gitlab::Utils::Override
override :add_gon_variables
def add_gon_variables
......@@ -11,6 +12,21 @@ module EE
gon.roadmap_epics_limit = 1000
end
# Exposes if a licensed feature is available.
#
# name - The name of the licensed feature
# obj - the object to check the licensed feature on (project, namespace)
override :push_licensed_feature
def push_licensed_feature(name, obj = nil)
enabled = if obj
obj.feature_available?(name)
else
::License.feature_available?(name)
end
push_to_gon_attributes(:licensed_features, name, enabled)
end
end
end
end
......@@ -26,4 +26,47 @@ RSpec.describe EE::Gitlab::GonHelper do
helper.add_gon_variables
end
end
describe '#push_licensed_feature' do
let_it_be(:feature) { License::EEU_FEATURES.first }
shared_examples 'sets the licensed features flag' do
it 'pushes the licensed feature flag to the frotnend' do
gon = instance_double('gon')
stub_licensed_features(feature => true)
allow(helper)
.to receive(:gon)
.and_return(gon)
expect(gon)
.to receive(:push)
.with({ licensed_features: { feature.to_s.camelize(:lower) => true } }, true)
subject
end
end
context 'no obj given' do
subject { helper.push_licensed_feature(feature) }
before do
expect(License).to receive(:feature_available?).with(feature)
end
it_behaves_like 'sets the licensed features flag'
end
context 'obj given' do
let(:project) { create(:project) }
subject { helper.push_licensed_feature(feature, project) }
before do
expect(project).to receive(:feature_available?).with(feature).and_call_original
end
it_behaves_like 'sets the licensed features flag'
end
end
end
......@@ -61,15 +61,23 @@ module Gitlab
def push_frontend_feature_flag(name, *args, **kwargs)
enabled = Feature.enabled?(name, *args, **kwargs)
push_to_gon_features(name, enabled)
push_to_gon_attributes(:features, name, enabled)
end
def push_to_gon_features(name, enabled)
# Exposes if a licensed feature is available.
# This is always set to false in CE.
# name - The name of the licensed feature
def push_licensed_feature(name, _obj = nil)
push_to_gon_attributes(:licensed_features, name, false)
end
def push_to_gon_attributes(key, name, enabled)
var_name = name.to_s.camelize(:lower)
# 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)
gon.push({ key => { var_name => enabled } }, true)
end
def default_avatar_url
......
......@@ -32,6 +32,22 @@ RSpec.describe Gitlab::GonHelper do
end
end
describe '#push_licensed_feature' do
it 'pushes a licensed flag set to false to the frontend,' do
gon = instance_double('gon')
allow(helper)
.to receive(:gon)
.and_return(gon)
expect(gon)
.to receive(:push)
.with({ licensed_features: { 'testing' => false } }, true)
helper.push_licensed_feature(:testing)
end
end
describe '#default_avatar_url' do
it 'returns an absolute URL' do
url = helper.default_avatar_url
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment