Module: Bandit::ViewConcerns

Extended by:
ActiveSupport::Concern
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



8
9
10
# File 'lib/bandit/extensions/view_concerns.rb', line 8

def bandit_choose(exp)
  bandit_session_choose(exp)
end

#bandit_session_choose(exp) ⇒ Object

stick to one alternative for the entire browser session



18
19
20
21
22
23
24
# File 'lib/bandit/extensions/view_concerns.rb', line 18

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



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

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



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bandit/extensions/view_concerns.rb', line 27

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