Module: Catptcha

Defined in:
lib/catptcha.rb

Defined Under Namespace

Modules: Seed Classes: Photo, PhotoGroup

Constant Summary collapse

VERSION =
'0.1.3'

Class Method Summary collapse

Class Method Details

.check_guess(*keys) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/catptcha.rb', line 47

def check_guess *keys
  guesses = keys.flatten.compact
  if guesses.size != 3
    return false
  end
  3 == Photo.count(:include => 'photo_group', :conditions => [
    'catptcha_photos.key in (?) and kittens = ?', guesses, true
  ])
end

.kittensObject

Get a list of 3 kitten photos



6
7
8
# File 'lib/catptcha.rb', line 6

def kittens
  PhotoGroup.kittens.random(3)
end

.not_kittensObject

Get a list of 6 non-kitten photos



11
12
13
# File 'lib/catptcha.rb', line 11

def not_kittens
  PhotoGroup.not_kittens.random(6)
end

.puzzleObject



15
16
17
# File 'lib/catptcha.rb', line 15

def puzzle
  (kittens + not_kittens).sort_by {rand}
end

.puzzle_jsObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/catptcha.rb', line 19

def puzzle_js
  return "      function catptcha_click(e) {\n        if (e.checked) {\n          e.parentNode.getElementsByTagName('img')[0].setAttribute(\"style\",\"border: 2px solid red;\");\n        } else {\n          e.parentNode.getElementsByTagName('img')[0].setAttribute(\"style\", \"padding: 2px\");\n        }\n      }\n  EOJS\nend\n"

.puzzle_tagsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/catptcha.rb', line 30

def puzzle_tags
  tags = '<div class="catptcha" style="width:340px"><p>Click the 3 kitties!</p>'
  puzzle.each do |photo|
    tags << "    <span class=\"catptcha_guess\" style=\"float:left\">\n      <input type=\"checkbox\" id=\"catptcha_guess_\#{photo.key}\" name=\"catptcha_guess[]\" value=\"\#{ photo.key }\" onclick=\"catptcha_click(this)\" style=\"opacity:0.0;width:0px;\"/>\n      <label for=\"catptcha_guess_\#{photo.key }\" style=\"margin:2px\">\n        <img src=\"\#{ photo.url }\" title=\"\#{photo.attribution}\" alt=\"\#{photo.attribution}\" style=\"padding: 2px\" />\n      </label>\n    </span>\n    EOT\n  end\n  tags << '</div>'\n  tags << \"<script>\#{puzzle_js}</script>\"\n  tags\nend\n"