Skip to content
Browse files

Merge pull request #26 from netoneko/master

Basic Mongoid support
  • Loading branch information...
2 parents f243241 + 2039a4d commit 7bf17fe6bceda3313b3cce37853f685cbc64afde @galetahub committed Sep 13, 2012
Showing with 33 additions and 2 deletions.
  1. +8 −2 lib/simple_captcha.rb
  2. +25 −0 lib/simple_captcha/simple_captcha_data_mongoid.rb
View
10 lib/simple_captcha.rb
@@ -6,12 +6,18 @@ module SimpleCaptcha
autoload :ImageHelpers, 'simple_captcha/image'
autoload :ViewHelper, 'simple_captcha/view'
autoload :ControllerHelpers, 'simple_captcha/controller'
- autoload :ModelHelpers, 'simple_captcha/active_record'
autoload :FormBuilder, 'simple_captcha/form_builder'
autoload :CustomFormBuilder, 'simple_captcha/formtastic'
- autoload :SimpleCaptchaData, 'simple_captcha/simple_captcha_data'
+ if defined?(ActiveRecord)
+ autoload :ModelHelpers, 'simple_captcha/active_record'
+ autoload :SimpleCaptchaData, 'simple_captcha/simple_captcha_data'
+ else
+ autoload :SimpleCaptchaData, 'simple_captcha/simple_captcha_data_mongoid.rb'
+ end
+
+
autoload :Middleware, 'simple_captcha/middleware'
mattr_accessor :image_size
View
25 lib/simple_captcha/simple_captcha_data_mongoid.rb
@@ -0,0 +1,25 @@
+module SimpleCaptcha
+ class SimpleCaptchaData
+ include Mongoid::Document
+ include Mongoid::Timestamps
+
+ field :key, type: String
+ field :value, type: String
+
+ class << self
+ def get_data(key)
+ data = where(:key => key).first || new(:key => key)
+ end
+
+ def remove_data(key)
+ where(:key => key).delete_all
+ clear_old_data(1.hour.ago)
+ end
+
+ def clear_old_data(time = 1.hour.ago)
+ return unless Time === time
+ where(:updated_at.lte => time).delete_all
+ end
+ end
+ end
+end

0 comments on commit 7bf17fe

Please sign in to comment.
Something went wrong with that request. Please try again.