Sha256: 5ee769a22e66dc1e64a19e2c50e70d5408c51921b10f56404b724b702c207e3f

Contents?: true

Size: 952 Bytes

Versions: 2

Compression:

Stored size: 952 Bytes

Contents

module DalliCaptcha
  module Model
    extend ActiveSupport::Concern

    def self.included(base)
      base.extend(ClassMethods)
      base.send :validate, :check_captcha
      base.send :after_save, :delete_captcha
      base.send :attr_accessor, :captcha, :captcha_key
    end

    module ClassMethods
    end

    def key_handler
      @key_handler ||= DalliCaptcha::KeyHandler.new(captcha_key)
    end

    def check_captcha
      if without_captcha?
        @skip_captcha = false
      else
        errors.add(:captcha, DalliCaptcha::options[:error_message]) unless key_handler.valid?(captcha)
      end
    end

    def delete_captcha
      key_handler.delete
    end

    def without_captcha?
      !!@skip_captcha
    end

    def valid_without_captcha?
      @skip_captcha = true
      self.valid?
    end

    def save_without_captcha
      @skip_captcha = true
      self.save
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dalli_captcha-0.0.2 lib/dalli_captcha/model.rb
dalli_captcha-0.0.1 lib/dalli_captcha/model.rb