Module: QuickCaptcha::ModelValidation::ClassMethods

Defined in:
lib/quick_captcha/model_validation.rb

Overview

To implement model based simple captcha use this method in the model as…

class User < ActiveRecord::Base
  validates_captcha :message => "Are you a bot?"
end

Configuration options:

* :add_to_base - Specifies if error should be added to base or captcha field. defaults to false.
* :message - A custom error message (default is: "Secret Code did not match with the Image")
* :on - Specifies when this validation is active (default is :save, other options :create, :update)
* :if - Specifies a method, proc or string to call to determine if the validation should occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value.
* :unless - Specifies a method, proc or string to call to determine if the validation should not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The method, proc or string should return or evaluate to a true or false value.

Instance Method Summary collapse

Instance Method Details

#apply_quick_captcha(options = {}) ⇒ Object



57
58
59
# File 'lib/quick_captcha/model_validation.rb', line 57

def apply_quick_captcha(options = {})
  apply_simple_captcha(options)
end

#apply_simple_captcha(options = {}) ⇒ Object

4 backward compatibility



61
62
63
64
65
66
# File 'lib/quick_captcha/model_validation.rb', line 61

def apply_simple_captcha(options = {}) # 4 backward compatibility
  outcome = validates_captcha(options)
  self.captcha_validation = false
  include QuickCaptcha::ModelValidation::SaveWithCaptcha
  outcome
end

#captcha_validation=(flag) ⇒ Object



72
73
74
# File 'lib/quick_captcha/model_validation.rb', line 72

def captcha_validation=(flag)
  @_captcha_validation = flag
end

#captcha_validation?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/quick_captcha/model_validation.rb', line 68

def captcha_validation?
  defined?(@_captcha_validation) ? @_captcha_validation : true
end

#validates_captcha(options = {}) ⇒ Object



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
55
# File 'lib/quick_captcha/model_validation.rb', line 26

def validates_captcha(options = {})
  orig_options = options
  # AR by default looks into the following i18n scope :
  # :'activerecord.errors.messages.record_invalid'
  options = { :message => :captcha }
  options.update(orig_options)

  attr_accessor :captcha, :captcha_key 
  include QuickCaptcha::ModelValidation::InstanceMethods

  validate =
    case (options[:on] || :save)
      when :save   then :validate
      when :create then :validate_on_create
      when :update then :validate_on_update
    end
  send(validate, options) do |record|
    if !record.captcha_validation?
      true
    elsif record.captcha_is_valid?
      true
    elsif options[:add_to_base]
      record.errors.add_to_base(options[:message])
      false
    else
      record.errors.add(:captcha, options[:message])
      false
    end
  end
end