Module: RmagickCaptcha::ModelAdditions::ClassMethods

Defined in:
lib/rmagick_captcha/model_additions.rb

Instance Method Summary collapse

Instance Method Details

#has_rmagick_captcha(*symbols) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rmagick_captcha/model_additions.rb', line 4

def has_rmagick_captcha(*symbols)
  symbols.each do |sym|
    define_method("#{sym}_key") do
      @attributes["#{sym}_key"]
    end
    define_method("#{sym}_key=") do |val|
      @attributes["#{sym}_key"]=val
    end
    define_method("#{sym}_text") do
      @attributes["#{sym}_text"]
    end
    define_method("#{sym}_text=") do |val|
      @attributes["#{sym}_text"]=val
    end

    define_method("validate_rmagick_#{sym}") do
      text  = @attributes["#{sym}_text"]
      key = @attributes["#{sym}_key"]
      if ::Rails.env != 'test' && text != key
        self.errors.add(:"#{sym}_text")
        false
      end
    end

    # First parameter should be options hash
    define_method("reset_#{sym}_key") do |*argv|
      result = ""
      parameter1 = argv.first || Hash.new
      options = ::RmagickCaptcha.options.merge(parameter1)
      options[:captcha_key_len].times do
        size = ::RmagickCaptcha.options[:random_chars].size;
        result << ::RmagickCaptcha.options[:random_chars][rand(size)]
      end
      @attributes["#{sym}_key"] = result
      @attributes["#{sym}_text"] = nil
      result
    end
  end
end