Sha256: 1b09ec389b3af87637df3b9189d80ffdd0a8fe1eda05381c18344f90c598ceb3

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

#
module RmagickCaptcha
  module ModelAdditions
    # ::Rails.logger.error("...")

    module ClassMethods
      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
    end


  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rmagick_captcha-0.6.3 lib/rmagick_captcha/model_additions.rb