Module: TinyCaptcha::ModelHelpers::InstanceMethods

Defined in:
lib/tiny_captcha/active_record.rb

Instance Method Summary collapse

Instance Method Details

#is_captcha_valid?Boolean



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tiny_captcha/active_record.rb', line 61

def is_captcha_valid?
  return true if Rails.env.test?

  if captcha && captcha.upcase.delete(" ") == ::TinyCaptcha::Utils::tiny_captcha_value(captcha_key)
    ::TinyCaptcha::Utils::tiny_captcha_passed!(captcha_key)
    return true
  else
    message = tiny_captcha_options[:message] || I18n.t(
      self.class.model_name.to_s.downcase, :scope => [:tiny_captcha, :message], :default => :default
    )

    # tiny_captcha_options[:add_to_base] ? errors.add(:base, message) : errors.add(:captcha, message)
    errors.add(:captcha, message)
    return false
  end
end

#save_with_captchaObject



82
83
84
85
# File 'lib/tiny_captcha/active_record.rb', line 82

def save_with_captcha
  # valid_with_captcha? && save(:validate => false)
  valid_with_captcha? && self.save
end

#save_with_captcha!Object



87
88
89
# File 'lib/tiny_captcha/active_record.rb', line 87

def save_with_captcha!
  valid_with_captcha? && self.save!
end

#tiny_captcha_valid?Boolean



57
58
59
# File 'lib/tiny_captcha/active_record.rb', line 57

def tiny_captcha_valid?
  self.errors[:captcha].empty?
end

#update_attributes_with_captcha(*args) ⇒ Object



78
79
80
# File 'lib/tiny_captcha/active_record.rb', line 78

def update_attributes_with_captcha *args
  valid_with_captcha? && update_attributes(*args)
end

#valid_with_captcha?Boolean



53
54
55
# File 'lib/tiny_captcha/active_record.rb', line 53

def valid_with_captcha?
  [valid?, is_captcha_valid?].all?
end