Module: Abongo::ViewHelper

Defined in:
lib/abongo/view_helper.rb

Instance Method Summary collapse

Instance Method Details

#ab_test(test_name, alternatives = nil, options = {}, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/abongo/view_helper.rb', line 6

def ab_test(test_name, alternatives = nil, options = {}, &block)
  
  if (Abongo.options[:enable_specification] && !params[test_name].nil?)
    choice = params[test_name]
  elsif (Abongo.options[:enable_override_in_session] && !session[test_name].nil?)
    choice = session[test_name]
  elsif (Abongo.options[:enable_selection] && !params[test_name].nil?)
    choice = Abongo.parse_alternatives(alternatives)[params[test_name].to_i]
  elsif (alternatives.nil?)
    begin
      choice = Abongo.flip(test_name, options)
    rescue
      if Abongo.options[:failsafe]
        choice = true
      else
        raise
      end
    end
  else
    begin
      choice = Abongo.test(test_name, alternatives, options)
    rescue
      if Abongo.options[:failsafe]
        choice = Abongo.parse_alternatives(alternatives).first
      else
        raise
      end
    end
  end
  
  if block
     = capture(choice, &block)
    Rails::VERSION::MAJOR <= 2 and block_called_from_erb?(block) ? concat() : 
  else
    choice
  end
end

#bongo!(test_name, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/abongo/view_helper.rb', line 44

def bongo!(test_name, options = {})
  begin
    Abongo.bongo!(test_name, options)
  rescue
    if Abongo.options[:failsafe]
      return
    else
      raise
    end
  end
end

#include_humanizing_javascript(url = "/abongo_mark_human", style = :prototype) ⇒ Object

This causes an AJAX post against the URL. That URL should call Abongo.human! This guarantees that anyone calling Abongo.human! is capable of at least minimal Javascript execution, and thus is (probably) not a robot.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/abongo/view_helper.rb', line 58

def include_humanizing_javascript(url = "/abongo_mark_human", style = :prototype)
  begin
    return if Abongo.is_human?
  rescue
    if Abongo.options[:failsafe]
    else
      raise
    end
  end
  
  script = nil
  if (style == :prototype)
    script = "var a=Math.floor(Math.random()*11); var b=Math.floor(Math.random()*11);var x=new Ajax.Request('#{url}', {parameters:{a: a, b: b, c: a+b}})"
  elsif (style == :jquery)
    script = "var a=Math.floor(Math.random()*11); var b=Math.floor(Math.random()*11);var x=jQuery.post('#{url}', {a: a, b: b, c: a+b})"
  end
  script.nil? ? "" : %Q|<script type="text/javascript">#{script}</script>|.html_safe
end