Skip to content

Commit

Permalink
Merge pull request #296 from ekorneeff/metadata-helper
Browse files Browse the repository at this point in the history
Push metadata to ab_test block
  • Loading branch information
andrew committed Feb 3, 2015
2 parents cb970c7 + d260016 commit dc9be0e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/split/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ def ab_test(metric_descriptor, control = nil, *alternatives)

if block_given?
if defined?(capture) # a block in a rails view
block = Proc.new { yield(alternative) }
block = Proc.new { yield(alternative, (trial.metadata if trial)) }
concat(capture(alternative, &block))
false
else
yield(alternative)
yield(alternative, (trial.metadata if trial))
end
else
alternative
Expand Down
2 changes: 1 addition & 1 deletion lib/split/trial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(attrs = {})
end

def metadata
@metadata ||= experiment.metadata[alternative.name]
@metadata ||= experiment.metadata[alternative.name] if experiment.metadata
end

def alternative
Expand Down
20 changes: 20 additions & 0 deletions spec/helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,26 @@
end
end

describe 'metadata' do
before do
Split.configuration.experiments = {
:my_experiment => {
:alternatives => ["one", "two"],
:resettable => false,
:metadata => { 'one' => 'Meta1', 'two' => 'Meta2' }
}
}
end

it 'should be passed to helper block' do
@params = {'my_experiment' => 'one'}
expect(ab_test('my_experiment')).to eq 'one'
expect(ab_test('my_experiment') do |alternative, meta|
meta
end).to eq('Meta1')
end
end

describe 'finished' do
before(:each) do
@experiment_name = 'link_color'
Expand Down

0 comments on commit dc9be0e

Please sign in to comment.