Skip to content

Commit

Permalink
[change] validates in model instead of controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Kroll committed May 26, 2010
1 parent 754d42e commit 62799ab
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/recaptcha.rb
@@ -1,5 +1,6 @@
require 'recaptcha/client_helper'
require 'recaptcha/verify'
require 'recaptcha/activerecord'

module Recaptcha
module VERSION #:nodoc:
Expand All @@ -20,4 +21,4 @@ module VERSION #:nodoc:

class RecaptchaError < StandardError
end
end
end
39 changes: 39 additions & 0 deletions lib/recaptcha/activerecord.rb
@@ -0,0 +1,39 @@
# reCAPTCHA model validation, based on SimpleCaptcha
# (SimpleCaptcha Copyright (c) 2008 [Sur http://expressica.com])

module Recaptcha #:nodoc
module ModelHelpers #:nodoc
# class User < ActiveRecord::Base
# apply_recaptcha :message => "my customized message"
# end
module ClassMethods
def apply_recaptcha(options = {})
instance_variable_set(:@add_to_base, options[:add_to_base])
instance_variable_set(:@recaptcha_invalid_message, options[:message] || "The words were not entered correctly")
module_eval do
attr_accessor :remote_ip, :recaptcha_challenge_field, :recaptcha_response_field, :authenticate_with_recaptcha
include Recaptcha::ModelHelpers::InstanceMethods
end
end
end

module InstanceMethods
def save_with_recaptcha
def self.validate
super
verify_recaptcha(:model => self, :attribute => :recaptcha)
end
ret = save
def self.validate
super
end
ret
end
end
end
end

ActiveRecord::Base.module_eval do
extend Recaptcha::ModelHelpers::ClassMethods
end

2 changes: 1 addition & 1 deletion lib/recaptcha/client_helper.rb
Expand Up @@ -6,7 +6,7 @@ def recaptcha_tags(options = {})
# Default options
key = options[:public_key] ||= ENV['RECAPTCHA_PUBLIC_KEY']
raise RecaptchaError, "No public key specified." unless key
error = options[:error] ||= (defined? flash ? flash[:recaptcha_error] : "")
error = current_object.errors.on(:recaptcha)
uri = options[:ssl] ? RECAPTCHA_API_SECURE_SERVER : RECAPTCHA_API_SERVER
html = ""
if options[:display]
Expand Down
2 changes: 1 addition & 1 deletion lib/recaptcha/rails.rb
@@ -1,4 +1,4 @@
require 'recaptcha'

ActionView::Base.send(:include, Recaptcha::ClientHelper)
ActionController::Base.send(:include, Recaptcha::Verify)
ActiveRecord::Base.send(:include, Recaptcha::Verify)
11 changes: 3 additions & 8 deletions lib/recaptcha/verify.rb
Expand Up @@ -19,27 +19,22 @@ def verify_recaptcha(options = {})
Timeout::timeout(options[:timeout] || 3) do
recaptcha = Net::HTTP.post_form URI.parse("http://#{RECAPTCHA_VERIFY_SERVER}/verify"), {
"privatekey" => private_key,
"remoteip" => request.remote_ip,
"challenge" => params[:recaptcha_challenge_field],
"response" => params[:recaptcha_response_field]
"remoteip" => remote_ip,
"challenge" => recaptcha_challenge_field,
"response" => recaptcha_response_field
}
end
answer, error = recaptcha.body.split.map { |s| s.chomp }
unless answer == 'true'
flash[:recaptcha_error] = error
if model
model.valid?
model.errors.add attribute, options[:message] || "Word verification response is incorrect, please try again."
end
return false
else
flash[:recaptcha_error] = nil
return true
end
rescue Timeout::Error
flash[:recaptcha_error] = "recaptcha-not-reachable"
if model
model.valid?
model.errors.add attribute, options[:message] || "Oops, we failed to validate your word verification response. Please try again."
end
return false
Expand Down

0 comments on commit 62799ab

Please sign in to comment.