glebtv-simple_captcha 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,18 +17,15 @@ backward compatibility with previous versions of Rails.
17
17
 
18
18
  ==Requirements
19
19
 
20
- * {Ruby}[http://ruby-lang.org/] >= 1.8.7
20
+ * {Ruby}[http://ruby-lang.org/] >= 1.9
21
21
  * {Rails}[http://github.com/rails/rails] >= 3
22
22
  * ImageMagick should be installed on your machine to use this plugin.
23
23
  visit http://www.imagemagick.org/script/index.php for more details.
24
+ * AR or mongoid
24
25
 
25
26
  ==Installation
26
27
 
27
- gem "galetahub-simple_captcha", :require => "simple_captcha"
28
-
29
- or
30
-
31
- gem 'galetahub-simple_captcha', :require => 'simple_captcha', :git => 'git://github.com/galetahub/simple-captcha.git'
28
+ gem "glebtv-simple_captcha", "~> 0.2.0", require: "simple_captcha"
32
29
 
33
30
  ==Setup
34
31
 
@@ -6,14 +6,16 @@ module SimpleCaptcha
6
6
  autoload :ImageHelpers, 'simple_captcha/image'
7
7
  autoload :ViewHelper, 'simple_captcha/view'
8
8
  autoload :ControllerHelpers, 'simple_captcha/controller'
9
- autoload :ModelHelpers, 'simple_captcha/active_record'
10
9
 
11
10
  autoload :FormBuilder, 'simple_captcha/form_builder'
12
11
  autoload :CustomFormBuilder, 'simple_captcha/formtastic'
13
12
 
14
13
  if defined?(Mongoid)
14
+ require 'simple_captcha/mongoid'
15
+ autoload :ModelHelpers, 'simple_captcha/mongoid'
15
16
  autoload :SimpleCaptchaData, 'simple_captcha/simple_captcha_data_mongoid'
16
17
  else
18
+ autoload :ModelHelpers, 'simple_captcha/active_record'
17
19
  autoload :SimpleCaptchaData, 'simple_captcha/simple_captcha_data_ar'
18
20
  end
19
21
  autoload :Middleware, 'simple_captcha/middleware'
@@ -5,8 +5,10 @@ require 'simple_captcha'
5
5
  module SimpleCaptcha
6
6
  class Engine < ::Rails::Engine
7
7
  config.before_initialize do
8
- ActiveSupport.on_load :active_record do
9
- ActiveRecord::Base.send(:include, SimpleCaptcha::ModelHelpers)
8
+ unless defined?(Mongoid)
9
+ ActiveSupport.on_load :active_record do
10
+ ActiveRecord::Base.send(:include, SimpleCaptcha::ModelHelpers)
11
+ end
10
12
  end
11
13
  end
12
14
 
@@ -0,0 +1,75 @@
1
+ module SimpleCaptcha #:nodoc
2
+ module ModelHelpers #:nodoc
3
+ def self.included(base)
4
+ base.extend(SingletonMethods)
5
+ end
6
+
7
+ # To implement model based simple captcha use this method in the model as...
8
+ #
9
+ # class User
10
+ # include Mongoid::Document
11
+ # include Mongoid::SimpleCaptcha
12
+ # apply_simple_captcha :message => "my customized message"
13
+ #
14
+ # end
15
+ #
16
+ # Customize the error message by using :message, the default message is "Captcha did not match".
17
+ # As in the applications captcha is needed with a very few cases like signing up the new user, but
18
+ # not every time you need to authenticate the captcha with @user.save. So as to maintain simplicity
19
+ # here we have the explicit method to save the instace with captcha validation as...
20
+ #
21
+ # * to validate the instance
22
+ #
23
+ # @user.valid_with_captcha? # whene captcha validation is required.
24
+ #
25
+ # @user.valid? # when captcha validation is not required.
26
+ #
27
+ # * to save the instance
28
+ #
29
+ # @user.save_with_captcha # whene captcha validation is required.
30
+ #
31
+ # @user.save # when captcha validation is not required.
32
+ module SingletonMethods
33
+ def apply_simple_captcha(options = {})
34
+ options = { :add_to_base => false }.merge(options)
35
+
36
+ class_attribute :simple_captcha_options
37
+ self.simple_captcha_options = options
38
+
39
+ unless self.is_a?(ClassMethods)
40
+ include InstanceMethods
41
+ extend ClassMethods
42
+
43
+ attr_accessor :captcha, :captcha_key
44
+ end
45
+ end
46
+ end
47
+
48
+ module ClassMethods
49
+ end
50
+
51
+ module InstanceMethods
52
+
53
+ def valid_with_captcha?
54
+ [valid?, is_captcha_valid?].all?
55
+ end
56
+
57
+ def is_captcha_valid?
58
+ return true if Rails.env.test?
59
+
60
+ if captcha && captcha.upcase.delete(" ") == SimpleCaptcha::Utils::simple_captcha_value(captcha_key)
61
+ SimpleCaptcha::Utils::simple_captcha_passed!(captcha_key)
62
+ return true
63
+ else
64
+ message = simple_captcha_options[:message] || I18n.t(self.class.model_name.downcase, :scope => [:simple_captcha, :message], :default => :default)
65
+ simple_captcha_options[:add_to_base] ? errors.add(:base, message) : errors.add(:captcha, message)
66
+ return false
67
+ end
68
+ end
69
+
70
+ def save_with_captcha
71
+ valid_with_captcha? && save(:validate => false)
72
+ end
73
+ end
74
+ end
75
+ end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module SimpleCaptcha
3
- VERSION = "0.2.0".freeze
3
+ VERSION = "0.2.1".freeze
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glebtv-simple_captcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -30,6 +30,7 @@ files:
30
30
  - lib/simple_captcha/utils.rb
31
31
  - lib/simple_captcha/controller.rb
32
32
  - lib/simple_captcha/formtastic.rb
33
+ - lib/simple_captcha/mongoid.rb
33
34
  - lib/simple_captcha/simple_captcha_data_ar.rb
34
35
  - lib/simple_captcha/engine.rb
35
36
  - lib/simple_captcha/form_builder.rb