Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation should be idempotent #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ and in the controller's action authenticate it as

if simple_captcha_valid?
do this
simple_captcha_passed!
else
do that
end
Expand Down
30 changes: 17 additions & 13 deletions lib/simple_captcha/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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
Expand All @@ -12,13 +12,13 @@ def self.included(base)
#
# end
#
# Customize the error message by using :message, the default message is "Captcha did not match".
# 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.
Expand All @@ -31,43 +31,47 @@ def self.included(base)
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 Rails.env.test?

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.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)
if valid_with_captcha? && save(:validate => false)
SimpleCaptcha::Utils::simple_captcha_passed!(captcha_key)
true
else
false
end
end
end
end
Expand Down
9 changes: 6 additions & 3 deletions lib/simple_captcha/controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module SimpleCaptcha #:nodoc
module SimpleCaptcha #:nodoc
module ControllerHelpers #:nodoc
# This method is to validate the simple captcha in controller.
# It means when the captcha is controller based i.e. :object has not been passed to the method show_simple_captcha.
Expand All @@ -15,15 +15,18 @@ module ControllerHelpers #:nodoc
# end
def simple_captcha_valid?
return true if Rails.env.test?

if params[:captcha]
data = SimpleCaptcha::Utils::simple_captcha_value(params[:captcha_key] || session[:captcha])
result = data == params[:captcha].delete(" ").upcase
SimpleCaptcha::Utils::simple_captcha_passed!(session[:captcha]) if result
return result
else
return false
end
end

def simple_captcha_passed!
SimpleCaptcha::Utils::simple_captcha_passed!(session[:captcha])
end
end
end