public
Clone URL: git://github.com/bwyrosdick/less-captcha.git
Search Repo:
broke apart the question from the fields and updated README
bwyrosdick (author)
Tue Mar 25 09:10:46 -0700 2008
commit  6bcfe61d4026e5e425a9c808792a80a8c03224f1
tree    343691b04a711ea021218c7573c0c2be0e6ae967
parent  92817d701a8389812d1472a339bbad47c96eaf99
0
...
8
9
10
11
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
...
8
9
10
 
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
0
@@ -8,4 +8,19 @@ end
0
 
0
 == In the view
0
 
0
-<%= captcha_field("user") %>
0
\ No newline at end of file
0
+<p>
0
+ Unless you are a bot answer this question: <%= captcha_display %>
0
+<p>
0
+<p>
0
+ <%= captcha_field("user") %>
0
+</p>
0
+
0
+== Generated HTML
0
+
0
+<p>
0
+ Unless you are a bot answer this question: <span class='less_captcha_challenge'>What is 4 + 7?</span>
0
+</p>
0
+<p>
0
+ <input type="text" name="entry[captcha]" />
0
+ <input type="hidden" name="entry[captcha_answer]" value="e43b21e80194c61f30930a442f2c19de6fe74f8c" />
0
+</p>
0
\ No newline at end of file
...
40
41
42
43
 
44
45
46
47
48
 
 
 
 
 
 
 
 
 
 
 
49
50
51
...
61
62
63
64
65
66
67
68
69
 
 
 
 
 
70
71
72
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
75
76
77
...
40
41
42
 
43
44
45
46
 
 
47
48
49
50
51
52
53
54
55
56
57
58
59
60
...
70
71
72
 
 
 
 
 
 
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
0
@@ -40,12 +40,21 @@ module Less
0
     module InstanceMethods
0
       # Sets up the passing answer for the captcha challenge
0
       #
0
- # setup_captcha 'foo'
0
+ # setup_captcha
0
       #
0
       # options:
0
       # * <tt>answer</tt> - The passing answer for the captcha challenge
0
- def setup_captcha(answer)
0
- send(PREFIX + SUFFIX + '=', Digest::SHA1.hexdigest(SALT + answer.to_s))
0
+ def setup_captcha
0
+ unless send(PREFIX) and send(PREFIX + SUFFIX)
0
+ b = rand(10) + 1
0
+ a = b + rand(10)
0
+ op = ['+', '-'][rand(2)]
0
+ question = "What is #{a} #{op} #{b}?"
0
+ answer = a.send(op, b)
0
+
0
+ send(PREFIX + '=', question.to_s)
0
+ send(PREFIX + SUFFIX + '=', Digest::SHA1.hexdigest(SALT + answer.to_s))
0
+ end
0
       end
0
     end
0
 
0
@@ -61,16 +70,35 @@ module Less
0
       #
0
       # You can use the +options+ argument to pass additional options to the text-field tag.
0
       def captcha_field(object, options={})
0
- b = rand(10) + 1
0
- a = b + rand(10)
0
- op = ['+', '-'][rand(2)]
0
- question = "What is #{a} #{op} #{b}?"
0
- answer = a.send(op, b)
0
- eval("@"+object.to_s).setup_captcha(answer)
0
+ if object.is_a?(String) or object.is_a?(Symbol)
0
+ eval("@"+object.to_s).setup_captcha
0
+ else
0
+ object.setup_captcha
0
+ end
0
 
0
         result = ActionView::Helpers::InstanceTag.new(object, PREFIX, self).to_input_field_tag("text", options)
0
         result << ActionView::Helpers::InstanceTag.new(object, PREFIX + SUFFIX, self).to_input_field_tag("hidden", {})
0
       end
0
+
0
+ # Use this helper to display a captcha challenge question
0
+ #
0
+ # <%= captcha_display %>
0
+ #
0
+ # the following HTML will be generated.
0
+ #
0
+ # <span class='less_captcha_challenge'>...</span>
0
+ #
0
+ def captcha_display
0
+ if object.is_a?(String) or object.is_a?(Symbol)
0
+ eval("@"+object.to_s).setup_captcha
0
+ captcha = eval("@"+object.to_s).send(PREFIX)
0
+ else
0
+ object.setup_captcha
0
+ captcha = object.send(PREFIX)
0
+ end
0
+
0
+ "<span class='less_captcha_challenge'>#{captcha}</span>"
0
+ end
0
     end
0
   end
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.