Commit f65cd503 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch 'dreedy-create-gitlab-experimentation-shared-module' into 'master'

Create & use an Experimentation::GroupTypes module

See merge request gitlab-org/gitlab!46392
parents a6997eab 97c97d9d
# frozen_string_literal: true
class Experiment < ApplicationRecord
include ::Gitlab::Experimentation::GroupTypes
has_many :experiment_users
has_many :users, through: :experiment_users
has_many :control_group_users, -> { merge(ExperimentUser.control) }, through: :experiment_users, source: :user
......@@ -14,7 +16,7 @@ class Experiment < ApplicationRecord
return unless experiment
return if experiment.experiment_users.where(user: user).exists?
group_type == ::Gitlab::Experimentation::GROUP_CONTROL ? experiment.add_control_user(user) : experiment.add_experimental_user(user)
group_type == GROUP_CONTROL ? experiment.add_control_user(user) : experiment.add_experimental_user(user)
end
def add_control_user(user)
......
# frozen_string_literal: true
class ExperimentUser < ApplicationRecord
include ::Gitlab::Experimentation::GroupTypes
belongs_to :experiment
belongs_to :user
enum group_type: { control: 0, experimental: 1 }
enum group_type: { GROUP_CONTROL => 0, GROUP_EXPERIMENTAL => 1 }
validates :group_type, presence: true
end
......@@ -91,15 +91,13 @@ module Gitlab
}
}.freeze
GROUP_CONTROL = :control
GROUP_EXPERIMENTAL = :experimental
# Controller concern that checks if an `experimentation_subject_id cookie` is present and sets it if absent.
# Used for A/B testing of experimental features. Exposes the `experiment_enabled?(experiment_name)` method
# to controllers and views. It returns true when the experiment is enabled and the user is selected as part
# of the experimental group.
#
module ControllerConcern
include ::Gitlab::Experimentation::GroupTypes
extend ActiveSupport::Concern
included do
......
# frozen_string_literal: true
module Gitlab
module Experimentation
module GroupTypes
GROUP_CONTROL = :control
GROUP_EXPERIMENTAL = :experimental
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Experimentation::GroupTypes do
it 'defines a GROUP_CONTROL constant' do
expect(described_class.const_defined?(:GROUP_CONTROL)).to be_truthy
end
it 'defines a GROUP_EXPERIMENTAL constant' do
expect(described_class.const_defined?(:GROUP_EXPERIMENTAL)).to be_truthy
end
end
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