Sha256: 32ee494246abda281b984ac64dc1611b91d01fe1fbb8e382a0dbc553832682b4

Contents?: true

Size: 1.48 KB

Versions: 5

Compression:

Stored size: 1.48 KB

Contents

# coding: utf-8
module RailsCaptcha
  module Model
    def self.included(base)
      base.extend ActMethods
    end

    module ActMethods
      def acts_as_captcha(options={})
        extend ClassMethods
        include InstanceMethods
        attr_reader :captcha, :known_captcha
        cattr_accessor :captcha_options
        self.captcha_options = options
        validate :captcha_must_match_known_captcha
      end
    end

    module ClassMethods
    end

    module InstanceMethods
      def captcha=(c)
        @captcha = c || ''
      end

      def known_captcha=(c)
        @known_captcha = c || ''
      end

      def captcha_must_match_known_captcha
        return true if self.captcha.nil? || self.known_captcha.nil?
        decrypted = RailsCaptcha::Cipher.decrypt(self.known_captcha) rescue "no_decrypt"
        if self.captcha.strip.downcase != decrypted
          if self.captcha_options[:base]
            self.errors.add_to_base(
              case self.captcha_options[:base]
              when true
                I18n.t("rails_captcha.enter_the_correct_captcha")
              else
                self.captcha_options[:base]
              end
            )
          else
            self.errors.add(:captcha,
              case self.captcha_options[:field]
              when true, nil
                I18n.t("rails_captcha.wrong_captcha")
              else
                self.captcha_options[:field]
              end
            )
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails_captcha-0.0.10 lib/rails_captcha/captcha/model.rb
rails_captcha-0.0.8 lib/rails_captcha/captcha/model.rb
rails_captcha-0.0.7 lib/rails_captcha/captcha/model.rb
rails_captcha-0.0.6 lib/rails_captcha/captcha/model.rb
rails_captcha-0.0.5 lib/rails_captcha/captcha/model.rb