has_captcha.rb
1.43 KB
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
47
48
49
50
51
52
53
54
module MathCaptcha
module HasCaptcha
module InstanceMethods
def must_solve_captcha
self.errors.add(:captcha_solution, "wrong answer.") unless self.captcha.check(self.captcha_solution.to_i)
end
def skip_captcha!
self.class.skip_captcha!
end
def skip_captcha?
self.class.skip_captcha?
end
def captcha
@captcha ||= Captcha.new
end
def captcha_secret=(secret)
@captcha = Captcha.from_secret(secret)
end
def captcha_secret
captcha.to_secret
end
end
module ClassMethods
def has_captcha
include InstanceMethods
attr_accessor :captcha_solution
dont_skip_captcha!
validates_presence_of :captcha_solution,
:on => :create, :message => "can't be blank",
:unless => Proc.new {|record| record.skip_captcha? }
validate_on_create :must_solve_captcha,
:unless => Proc.new {|record| record.skip_captcha? }
end
def skip_captcha!
@@skip_captcha = true
end
def dont_skip_captcha!
@@skip_captcha = false
end
def skip_captcha?
@@skip_captcha
end
def skipping_captcha(&block)
skipping_before = skip_captcha?
skip_captcha!
yield
dont_skip_captcha! if skipping_before
end
end
end
end
ActiveRecord::Base.send(:extend, MathCaptcha::HasCaptcha::ClassMethods)