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
47
48
49
50
51
|
# File 'lib/aritcaptcha/aritcaptcha_helper.rb', line 15
def aritcaptcha_tag(options={})
equation_key = Time.now.to_i
default_operations = {:add => "+", :sub => "-", :mul => "*", :div => "/"}
operator = nil
if options[:operations] == nil
operator = default_operations.to_a[rand(default_operations.size)][1]
else
non_default_operations = {}
options[:operations].each do |op|
non_default_operations[op] = default_operations[op]
end
operator = non_default_operations.to_a[rand(non_default_operations.size)][1]
end
equation, result = Aritcaptcha::Calculation.generate_calculation 50, 50, operator
session[:equation] = [equation_key, eval(equation)]
if options[:html]
options_html = options[:html].inject([]) { |dump, pair| dump << "#{pair[0]}=\"#{pair[1]}\"" }
options_html = options_html.join(" ")
end
html = ""
if options[:format] == "image"
session[:image] = equation_key
img = generate_image equation_key, equation
html << "<img src=\"/images/#{img}\" style='vertical-align:top;' /> <input type=\"text\" name=\"equation\" size=\"3\" style='vertical-align:top;' #{options_html unless options_html.nil?} />"
else
html << "#{equation} = <input type=\"text\" name=\"equation\" style='vertical-align:top;' size=\"3\" #{options_html unless options_html.nil?} /></div>"
end
html << "<input type=\"hidden\" name=\"equation_key\" value=\"#{equation_key}\" /> \n"
end
|