Skip to content
This repository was archived by the owner on Feb 2, 2018. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 36 additions & 38 deletions lib/mongoid/encryptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,51 +32,49 @@ def encrypts(*attrs)
end
end

module InstanceMethods #:nodoc:
# Returns decrypted value for key.
#
# @param [String] key
# @return [Object]
def read_attribute_for_validation(key)
v = read_attribute(key)
v.try(:encrypted?) ? v.decrypt : v
end
# Returns decrypted value for key.
#
# @param [String] key
# @return [Object]
def read_attribute_for_validation(key)
v = read_attribute(key)
v.try(:encrypted?) ? v.decrypt : v
end

private
private

# @param [String] attr_name
# @param [Class] cipher_class
# @param [Hash] options
def write_encrypted_attribute(attr_name, cipher_class, options)
value = read_attribute(attr_name.to_sym)
return if value.blank? or value.encrypted?
# @param [String] attr_name
# @param [Class] cipher_class
# @param [Hash] options
def write_encrypted_attribute(attr_name, cipher_class, options)
value = read_attribute(attr_name.to_sym)
return if value.blank? or value.encrypted?

cipher = instantiate_cipher(cipher_class, options)
value = cipher.encrypt(value)
value.cipher = cipher
send("#{attr_name}=", value)
end
cipher = instantiate_cipher(cipher_class, options)
value = cipher.encrypt(value)
value.cipher = cipher
send("#{attr_name}=", value)
end

# @param [String] attr_name
# @param [Class] cipher_class
# @param [Hash] options
# @return [String]
def read_encrypted_attribute(attr_name, cipher_class, options)
value = read_attribute(attr_name)
# @param [String] attr_name
# @param [Class] cipher_class
# @param [Hash] options
# @return [String]
def read_encrypted_attribute(attr_name, cipher_class, options)
value = read_attribute(attr_name)

unless value.blank? || value.encrypted? || attribute_changed?(attr_name) || new_record?
value.cipher = instantiate_cipher(cipher_class, options)
end

value
unless value.blank? || value.encrypted? || attribute_changed?(attr_name) || new_record?
value.cipher = instantiate_cipher(cipher_class, options)
end

# @param [Class] cipher_class
# @param [Hash] options
# @return [EncryptedStrings::Cipher]
def instantiate_cipher(cipher_class, options)
cipher_class.new(options.dup)
end
value
end

# @param [Class] cipher_class
# @param [Hash] options
# @return [EncryptedStrings::Cipher]
def instantiate_cipher(cipher_class, options)
cipher_class.new(options.dup)
end
end

Expand Down