Add field_name in PlainCaptcha config

This commit is contained in:
Guillaume DOTT 2013-01-11 14:40:58 +01:00
parent 0203ae018e
commit caaf1b0fd8
2 changed files with 10 additions and 6 deletions

View File

@ -44,8 +44,9 @@ ReverseCaptcha and PlainCaptcha must be configured separately.
### PlainCaptcha ### PlainCaptcha
``` ```
ReallySimpleCaptcha::Captcha::PlainCaptcha.configure do |config| ReallySimpleCaptcha::Captcha::PlainCaptcha.configure do |config|
config.text_length = 6 config.field_name = :plain_captcha
config.text_length = 6
# colors # colors
config.fill = 'darkblue' config.fill = 'darkblue'
config.background_color = 'white' config.background_color = 'white'
@ -61,7 +62,7 @@ end
### ReverseCaptcha ### ReverseCaptcha
``` ```
ReallySimpleCaptcha::Captcha::ReverseCaptcha.configure do |config| ReallySimpleCaptcha::Captcha::ReverseCaptcha.configure do |config|
config.field_name = 'reverse_captcha' config.field_name = :reverse_captcha
end end
``` ```

View File

@ -4,6 +4,8 @@ require 'base64'
module ReallySimpleCaptcha::Captcha module ReallySimpleCaptcha::Captcha
module PlainCaptcha module PlainCaptcha
include ActiveSupport::Configurable include ActiveSupport::Configurable
config_accessor :field_name
config_accessor :implode_amount config_accessor :implode_amount
config_accessor :wave_amplitude config_accessor :wave_amplitude
config_accessor :wave_length config_accessor :wave_length
@ -12,6 +14,7 @@ module ReallySimpleCaptcha::Captcha
config_accessor :background_color config_accessor :background_color
config_accessor :text_length config_accessor :text_length
config.field_name = :plain_captcha
config.text_length = 6 config.text_length = 6
def self.configure(&block) def self.configure(&block)
@ -20,7 +23,7 @@ module ReallySimpleCaptcha::Captcha
module ViewHelpers module ViewHelpers
def plain_captcha_tag def plain_captcha_tag
session[:plain_captcha] = ReallySimpleCaptcha::Util.random_string(PlainCaptcha.text_length) session[PlainCaptcha.field_name] = ReallySimpleCaptcha::Util.random_string(PlainCaptcha.text_length)
image = PlainCaptcha.generate_image(session[:plain_captcha], { image = PlainCaptcha.generate_image(session[:plain_captcha], {
implode_amount: PlainCaptcha.implode_amount, implode_amount: PlainCaptcha.implode_amount,
@ -33,7 +36,7 @@ module ReallySimpleCaptcha::Captcha
content_tag :div, class: 'plain_captcha' do content_tag :div, class: 'plain_captcha' do
html = image_tag "data:image/gif;base64,#{image}", alt: "Captcha" html = image_tag "data:image/gif;base64,#{image}", alt: "Captcha"
html.concat text_field_tag 'plain_captcha', nil, required: 'required', autocomplete: 'off' html.concat text_field_tag PlainCaptcha.field_name, nil, required: 'required', autocomplete: 'off'
html html
end end
@ -42,8 +45,8 @@ module ReallySimpleCaptcha::Captcha
module ControllerHelpers module ControllerHelpers
def plain_captcha_valid? def plain_captcha_valid?
res = params[:plain_captcha] == session[:plain_captcha] res = params[PlainCaptcha.field_name] == session[PlainCaptcha.field_name]
session[:plain_captcha] = nil session[PlainCaptcha.field_name] = nil
res res
end end