Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ Split.configure do |config|
config.enabled = true
config.persistence = Split::Persistence::SessionAdapter
#config.start_manually = false ## new test will have to be started manually from the admin panel. default false
config.include_rails_helper = true
end
```

Expand Down
1 change: 1 addition & 0 deletions lib/split.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
helper
metric
persistence
encapsulated_helper
trial
version].each do |f|
require "split/#{f}"
Expand Down
8 changes: 7 additions & 1 deletion lib/split/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Configuration
attr_accessor :on_trial_complete
attr_accessor :on_experiment_reset
attr_accessor :on_experiment_delete
attr_accessor :include_rails_helper

attr_reader :experiments

Expand Down Expand Up @@ -118,6 +119,10 @@ def normalized_experiments
if goals = value_for(settings, :goals)
experiment_config[experiment_name.to_sym][:goals] = goals
end

if (resettable = value_for(settings, :resettable)) != nil
experiment_config[experiment_name.to_sym][:resettable] = resettable
end
end

experiment_config
Expand Down Expand Up @@ -172,13 +177,14 @@ def initialize
@experiments = {}
@persistence = Split::Persistence::SessionAdapter
@algorithm = Split::Algorithms::WeightedSample
@include_rails_helper = true
end

private

def value_for(hash, key)
if hash.kind_of?(Hash)
hash[key.to_s] || hash[key.to_sym]
hash.has_key?(key.to_s) ? hash[key.to_s] : hash[key.to_sym]
end
end

Expand Down
58 changes: 58 additions & 0 deletions lib/split/encapsulated_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Split's helper exposes all kinds of methods we don't want to
# mix into our model classes.
#
# This module exposes only two methods
# - ab_test and
# - ab_test_finished
# that can safely be mixed into any class.
#
# Passes the instance of the class that it's mixed into to the
# Split persistence adapter as context.
#
module Split
module EncapsulatedHelper

class ContextShim
include Split::Helper
def initialize(context, original_params)
@context = context
@_params = original_params
end
def ab_user
@ab_user ||= Split::Persistence.adapter.new(@context)
end
def params
@_params
end
end

def ab_test(*arguments)
ret = split_context_shim.ab_test(*arguments)
# TODO there must be a better way to pass a block straight
# through to the original ab_test
if block_given?
if defined?(capture) # a block in a rails view
block = Proc.new { yield(ret) }
concat(capture(ret, &block))
false
else
yield(ret)
end
else
ret
end
end

def ab_test_finished(*arguments)
split_context_shim.finished *arguments
end

private

# instantiate and memoize a context shim in case of multiple ab_test* calls
def split_context_shim
_params = defined?(params) ? params : {}
@split_context_shim ||= ContextShim.new(self, _params)
end
end
end
8 changes: 5 additions & 3 deletions lib/split/engine.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module Split
class Engine < ::Rails::Engine
initializer "split" do |app|
ActionController::Base.send :include, Split::Helper
ActionController::Base.helper Split::Helper
if Split.configuration.include_rails_helper
ActionController::Base.send :include, Split::Helper
ActionController::Base.helper Split::Helper
end
end
end
end
end
6 changes: 3 additions & 3 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
end

it 'should normalize experiments' do
@config.normalized_experiments.should == {:my_experiment=>{:alternatives=>["Control Opt", ["Alt One", "Alt Two"]]}}
@config.normalized_experiments.should == {:my_experiment=>{:resettable=>false,:alternatives=>["Control Opt", ["Alt One", "Alt Two"]]}}
end
end

Expand All @@ -112,7 +112,7 @@
end

it "should normalize experiments" do
@config.normalized_experiments.should == {:my_experiment=>{:alternatives=>[{"Control Opt"=>0.67},
@config.normalized_experiments.should == {:my_experiment=>{:resettable=>false,:alternatives=>[{"Control Opt"=>0.67},
[{"Alt One"=>0.1}, {"Alt Two"=>0.23}]]}, :another_experiment=>{:alternatives=>["a", ["b"]]}}
end

Expand All @@ -139,7 +139,7 @@
end

it "should normalize experiments" do
@config.normalized_experiments.should == {:my_experiment=>{:alternatives=>["Control Opt", ["Alt One", "Alt Two"]]}}
@config.normalized_experiments.should == {:my_experiment=>{:resettable=>false,:alternatives=>["Control Opt", ["Alt One", "Alt Two"]]}}
end
end

Expand Down