Module: AttributeQueryableEncrypted::Adapters::ActiveRecord::ClassMethods

Defined in:
lib/attribute_queryable_encrypted/adapters/active_record.rb

Instance Method Summary collapse

Instance Method Details

#attribute_queryable_encrypted(*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/attribute_queryable_encrypted/adapters/active_record.rb', line 13

def attribute_queryable_encrypted *args
  define_attribute_methods rescue nil
  super *args
  
  args.reject { |arg| arg.is_a?(Hash) }.each do |attribute|
    options = queryable_encrypted_attributes[attribute]
    
    find_all_by_method = proc do |prefix_value|
      send("find_all_by_#{[options[:prefix], attribute, options[:suffix]].join('_')}", prefix_encrypt(prefix_value, options))
    end

    find_by_method = proc do |original_value| 
      send("find_all_by_#{[options[:prefix], attribute].join('_')}", original_value.prefix(options[:length])).detect do |result|
        result.send(attribute).match original_value
      end
    end
    
    alias_method "original_find_by_#{attribute}", "find_by_#{attribute}" if respond_to?(attribute)
    
    define_singleton_method "find_all_by_#{[options[:prefix], attribute].join('_')}", find_all_by_method
    define_singleton_method "find_by_#{attribute}", find_by_method

  end
end