Module: Catptcha

Defined in:
lib/catptcha.rb

Defined Under Namespace

Modules: Seed Classes: Photo, PhotoGroup

Constant Summary collapse

VERSION =
'0.1.6'

Class Method Summary collapse

Class Method Details

.check_guess(*keys) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/catptcha.rb', line 59

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

.paginate_kittens(page) ⇒ Object



19
20
21
# File 'lib/catptcha.rb', line 19

def paginate_kittens(page)
  PhotoGroup.kittens.paginate(page)
end

.paginate_not_kittens(page) ⇒ Object



23
24
25
# File 'lib/catptcha.rb', line 23

def paginate_not_kittens(page)
  PhotoGroup.not_kittens.paginate(page)
end

.puzzleObject



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

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

.puzzle_jsObject



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

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

.puzzle_tags(param_name = 'catptcha_guess') ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/catptcha.rb', line 42

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

.remove_image(key) ⇒ Object



27
28
29
# File 'lib/catptcha.rb', line 27

def remove_image(key)
  Photo.first(:conditions => {:key => key}).try(:destroy)
end