Module: Kamcaptcha::Helper

Defined in:
lib/kamcaptcha/helper.rb

Constant Summary collapse

DEFAULT_LABEL =
"Please type the characters in the image below"

Instance Method Summary collapse

Instance Method Details

#kamcaptcha(options = {}) ⇒ Object

Usage: <%= kamcaptcha :label => “Please prove that you’re a human” %>



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kamcaptcha/helper.rb', line 8

def kamcaptcha(options = {})
  label = options.fetch(:label, DEFAULT_LABEL)
  template = options.fetch(:template, Kamcaptcha.template)

  image = Kamcaptcha.random

  token = image.split(".").first
  token = instance_exec(token, &Kamcaptcha::Token.generator) if Kamcaptcha::Token.generator

  image = File.join('/', Kamcaptcha.prefix, image)

  instance_exec(token, &Kamcaptcha::Token.store) if Kamcaptcha::Token.store

  # Makes sure the ERB template only gets these variables: label, token, image
  form = ERB.new(template).result(OpenStruct.new(:label => label, :token => token, :image => image).send(:binding))

  form = form.html_safe if form.respond_to?(:html_safe)
  form
end