Captcha in Ruby on Rails – Customize the use of captcha in the plugin validates_captcha
Hello Everyone !!
I have released a captcha plugin Simple Captcha. It is really simple to implement, and provides a cool feature of multiple styles of images.
Previous Post for validates_captcha
To implement captcha in RubyonRails, validates_captcha plugin can be a good option but a small customization i need with this plugin was to use it on some specific action and not to be validated the captcha field every time an instance of the model is saved or updated.
Here is a small work-around for its customization…
How to use customized captcha in RoR ?
Install the plugin validates_captcha in your rails application by running this command from the root of your application
ruby script/plugin install http://svn.2750flesk.com/validates_captcha
Make sure that you can now see the directory vedor/plugins/validates_captcha.
Now run these commands from your application root to make the image and data directories
ruby script/generate captcha store_directory ruby script/generate captcha image_directory
Here is the complete API for the usage of this plugin. I am describing the same idea as given in this API but in a bit more specific means.
Lets consider a model User in which we will implement the captcha.
Add the following code in the file app/models/user.rb
class User < ActiveRecord::Base
validates_captcha :if => :request_captcha_validation?
attr_accessor :request_captcha_validation
def request_captcha_validation?
(self.request_captcha_validation==true)? true : false
end
end
Handle View and Controller
Add the code in the view inside your existing form.
<% c = prepare_captcha :type => :image -%> <%= captcha_hidden_field c, 'user' %> <%= captcha_image_tag c %> <%= captcha_label 'user', 'Type in the text from the image above' %> <%= captcha_text_field 'user' %>
Your controller will look like
def save
# the line in bold represents that you need captcha validation.
# if captcha validation is not required then remove this line from your controller.
@user = User.new(params[:user])
@user.request_captcha_validation = true
@user.save
end
However image is too noisy and it contains repeated strings.
To improve the quality of images generated by the plugin validates_captcha visit Here.
How to improve the image quality and generate random string image in the plugin validates_captcha
Validates captchais a good pluging to implement captcha in your rails application.
However i found that there is repetition of the string of the image and the quality of image is not that good. To get a good quality image and random string replace the code of the file /vendor/plugins/validates_captcha/lib/captcha_challenge.rb with the following code…
require 'digest/sha1'
module AngryMidgetPluginsInc #:nodoc:
# CaptchaChallenge
class CaptchaChallenge
include CaptchaConfig
extend CaptchaConfig
DEFAULT_TTL = 1200#Lifetime in seconds. Default is 20 minutes.
attr_reader :id, :created_at
attr_accessor :ttl
def initialize(options = {}) #:nodoc:
generate_id
options = {
:ttl => config['default_ttl'] || DEFAULT_TTL}.update(options)
self.ttl = options[:ttl]
@created_at = Time.now
self.class.prune
end
# Implement in subclasses.
def correct? #:nodoc:
raise NotImplementedError
end
private
def generate_id #:nodoc:
self.id = Digest::SHA1.hexdigest(Time.now.to_s+rand.to_s)
end
def id=(i) #:nodoc:
@id = i
end
def write_to_store #:nodoc:
store.transaction{
store[:captchas] = Array.new unless store.root?(:captchas)
store[:captchas] <<> c.created_at+c.ttl
store[:captchas].delete_at(i)
end
}
end
}
end#prune
end#class << words =" 'gorilla" default_dir =" 'captcha'#public/images/captcha" write_dir =" File.join(RAILS_ROOT," default_filetype =" 'jpg'" options =" {})" options =" {" string =""> config['words'] ? config['words'][rand(config['words'].size)] : WORDS[rand(WORDS.size)],
:dir => config['default_dir'] || DEFAULT_DIR,
:filetype => config['default_filetype'] || DEFAULT_FILETYPE
}.update(options)
#changed
self.string = Digest::SHA1.hexdigest(Time.now.to_s)[0..4] #options[:string]
self.dir = options[:dir]
self.filetype = options[:filetype]
self.filename = options[:filename] || generate_filename
write_to_store
end
# Generates the image.
def generate(options = {})
options = {
:fontsize => 50,
:padding => 20,
:color => '#000',
:background => '#fff',
:fontweight => 'bold',
:rotate => true
}.update(options)
options[:fontweight] = case options[:fontweight]
when 'bold' then 700
else 400
end
#added
text = Array.new
0.upto(4) do |i|
text[i] = Magick::Draw.new
text[i].pointsize = options[:fontsize]
text[i].font_weight = 300 #options[:fontweight]
text[i].fill = 'blue' #options[:color]
text[i].gravity = Magick::CenterGravity
text[i].rotation = (rand(2)==1 ? rand(30) : -rand(30)) if options[:rotate]
end
metric = text[2].get_type_metrics(self.string)
#add bg
canvas = Magick::ImageList.new
fill = Magick::HatchFill.new('white','lime')
x = metric.width+options[:padding]
y = metric.height+options[:padding]
#ADDING LINES
img1 = Magick::Image.new(x,y,fill)
gc = Magick::Draw.new
gc.stroke_linejoin('round')
gc.stroke('blue')
gc.stroke_width(2)
gc.line(rand(x),rand(y),rand(x),rand(y))
gc.line(rand(x),rand(y),rand(x),rand(y))
gc.line(rand(x),rand(y),rand(x),rand(y))
gc.draw(img1)
canvas << background_color =" '#000F'" y_loc =" rand(y)" p =" Magick::Pixel.from_color(options[:background])" opacity =" Magick::MaxRGB" background_color =" p" image =" canvas.flatten_images.blur_image(1)" dir =" self.dir," filename =" self.filename)" downcase ="=">public/images.
def file_path
File.join(dir,filename)
end
class <<> c.created_at+c.ttl
if File.exists?(File.join(WRITE_DIR, c.file_path))
begin
File.unlink(File.join(WRITE_DIR, c.file_path))
rescue Exception
end
end
end
}
end
}
super
end#prune
end#class << image="(i)" image =" i">
visit Here to view the customized use of the plugin validates_captcha in RoR.


Working with Vinsol has been one of the most rewarding and productive collaborations I've ever had in the technology industry. Manik is an intelligent and honest engineer with a great faculty for open and clear communication, and his team include some of the swiftest and keenest developers I've ever had the pleasure to work with. They're great value for money, excellent people who are a joy to know and interact with, and above all they really know their stuff