forked from moeffju/abingo
/
abingo_view_helper.rb
46 lines (39 loc) · 1.58 KB
/
abingo_view_helper.rb
1
2
3
4
5
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
45
46
#Gives you easy syntax to use ABingo in your views.
module AbingoViewHelper
def ab_test(test_name, alternatives = nil, options = {}, &block)
if (Abingo.options[:enable_specification] && !params[test_name].nil?)
choice = params[test_name]
elsif (Abingo.options[:enable_override_in_session] && !session[test_name].nil?)
choice = session[test_name]
elsif (alternatives.nil?)
choice = Abingo.flip(test_name)
else
choice = Abingo.test(test_name, alternatives, options)
end
if block
capture(choice, &block)
else
choice
end
#if block
# content_tag = capture(choice, &block)
# block_called_from_erb?(block) ? concat(content_tag) : content_tag
#else
# choice
#end
end
def bingo!(test_name, options = {})
Abingo.bingo!(test_name, options)
end
#This causes an AJAX post against the URL. That URL should call Abingo.human!
#This guarantees that anyone calling Abingo.human! is capable of at least minimal Javascript execution, and thus is (probably) not a robot.
def include_humanizing_javascript(url = "/abingo_mark_human", style = :prototype)
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>|
end
end