acts_as_textcaptcha 3.0.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -81,6 +81,15 @@ Finally, in your form view add the spam question and answer fields using the tex
81
81
 
82
82
  *NOTE:* If you'd rather NOT use this helper and prefer to write all your own view code, see the html produced from the textcaptcha_fields method in the gem source code.
83
83
 
84
+ === Toggling Textcaptcha
85
+
86
+ You can toggle textcaptcha on/off for your models by overriding the `perform_textcaptcha?` method. If you overridde it to return false, no questions will be fetched from
87
+ the web service and textcaptcha validation is not performed. Additionally the `textcaptcha_fields` form helper will render nothing. This is useful for writing your own
88
+ logic to disable spam protection for logged in users etc.
89
+
90
+ For flexibility you can also use a `skip_textcaptcha` attribute (protected from mass-assignment) to skip the textcaptcha validation step only. This is helpful when you
91
+ need to bypass spam protection after a question has been generated and `perform_textcaptcha?` is true.
92
+
84
93
  == More Configurations
85
94
 
86
95
  The gem can be configured for models individually (as shown above) or with a config/textcaptcha.yml file for the whole app. A config must have a valid bcrypt_salt, and an api_key and/or an array of questions defined.
@@ -104,14 +113,14 @@ The gem can be configured for models individually (as shown above) or with a con
104
113
 
105
114
  The gem comes with a handy rake task to copy over a {textcaptcha.yml}[http://github.com/matthutchinson/acts_as_textcaptcha/raw/master/config/textcaptcha.yml] template to your Rails config directory. It will also generate a random BCrypt Salt when you first run it.
106
115
 
107
- rake textcaptcha:config
116
+ rake textcaptcha:config
108
117
 
109
118
  *NOTE:* If you are on Rails 2.3.*, you'll have to add the following to your Rakefile to make this task available;
110
119
 
111
- `# load textcaptcha rake tasks
112
- Dir["#{Gem.searcher.find('acts_as_textcaptcha').full_gem_path}/lib/tasks/**/*.rake"].each { |ext| load ext } if Gem.searcher.find('acts_as_textcaptcha')`
120
+ # load textcaptcha rake tasks
121
+ Dir["#{Gem.searcher.find('acts_as_textcaptcha').full_gem_path}/lib/tasks/**/*.rake"].each { |ext| load ext } if Gem.searcher.find('acts_as_textcaptcha')
113
122
 
114
- === Using _without_ the Text CAPTCHA web service
123
+ === Confguring _without_ the Text CAPTCHA web service
115
124
 
116
125
  To use only your own logic questions simply ommit the api_key from your config and define at least 1 logic question/answer.
117
126
 
@@ -150,8 +159,6 @@ A call to @model.textcaptcha in your controller will query the Text CAPTCHA web
150
159
 
151
160
  validate_textcaptcha is called on @model.validate and checks that the @model.spam_answer matches one of the possible answers (decrypted). This validation is _only_ carried out on new records, i.e. never on edit, only on create. All attempted spam answers are case-insensitive and have trailing/leading white-space removed.
152
161
 
153
- perform_textcaptcha? is a simple utility method (that can be overridden in your model) It allows you to define whether the spam check should be carried out at all. For example, to turn off spam checking for logged in users.
154
-
155
162
  If an error or timeout occurs in loading or parsing the API, ActsAsTextcaptcha will fall back to choose a random logic question defined in your options. If the web service fails or no API key is specified AND no alternate questions are configured, the @model will not require spam checking and will pass as valid.
156
163
 
157
164
  For more details on the code please check the {documentation}[http://rdoc.info/projects/matthutchinson/acts_as_textcaptcha]. Tests are written with {MiniTest}[https://rubygems.org/gems/minitest] and code coverage is provided by {SimpleCov}[https://github.com/colszowka/simplecov]
@@ -31,8 +31,16 @@ module ActsAsTextcaptcha
31
31
 
32
32
  def acts_as_textcaptcha(options = nil)
33
33
  cattr_accessor :textcaptcha_config
34
- attr_accessor :spam_question, :spam_answers, :spam_answer
35
- attr_protected :spam_question if respond_to?(:accessible_attributes) && accessible_attributes.nil?
34
+ attr_accessor :spam_question, :spam_answers, :spam_answer, :skip_textcaptcha
35
+
36
+ if respond_to?(:accessible_attributes)
37
+ if accessible_attributes.nil?
38
+ attr_protected :spam_question
39
+ attr_protected :skip_textcaptcha
40
+ else
41
+ attr_accessible :spam_answer, :spam_answers
42
+ end
43
+ end
36
44
 
37
45
  validate :validate_textcaptcha
38
46
 
@@ -52,7 +60,7 @@ module ActsAsTextcaptcha
52
60
 
53
61
  module InstanceMethods
54
62
 
55
- # override this method to toggle textcaptcha spam checking, default is on (true)
63
+ # override this method to toggle textcaptcha spam checking altogether, default is on (true)
56
64
  def perform_textcaptcha?
57
65
  true
58
66
  end
@@ -121,7 +129,7 @@ module ActsAsTextcaptcha
121
129
  def validate_textcaptcha
122
130
  # only spam check on new/unsaved records (ie. no spam check on updates/edits)
123
131
  if !respond_to?('new_record?') || new_record?
124
- if perform_textcaptcha? && !validate_spam_answer
132
+ if !skip_textcaptcha && perform_textcaptcha? && !validate_spam_answer
125
133
  errors.add(:spam_answer, :incorrect_answer, :message => "is incorrect, try another question instead")
126
134
  # regenerate question
127
135
  textcaptcha
@@ -1,3 +1,3 @@
1
1
  module ActsAsTextcaptcha
2
- VERSION = "3.0.0"
2
+ VERSION = "3.0.1"
3
3
  end
@@ -60,6 +60,17 @@ describe 'Textcaptcha' do
60
60
  @contact.valid?.must_equal true
61
61
  @contact.errors[:spam_answer].must_be_empty
62
62
  end
63
+
64
+ it 'should allow validation to be skipped' do
65
+ @note.valid?.must_equal false
66
+ @note.skip_textcaptcha = true
67
+ @note.valid?.must_equal true
68
+ end
69
+
70
+ it 'should protect skip_textcaptcha attribute from mass assignment' do
71
+ @contact = Contact.new(:skip_textcaptcha => true)
72
+ @contact.skip_textcaptcha.must_equal nil
73
+ end
63
74
  end
64
75
 
65
76
  describe 'encryption' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_textcaptcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-06 00:00:00.000000000 +01:00
12
+ date: 2011-08-07 00:00:00.000000000 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bcrypt-ruby
17
- requirement: &2156333440 !ruby/object:Gem::Requirement
17
+ requirement: &2164674040 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 2.1.4
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2156333440
25
+ version_requirements: *2164674040
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rails
28
- requirement: &2156333020 !ruby/object:Gem::Requirement
28
+ requirement: &2164673620 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *2156333020
36
+ version_requirements: *2164673620
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: bundler
39
- requirement: &2156332560 !ruby/object:Gem::Requirement
39
+ requirement: &2164673160 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *2156332560
47
+ version_requirements: *2164673160
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: simplecov
50
- requirement: &2156332140 !ruby/object:Gem::Requirement
50
+ requirement: &2164672740 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2156332140
58
+ version_requirements: *2164672740
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: rdoc
61
- requirement: &2156331720 !ruby/object:Gem::Requirement
61
+ requirement: &2164672320 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *2156331720
69
+ version_requirements: *2164672320
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: sqlite3
72
- requirement: &2156331300 !ruby/object:Gem::Requirement
72
+ requirement: &2164671900 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *2156331300
80
+ version_requirements: *2164671900
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: fakeweb
83
- requirement: &2156330880 !ruby/object:Gem::Requirement
83
+ requirement: &2164671480 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,7 +88,7 @@ dependencies:
88
88
  version: '0'
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *2156330880
91
+ version_requirements: *2164671480
92
92
  description: ! "Simple question/answer based spam protection for your Rails models.\n
93
93
  \ You can define your own logic questions and/or fetch questions from the textcaptcha.com
94
94
  API.\n The questions involve human logic and are tough for spam bots to crack.\n