From bb5b4efb8a5dcc77f91f5adb456cdc2424e46aff Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Wed, 8 Mar 2017 11:54:36 +0800 Subject: [PATCH] - Mount Router by default, not need config now. - Default use [:file_store, 'tmp/cache/rucaptcha/session'] as RuCaptcha.config.cache_store, now it can work without any configurations. - Improve README doc. --- README.md | 33 ++++++++++++++++++++------------- lib/rucaptcha.rb | 24 +++++++++++++++++++++++- lib/rucaptcha/engine.rb | 26 +++++++------------------- 3 files changed, 50 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index d002e24..4ad7054 100644 --- a/README.md +++ b/README.md @@ -35,28 +35,20 @@ RuCaptcha.configure do  # self.style = :colorful # Custom captcha code expire time if you need, default: 2 minutes # self.expires_in = 120 -  # [Requirement/重要] +  # [Requirement / 重要] # Store Captcha code where, this config more like Rails config.cache_store # default: Read config info from `Rails.application.config.cache_store` # But RuCaptcha requirements cache_store not in [:null_store, :memory_store, :file_store] -  # 默认:会从 Rails 配置的 cache_store 里面读取相同的配置信息,用于存储验证码字符 +  # 默认:会从 Rails 配置的 cache_store 里面读取相同的配置信息,并尝试用可以运行的方式,用于存储验证码字符  # 但如果是 [:null_store, :memory_store, :file_store] 之类的,你可以通过下面的配置项单独给 RuCaptcha 配置 cache_store  self.cache_store = :mem_cache_store end ``` -Edit `config/routes.rb`, add the following code: - -```rb -Rails.application.routes.draw do - ... - mount RuCaptcha::Engine => "/rucaptcha" - ... -end -``` - Controller `app/controller/account_controller.rb` +When you called `verify_rucaptcha?`, it will uses value from `params[:_rucaptcha]` to validation. + ```rb class AccountController < ApplicationController def create @@ -68,6 +60,17 @@ class AccountController < ApplicationController end end end + +class ForgotPasswordController < ApplicationController + def create + # without any args + if verify_rucaptcha? + to_send_email + else + redirect_to '/forgot-password', alert: 'Invalid captcha code.' + end + end +end ``` > TIP: Sometime you may need keep last verified captcha code in session on `verify_rucaptcha?` method call, you can use `keep_session: true`. For example: `verify_rucaptcha? @user, keep_session: true`. @@ -75,13 +78,17 @@ end View `app/views/account/new.html.erb` ```erb -
+ ...
<%= rucaptcha_input_tag(class: 'form-control', placeholder: 'Input Captcha') %> <%= rucaptcha_image_tag(alt: 'Captcha') %>
... + +
+ +
``` diff --git a/lib/rucaptcha.rb b/lib/rucaptcha.rb index 98d0bfd..380f1a5 100644 --- a/lib/rucaptcha.rb +++ b/lib/rucaptcha.rb @@ -19,8 +19,9 @@ module RuCaptcha if Rails.application @config.cache_store = Rails.application.config.cache_store else - @config.cache_store = :null_store + @config.cache_store = :mem_cache_store end + @config.cache_store @config end @@ -32,6 +33,27 @@ module RuCaptcha style = config.style == :colorful ? 1 : 0 self.create(style) end + + def check_cache_store! + cache_store = RuCaptcha.config.cache_store + store_name = cache_store.is_a?(Array) ? cache_store.first : cache_store + if [:memory_store, :null_store, :file_store].include?(store_name) + RuCaptcha.config.cache_store = [:file_store, Rails.root.join('tmp/cache/rucaptcha/session')] + + puts " + + RuCaptcha's cache_store requirements are stored across processes and machines, + such as :mem_cache_store, :redis_store, or other distributed storage. + But your current set is #{cache_store}, it has changed to :file_store for working. + NOTE: :file_store is still not a good way, it only works with single server case. + + Please make config file `config/initializes/rucaptcha.rb` to setup `cache_store`. + More infomation please read GitHub RuCaptcha README file. + https://github.com/huacnlee/rucaptcha + +" + end + end end end diff --git a/lib/rucaptcha/engine.rb b/lib/rucaptcha/engine.rb index e4ff30f..1041af3 100644 --- a/lib/rucaptcha/engine.rb +++ b/lib/rucaptcha/engine.rb @@ -2,26 +2,14 @@ module RuCaptcha class Engine < ::Rails::Engine isolate_namespace RuCaptcha - initializer 'rucaptcha.prepend.cache' do - cache_store = RuCaptcha.config.cache_store - store_name = cache_store.is_a?(Array) ? cache_store.first : cache_store - if [:memory_store, :null_store, :file_store].include?(store_name) - msg = " - - RuCaptcha's cache_store requirements are stored across processes and machines, - such as :mem_cache_store, :redis_store, or other distributed storage. - But your current set is :#{store_name}. - - Please make config file `config/initializes/rucaptcha.rb` to setup `cache_store`. - More infomation please read GitHub RuCaptcha README file. - -" - if store_name == :null_store - raise msg - else - puts msg - end + initializer 'rucaptcha.init' do |app| + # https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/action_dispatch/routing/route_set.rb#L268 + # `app.routes.append` start from Rails 3.2 - 5.0 + app.routes.append do + mount RuCaptcha::Engine => '/rucaptcha' end + + RuCaptcha.check_cache_store! end end end