Skip to content
This repository was archived by the owner on Feb 2, 2018. It is now read-only.
Open
Show file tree
Hide file tree
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
75 changes: 36 additions & 39 deletions lib/mongoid/encryptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,52 +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
# Overrides Mongo version
#
# @param [String] key
# @return [Object]
def read_attribute_for_validation(key)
v = send(key)
(v.respond_to?(:encrypted?) && 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 = send(attr_name)
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

# @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)
cipher = instantiate_cipher(cipher_class, options)
value = cipher.encrypt(value)
value.cipher = cipher
send("#{attr_name}=", value)
end

unless value.blank? || value.encrypted? || attribute_changed?(attr_name) || new_record?
value.cipher = instantiate_cipher(cipher_class, options)
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)

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
end

# @param [Class] cipher_class
# @param [Hash] options
# @return [EncryptedStrings::Cipher]
def instantiate_cipher(cipher_class, options)
cipher_class.new(options.dup)
end
end
end
2 changes: 1 addition & 1 deletion mongoid-encryptor.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Gem::Specification.new do |s|
s.rubyforge_project = "mongoid-encryptor"

s.add_dependency("activesupport", "~> 3.0")
s.add_dependency("mongoid", "~> 2")
s.add_dependency("mongoid", "~> 3")
s.add_dependency("encrypted_strings", "~> 0.3.3")
s.add_development_dependency("bson_ext", "~> 1.4.0")
s.add_development_dependency("database_cleaner", "~> 0.6.7")
Expand Down