Sha256: bc12cffd8d11bdde4dd67bc3ee03065f558932380b583523626b09f62357f317

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 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.2 lib/rmagick_captcha/model_additions.rb