rsanheim / brain_buster

BrainBuster - a logic captcha for Rails

This URL has Read+Write access

brain_buster / lib / brain_buster.rb
100644 45 lines (36 sloc) 1.061 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require 'humane_integer'
 
 # Simple model to hold sets of questions and answers.
class BrainBuster < ActiveRecord::Base
  VERSION = "0.8.3"
 
  # Attempt to answer a captcha, returns true if the answer is correct.
  def attempt?(string)
    string = string.strip.downcase
    if answer_is_integer?
      return string == answer || string == HumaneInteger.new(answer.to_i).to_english
    else
      return string == answer.downcase
    end
  end
 
  def self.find_random_or_previous(id = nil)
    id ? find_specific_or_fallback(id) : find_random
  end
 
  def self.random_function
    case connection.adapter_name.downcase
      when /sqlite/, /postgres/ then "random()"
      else "rand()"
    end
  end
 
  private
  
  def self.find_random
    find(:first, :order => random_function)
  end
  
  def self.find_specific_or_fallback(id)
    find(id)
  rescue ActiveRecord::RecordNotFound
    find_random
  end
  
  def answer_is_integer?
    int_answer = answer.to_i
    (int_answer != 0) || (int_answer == 0 && answer == "0")
  end
  
end