Commit 0afeaa68 authored by Dallas Reedy's avatar Dallas Reedy

Create & use an Experimentation::Shared module

Moves two constants from the Gitlab::Experimentation module into a new
Gitlab::Experimentation::Shared module which is then included everywhere
those two constants were previously used (and one additional place where
it might be nice to use them).
parent a4b0fa69
# frozen_string_literal: true
class Experiment < ApplicationRecord
include ::Gitlab::Experimentation::Shared
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::Shared
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::Shared
extend ActiveSupport::Concern
included do
......
# frozen_string_literal: true
module Gitlab
module Experimentation
module Shared
GROUP_CONTROL = :control
GROUP_EXPERIMENTAL = :experimental
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Experimentation::Shared 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