Module: Bandit::ViewConcerns::InstanceMethods

Defined in:
lib/bandit/extensions/view_concerns.rb

Instance Method Summary collapse

Instance Method Details

#bandit_choose(exp) ⇒ Object

default choose is a session based choice



12
13
14
# File 'lib/bandit/extensions/view_concerns.rb', line 12

def bandit_choose(exp)
  bandit_session_choose(exp)
end

#bandit_session_choose(exp) ⇒ Object

stick to one alternative for the entire browser session



22
23
24
25
26
27
28
# File 'lib/bandit/extensions/view_concerns.rb', line 22

def bandit_session_choose(exp)
  name = "bandit_#{exp}".intern
  # choose url param with preference
  value = params[name].nil? ? cookies.signed[name] : params[name]
  # choose with default, and set cookie
  cookies.signed[name] = Bandit.get_experiment(exp).choose(value)
end

#bandit_simple_choose(exp) ⇒ Object

always choose something new and increase the participant count



17
18
19
# File 'lib/bandit/extensions/view_concerns.rb', line 17

def bandit_simple_choose(exp)
  Bandit.get_experiment(exp).choose(nil)
end

#bandit_sticky_choose(exp) ⇒ Object

stick to one alternative until user deletes cookies or changes browser



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bandit/extensions/view_concerns.rb', line 31

def bandit_sticky_choose(exp)
  name = "bandit_#{exp}".intern
  # choose url param with preference
  value = params[name].nil? ? cookies.signed[name] : params[name]
  # sticky choice may outlast a given alternative
  alternative = if Bandit.get_experiment(exp).alternatives.include?(value)
                  value
                else
                  Bandit.get_experiment(exp).choose(value)
                end
  # re-set cookie
  cookies.permanent.signed[name] = alternative
end