Module: SimpleCaptcha::ModelHelpers::ClassMethods

Defined in:
lib/simple_captcha_active_record.rb

Overview

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

class User < ActiveRecord::Base

  apply_simple_captcha :message => "my customized message"

end

Customize the error message by using :message, the default message is “Captcha did not match”. As in the applications captcha is needed with a very few cases like signing up the new user, but not every time you need to authenticate the captcha with @user.save. So as to maintain simplicity here we have the explicit method to save the instace with captcha validation as…

  • to validate the instance

@user.valid_with_captcha?  # whene captcha validation is required.

@user.valid?               # when captcha validation is not required.
  • to save the instance

@user.save_with_captcha   # whene captcha validation is required.

@user.save                # when captcha validation is not required.

Instance Method Summary collapse

Instance Method Details

#apply_simple_captcha(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/simple_captcha_active_record.rb', line 31

def apply_simple_captcha(options = {})
  instance_variable_set(:@add_to_base, options[:add_to_base])
  instance_variable_set(:@captcha_invalid_message, options[:message] || "Secret Code did not match with the Image")
  module_eval do
    include SimpleCaptcha::ConfigTasks
    attr_accessor :captcha, :captcha_key, :authenticate_with_captcha
    alias_method :valid_without_captcha?, :valid?
    alias_method :save_without_captcha, :save
    include SimpleCaptcha::ModelHelpers::InstanceMethods
  end
end