Module: TextCaptcha::Validation::InstanceMethods

Defined in:
lib/text_captcha/validation.rb

Instance Method Summary collapse

Instance Method Details

#all_challengesObject

Return all questions.



95
96
97
# File 'lib/text_captcha/validation.rb', line 95

def all_challenges
  @all_challenges ||= I18n.t("text_captcha.challenges")
end

#challengeObject

Return the question string.



79
80
81
# File 'lib/text_captcha/validation.rb', line 79

def challenge
  current_challenge.first
end

#challenge_answersObject

Return all accepted answers.



84
85
86
# File 'lib/text_captcha/validation.rb', line 84

def challenge_answers
  current_challenge.last
end

#challenge_idObject

Return the question id. If none is assigned it chooses on randomly.



90
91
92
# File 'lib/text_captcha/validation.rb', line 90

def challenge_id
  @challenge_id ||= Kernel.rand(all_challenges.count)
end

#current_challengeObject

Return an array with the current question and its answers.



74
75
76
# File 'lib/text_captcha/validation.rb', line 74

def current_challenge
  all_challenges[challenge_id.to_i]
end

#valid_challenge_answer?Boolean

Detect if the answer is correct. Will also return true if TextCaptcha.enabled is set to false.

Returns:

  • (Boolean)


101
102
103
104
105
106
107
# File 'lib/text_captcha/validation.rb', line 101

def valid_challenge_answer?
  return false unless current_challenge
  return true unless TextCaptcha.enabled

  answers = challenge_answers.collect {|a| to_captcha(a)}
  !challenge_answer.blank? && answers.include?(to_captcha(challenge_answer))
end