module AttrEncrypted::Adapters::ActiveRecord

Public Instance Methods

assign_attributes(*args) click to toggle source
   # File lib/attr_encrypted/adapters/active_record.rb
35 def assign_attributes(*args)
36   perform_attribute_assignment :assign_attributes_without_attr_encrypted, *args
37 end
attributes=(*args) click to toggle source
   # File lib/attr_encrypted/adapters/active_record.rb
41 def attributes=(*args)
42   perform_attribute_assignment :attributes_without_attr_encrypted=, *args
43 end
perform_attribute_assignment(method, new_attributes, *args) click to toggle source
   # File lib/attr_encrypted/adapters/active_record.rb
25 def perform_attribute_assignment(method, new_attributes, *args)
26   return if new_attributes.blank?
27 
28   send method, new_attributes.reject { |k, _|  self.class.encrypted_attributes.key?(k.to_sym) }, *args
29   send method, new_attributes.reject { |k, _| !self.class.encrypted_attributes.key?(k.to_sym) }, *args
30 end
reload(*args, &block) click to toggle source
   # File lib/attr_encrypted/adapters/active_record.rb
10 def reload(*args, &block)
11   result = reload_without_attr_encrypted(*args, &block)
12   self.class.encrypted_attributes.keys.each do |attribute_name|
13     instance_variable_set("@#{attribute_name}", nil)
14   end
15   result
16 end

Protected Instance Methods

attr_encrypted(*attrs) click to toggle source

attr_encrypted method

Calls superclass method
   # File lib/attr_encrypted/adapters/active_record.rb
50 def attr_encrypted(*attrs)
51   super
52   options = attrs.extract_options!
53   attr = attrs.pop
54   attribute attr if ::ActiveRecord::VERSION::STRING >= "5.1.0"
55   options.merge! encrypted_attributes[attr]
56 
57   define_method("#{attr}_was") do
58     attribute_was(attr)
59   end
60 
61   if ::ActiveRecord::VERSION::STRING >= "4.1"
62     define_method("#{attr}_changed?") do |options = {}|
63       attribute_changed?(attr, options)
64     end
65   else
66     define_method("#{attr}_changed?") do
67         attribute_changed?(attr)
68     end
69   end
70 
71   define_method("#{attr}_change") do
72     attribute_change(attr)
73   end
74 
75   define_method("#{attr}_with_dirtiness=") do |value|
76     attribute_will_change!(attr) if value != __send__(attr)
77     __send__("#{attr}_without_dirtiness=", value)
78   end
79 
80   alias_method "#{attr}_without_dirtiness=", "#{attr}="
81   alias_method "#{attr}=", "#{attr}_with_dirtiness="
82 
83   alias_method "#{attr}_before_type_cast", attr
84 end
attribute_instance_methods_as_symbols() click to toggle source
Calls superclass method
   # File lib/attr_encrypted/adapters/active_record.rb
86 def attribute_instance_methods_as_symbols
87   # We add accessor methods of the db columns to the list of instance
88   # methods returned to let ActiveRecord define the accessor methods
89   # for the db columns
90 
91   if connected? && table_exists?
92     columns_hash.keys.inject(super) {|instance_methods, column_name| instance_methods.concat [column_name.to_sym, :"#{column_name}="]}
93   else
94     super
95   end
96 end
attribute_instance_methods_as_symbols_available?() click to toggle source
    # File lib/attr_encrypted/adapters/active_record.rb
 98 def attribute_instance_methods_as_symbols_available?
 99   connected? && table_exists?
100 end
method_missing_with_attr_encrypted(method, *args, &block) click to toggle source

Allows you to use dynamic methods like find_by_email or scoped_by_email for encrypted attributes

NOTE: This only works when the :key option is specified as a string (see the README)

This is useful for encrypting fields like email addresses. Your user's email addresses are encrypted in the database, but you can still look up a user by email for logging in

Example

class User < ActiveRecord::Base
  attr_encrypted :email, key: 'secret key'
end

User.find_by_email_and_password('test@example.com', 'testing')
# results in a call to
User.find_by_encrypted_email_and_password('the_encrypted_version_of_test@example.com', 'testing')
    # File lib/attr_encrypted/adapters/active_record.rb
119 def method_missing_with_attr_encrypted(method, *args, &block)
120   if match = /^(find|scoped)_(all_by|by)_([_a-zA-Z]\w*)$/.match(method.to_s)
121     attribute_names = match.captures.last.split('_and_')
122     attribute_names.each_with_index do |attribute, index|
123       if attr_encrypted?(attribute) && encrypted_attributes[attribute.to_sym][:mode] == :single_iv_and_salt
124         args[index] = send("encrypt_#{attribute}", args[index])
125         warn "DEPRECATION WARNING: This feature will be removed in the next major release."
126         attribute_names[index] = encrypted_attributes[attribute.to_sym][:attribute]
127       end
128     end
129     method = "#{match.captures[0]}_#{match.captures[1]}_#{attribute_names.join('_and_')}".to_sym
130   end
131   method_missing_without_attr_encrypted(method, *args, &block)
132 end