simple_captcha

https://github.com/kares/simple_captcha

rails plugin install git://github.com/kares/simple_captcha.git

--这个版本已经支持rails3.x

Installation

ruby script/plugin install git://github.com/kares/simple_captcha.git

Setup

After installation, follow these simple steps to setup the plugin. The setup will depend on the version of rails your application is using.
STEP 1

for Rails 3.x :

rails generate simple_captcha

STEP 2

rake db:migrate

STEP 3 (Optional)

configure simple_captcha e.g. in app/config/initializers/simple_captcha.rb

SimpleCaptcha.backend = :quick_magick # default is :RMagick

SimpleCaptcha.image_options = {
    :image_color => 'white',
    :image_size => '110x30',
    :text_color => 'black',
    :text_font => 'arial',
    :text_size => 22
} # these are the defaults

Usage
Controller Based

Include SimpleCaptcha::ControllerValidation into Your captcha validating controller or put the include into app/controllers/application.rb

ApplicationController < ActionController::Base
  include SimpleCaptcha::ControllerValidation
end

in the view file within the form tags add this code

<%= show_simple_captcha %>

and in the controller's action authenticate it as

if simple_captcha_valid?
  do this
else
  do that
end

Model Based

In the view file within the form tags write this code

<%= show_simple_captcha(:object=>"user") %>

and in the model class include SimpleCaptcha::ModelValidation and setup the validation

class User < ActiveRecord::Base
  include SimpleCaptcha::ModelValidation

  validates_captcha : o n => :create, :message => 'invalid captcha'
end

or if You prefer the old version which doesn't trigger the captcha validation on save (one have to call save_with_captcha)

class User < ActiveRecord::Base
  include SimpleCaptcha::ModelValidation

  apply_simple_captcha :message => :'invalid_captcha'
end

Options & Examples

View Options
==========================================================================

:label
--------------------------------------------------------------------------
  provides the custom text b/w the image and the text field,
  the default is "type the code from the image"

:image_style
--------------------------------------------------------------------------
  Provides the specific image style for the captcha image.
  There are eight different styles available with the plugin as...
  1) simply_blue
  2) simply_red
  3) simply_green
  4) charcoal_grey
  5) embosed_silver
  6) all_black
  7) distorted_black
  a lmost_invisible

  See the included samples <http://github.com/kares/simple_captcha/samples>.
  You can also specify 'random' to select the random image style.


:distortion
--------------------------------------------------------------------------
  Handles the complexity of the image. The :distortion can be set to 'low',
  'medium' or 'high'. Default is 'low'.

:object
--------------------------------------------------------------------------
  the name of the object of the model class, to implement the model based
  captcha.


How to change the CSS for SimpleCaptcha DOM elements ?
-----------------------------------------------------
You can change the CSS of the SimpleCaptcha DOM elements as per your need
in this file.
For Rails >= 2.0 the file wiil reside as...
"/app/views/simple_captcha/_simple_captcha.erb"
For Rails < 2.0 the file will reside as...
"/app/views/simple_captcha/_simple_captcha.rhtml"


View's Examples
==========================================================================

Controller Based Example
--------------------------------------------------------------------------
  example
  -------
  <%= show_simple_captcha(:label => "human authentication") %>

  example
  -------
  <%= show_simple_captcha(:label       => "human authentication",
                          :image_style => 'embosed_silver') %>

  example
  -------
  <%= show_simple_captcha(:label       => "human authentication",
                          :image_style => 'simply_red',
                          :distortion  => 'medium') %>

Model Based Example
--------------------------------------------------------------------------

  example
  -------
  <%= show_simple_captcha(:object => 'user',
                          :label  => "human authentication") %>



Model Options
==========================================================================

:message
--------------------------------------------------------------------------
  provides the custom message on failure of captcha authentication
  the default is "Secret Code did not match with the Image"

:add_to_base
--------------------------------------------------------------------------
  if set to true, appends the error message to the base.

Model's Example
==========================================================================

  example
  -------
  class User < ActiveRecord::Base
    apply_simple_captcha # the "old" way using save_with_captcha
  end

  example
  -------
  class User < ActiveRecord::Base
    validates_captcha :message => "Are you a bot?", :add_to_base => true
  end

To disable the validations in test mode, You should now state it explicitly:

class User < ActiveRecord::Base
  validates_captcha :unless => lambda { Rails.env.test? }
end

NOTE: This will validate captcha every-time You do a user.save !

There's an API that allows You to (temporary) disable captcha validation for classes, individual instances or even blocks :

User.captcha_validation = false # disables validation globally
user = User.new
...
# force captcha validation for the given instance and block
user.captcha_validation(true) do
  user.save!
end
...
# enable captcha validation for the given instance
user.captcha_validation(true)
user.save
...
# reset captcha validation - fallback to the class setting
user.captcha_validation(nil)
user.save # validates captcha if User.captcha_validation?

你可能感兴趣的:(css,git,Ruby,Rails,ActiveRecord)