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
43
44
# File 'lib/abongo/view_helper.rb', line 6

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

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



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

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.



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

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