- 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.
This commit is contained in:
Jason Lee 2017-03-08 11:54:36 +08:00
parent afc6b47ff1
commit bb5b4efb8a
3 changed files with 50 additions and 33 deletions

View File

@ -35,28 +35,20 @@ RuCaptcha.configure do
 # self.style = :colorful  # self.style = :colorful
# Custom captcha code expire time if you need, default: 2 minutes # Custom captcha code expire time if you need, default: 2 minutes
# self.expires_in = 120 # self.expires_in = 120
 # [Requirement/重要]  # [Requirement / 重要]
# Store Captcha code where, this config more like Rails config.cache_store # Store Captcha code where, this config more like Rails config.cache_store
# default: Read config info from `Rails.application.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] # 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  # 但如果是 [:null_store, :memory_store, :file_store] 之类的,你可以通过下面的配置项单独给 RuCaptcha 配置 cache_store
 self.cache_store = :mem_cache_store  self.cache_store = :mem_cache_store
end 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` Controller `app/controller/account_controller.rb`
When you called `verify_rucaptcha?`, it will uses value from `params[:_rucaptcha]` to validation.
```rb ```rb
class AccountController < ApplicationController class AccountController < ApplicationController
def create def create
@ -68,6 +60,17 @@ class AccountController < ApplicationController
end end
end 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`. > 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` View `app/views/account/new.html.erb`
```erb ```erb
<form> <form method="POST">
... ...
<div class="form-group"> <div class="form-group">
<%= rucaptcha_input_tag(class: 'form-control', placeholder: 'Input Captcha') %> <%= rucaptcha_input_tag(class: 'form-control', placeholder: 'Input Captcha') %>
<%= rucaptcha_image_tag(alt: 'Captcha') %> <%= rucaptcha_image_tag(alt: 'Captcha') %>
</div> </div>
... ...
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form> </form>
``` ```

View File

@ -19,8 +19,9 @@ module RuCaptcha
if Rails.application if Rails.application
@config.cache_store = Rails.application.config.cache_store @config.cache_store = Rails.application.config.cache_store
else else
@config.cache_store = :null_store @config.cache_store = :mem_cache_store
end end
@config.cache_store
@config @config
end end
@ -32,6 +33,27 @@ module RuCaptcha
style = config.style == :colorful ? 1 : 0 style = config.style == :colorful ? 1 : 0
self.create(style) self.create(style)
end 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
end end

View File

@ -2,26 +2,14 @@ module RuCaptcha
class Engine < ::Rails::Engine class Engine < ::Rails::Engine
isolate_namespace RuCaptcha isolate_namespace RuCaptcha
initializer 'rucaptcha.prepend.cache' do initializer 'rucaptcha.init' do |app|
cache_store = RuCaptcha.config.cache_store # https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/action_dispatch/routing/route_set.rb#L268
store_name = cache_store.is_a?(Array) ? cache_store.first : cache_store # `app.routes.append` start from Rails 3.2 - 5.0
if [:memory_store, :null_store, :file_store].include?(store_name) app.routes.append do
msg = " mount RuCaptcha::Engine => '/rucaptcha'
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
end end
RuCaptcha.check_cache_store!
end end
end end
end end