diff --git a/lib/split/helper.rb b/lib/split/helper.rb index b8ddfc2a..78dc99fe 100644 --- a/lib/split/helper.rb +++ b/lib/split/helper.rb @@ -11,9 +11,13 @@ def ab_test(metric_descriptor, control = nil, *alternatives) alternative = if Split.configuration.enabled && !exclude_visitor? experiment.save raise(Split::InvalidExperimentsFormatError) unless (Split.configuration.experiments || {}).fetch(experiment.name.to_sym, {})[:combined_experiments].nil? - trial = Trial.new(:user => ab_user, :experiment => experiment, - :override => override_alternative(experiment.name), :exclude => exclude_visitor?, - :disabled => split_generically_disabled?) + trial = Trial.new( + :user => ab_user, + :experiment => experiment, + :override => override_alternative(experiment.name), + :exclude => !is_qualified?, + :disabled => split_generically_disabled? + ) alt = trial.choose!(self) alt ? alt.name : nil else @@ -161,5 +165,11 @@ def normalize_metric(metric_descriptor) def control_variable(control) Hash === control ? control.keys.first.to_s : control.to_s end + + private + + def is_qualified? + self.respond_to?(:ab_test_user_qualified?, true) ? self.send(:ab_test_user_qualified?) : true + end end end diff --git a/spec/helper_spec.rb b/spec/helper_spec.rb index 95eb32ee..9194ef2f 100755 --- a/spec/helper_spec.rb +++ b/spec/helper_spec.rb @@ -274,6 +274,30 @@ ab_test('link_color', 'blue', 'red') expect(ab_user).to eq(finished_session) end + + context "with ab_test_user_qualified is set" do + context "ab_test_user_qualified returns true" do + def ab_test_user_qualified? + true + end + + it "user is qualified to participate in experiment" do + ab_test('link_color', 'blue', 'red') + expect(['red', 'blue']).to include(ab_user['link_color']) + end + end + + context "ab_test_user_qualified returns false" do + def ab_test_user_qualified? + false + end + + it "user is not qualified to participate in experiment" do + ab_test('link_color', 'blue', 'red') + expect(ab_user['link_color']).to eq(nil) + end + end + end end describe 'metadata' do