Sha256: 10b0915d699c2894d2b9d1ed333e5fc3f5799512cfe5fa5419649374b8c526b6

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module ActiveRecord
  class Base
    class << self
      def attr_encrypted_search(*params)
        define_attribute_methods rescue nil

        options   = params.last.is_a?(Hash) ? params.pop.dup : {}
        compress  = options.delete(:compress) || false
        type      = options.delete(:type) || :string

        raise "Invalid type: #{type.inspect}. Valid types: #{SymmetricEncryption::COERCION_TYPES.inspect}" unless SymmetricEncryption::COERCION_TYPES.include?(type)

        options.each {|option| warn "Ignoring unknown option #{option.inspect} supplied to attr_encrypted_search with #{params.inspect}"}

        if const_defined?(:EncryptedSearchAttributes, _search_ancestors = false)
          mod = const_get(:EncryptedSearchAttributes)
        else
          mod = const_set(:EncryptedSearchAttributes, Module.new)
          include mod
        end

        params.each do |attribute|
          mod.module_eval(<<-ENCRYPTEDSEARCH, __FILE__, __LINE__ + 1)
            def #{attribute}=(value)
              if value
                self.encrypted_search_#{attribute} = ::SymmetricEncryption.encrypt(value.downcase,false,#{compress},:#{type})
              end
              super(value)
            end
          ENCRYPTEDSEARCH
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
encrypted_search_attributes-1.0.1 lib/encrypted_search_attributes/extensions/active_record/base.rb