Skip to content

Commit 0b67045

Browse files
henrikandrew
authored andcommitted
Namespace override param (splitrb#398)
Verified that both Rails and Sinatra parse [] in params to nested hashes this way.
1 parent adb1850 commit 0b67045

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ You can do this by passing it as a parameter in the url.
136136

137137
If you have an experiment called `button_color` with alternatives called `red` and `blue` used on your homepage, a url such as:
138138

139-
http://myawesomesite.com?button_color=red
139+
http://myawesomesite.com?ab_test[button_color]=red
140140

141141
will always have red buttons. This won't be stored in your session or count towards to results, unless you set the `store_override` configuration option.
142142

lib/split/helper.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22
module Split
33
module Helper
4+
OVERRIDE_PARAM_NAME = "ab_test"
5+
46
module_function
57

68
def ab_test(metric_descriptor, control = nil, *alternatives)
@@ -80,11 +82,11 @@ def finished(metric_descriptor, options = {:reset => true})
8082
end
8183

8284
def override_present?(experiment_name)
83-
defined?(params) && params[experiment_name]
85+
override_alternative(experiment_name)
8486
end
8587

8688
def override_alternative(experiment_name)
87-
params[experiment_name] if override_present?(experiment_name)
89+
defined?(params) && params[OVERRIDE_PARAM_NAME] && params[OVERRIDE_PARAM_NAME][experiment_name]
8890
end
8991

9092
def split_generically_disabled?

spec/helper_spec.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,33 @@
8989
expect(ab_test('link_color', 'blue', 'red')).to eq('orange')
9090
end
9191

92-
it "should allow the alternative to be force by passing it in the params" do
93-
@params = {'link_color' => 'blue'}
92+
it "should allow the alternative to be forced by passing it in the params" do
93+
# ?ab_test[link_color]=blue
94+
@params = { 'ab_test' => { 'link_color' => 'blue' } }
95+
9496
alternative = ab_test('link_color', 'blue', 'red')
9597
expect(alternative).to eq('blue')
98+
9699
alternative = ab_test('link_color', {'blue' => 1}, 'red' => 5)
97100
expect(alternative).to eq('blue')
98-
@params = {'link_color' => 'red'}
101+
102+
@params = { 'ab_test' => { 'link_color' => 'red' } }
103+
99104
alternative = ab_test('link_color', 'blue', 'red')
100105
expect(alternative).to eq('red')
106+
101107
alternative = ab_test('link_color', {'blue' => 5}, 'red' => 1)
102108
expect(alternative).to eq('red')
103109
end
104110

105111
it "should not allow an arbitrary alternative" do
106-
@params = {'link_color' => 'pink'}
112+
@params = { 'ab_test' => { 'link_color' => 'pink' } }
107113
alternative = ab_test('link_color', 'blue')
108114
expect(alternative).to eq('blue')
109115
end
110116

111117
it "should not store the split when a param forced alternative" do
112-
@params = {'link_color' => 'blue'}
118+
@params = { 'ab_test' => { 'link_color' => 'blue' } }
113119
expect(ab_user).not_to receive(:[]=)
114120
ab_test('link_color', 'blue', 'red')
115121
end
@@ -136,7 +142,7 @@
136142
before { Split.configuration.store_override = true }
137143

138144
it "should store the forced alternative" do
139-
@params = {'link_color' => 'blue'}
145+
@params = { 'ab_test' => { 'link_color' => 'blue' } }
140146
expect(ab_user).to receive(:[]=).with('link_color', 'blue')
141147
ab_test('link_color', 'blue', 'red')
142148
end
@@ -232,7 +238,7 @@
232238
end
233239

234240
it 'should be passed to helper block' do
235-
@params = {'my_experiment' => 'one'}
241+
@params = { 'ab_test' => { 'my_experiment' => 'one' } }
236242
expect(ab_test('my_experiment')).to eq 'one'
237243
expect(ab_test('my_experiment') do |alternative, meta|
238244
meta
@@ -790,7 +796,7 @@ def should_finish_experiment(experiment_name, should_finish=true)
790796

791797
context 'and given an override parameter' do
792798
it 'should use given override instead of the first alternative' do
793-
@params = {'link_color' => 'red'}
799+
@params = { 'ab_test' => { 'link_color' => 'red' } }
794800
expect(ab_test('link_color', 'blue', 'red')).to eq('red')
795801
expect(ab_test('link_color', 'blue', 'red', 'green')).to eq('red')
796802
expect(ab_test('link_color', {'blue' => 0.01}, 'red' => 0.2)).to eq('red')

0 commit comments

Comments
 (0)