File: model_helpers.rb

package info (click to toggle)
ruby-simple-captcha2 0.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 168 kB
  • sloc: ruby: 589; makefile: 2
file content (74 lines) | stat: -rw-r--r-- 2,418 bytes parent folder | download | duplicates (4)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
module SimpleCaptcha #:nodoc
  module ModelHelpers #:nodoc
    def self.included(base)
      base.extend(SingletonMethods)
    end

    # 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.
    module SingletonMethods
      def apply_simple_captcha(options = {})
        options = { :add_to_base => false }.merge(options)

        class_attribute :simple_captcha_options
        self.simple_captcha_options = options

        unless self.is_a?(ClassMethods)
          include InstanceMethods
          extend ClassMethods

          attr_accessor :captcha, :captcha_key
        end
      end
    end

    module ClassMethods
    end

    module InstanceMethods

      def valid_with_captcha?
        [valid?, is_captcha_valid?].all?
      end

      def is_captcha_valid?
        return true if SimpleCaptcha.always_pass

        if captcha && captcha.upcase.delete(" ") == SimpleCaptcha::Utils::simple_captcha_value(captcha_key)
          SimpleCaptcha::Utils::simple_captcha_passed!(captcha_key)
          return true
        else
          message = simple_captcha_options[:message] || I18n.t(self.class.model_name.to_s.downcase, :scope => [:simple_captcha, :message], :default => :default)
          simple_captcha_options[:add_to_base] ? errors.add(:base, message) : errors.add(:captcha, message)
          return false
        end
      end

      def save_with_captcha
        valid_with_captcha? && save(:validate => false)
      end
    end
  end
end