From 6543b3aaf8cf878c5ee4151ed585cb351d2ee6e4 Mon Sep 17 00:00:00 2001 From: Nader Akhnoukh Date: Tue, 9 Oct 2012 21:54:32 -0600 Subject: [PATCH 1/3] rails 3.2 support --- lib/mongoid/encryptor.rb | 74 +++++++++++++++++++-------------------- mongoid-encryptor.gemspec | 2 +- 2 files changed, 37 insertions(+), 39 deletions(-) diff --git a/lib/mongoid/encryptor.rb b/lib/mongoid/encryptor.rb index 8e53dcf..360eb09 100644 --- a/lib/mongoid/encryptor.rb +++ b/lib/mongoid/encryptor.rb @@ -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 diff --git a/mongoid-encryptor.gemspec b/mongoid-encryptor.gemspec index c8d14ef..34bf626 100644 --- a/mongoid-encryptor.gemspec +++ b/mongoid-encryptor.gemspec @@ -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") From 450217f2b86b41b0908e2de5935d03cc00f8321f Mon Sep 17 00:00:00 2001 From: Nader Akhnoukh Date: Wed, 10 Oct 2012 11:57:08 -0600 Subject: [PATCH 2/3] fix to work with mongoid 3 --- lib/mongoid/encryptor.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mongoid/encryptor.rb b/lib/mongoid/encryptor.rb index 360eb09..d528e5c 100644 --- a/lib/mongoid/encryptor.rb +++ b/lib/mongoid/encryptor.rb @@ -32,13 +32,13 @@ def encrypts(*attrs) end end - # Returns decrypted value for key. + # Overrides Mongo version # # @param [String] key # @return [Object] def read_attribute_for_validation(key) - v = read_attribute(key) - v.try(:encrypted?) ? v.decrypt : v + v = super(key) + v && v.respond_to?(:encrypted?) && v.encrypted? ? v.decrypt : v end private From 739c031bdf959bcfbae10cdb4c01e2f075f6af38 Mon Sep 17 00:00:00 2001 From: icebreaker Date: Tue, 23 Oct 2012 12:21:15 +0300 Subject: [PATCH 3/3] fix, fix, fix --- lib/mongoid/encryptor.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/mongoid/encryptor.rb b/lib/mongoid/encryptor.rb index d528e5c..71cf047 100644 --- a/lib/mongoid/encryptor.rb +++ b/lib/mongoid/encryptor.rb @@ -37,8 +37,8 @@ def encrypts(*attrs) # @param [String] key # @return [Object] def read_attribute_for_validation(key) - v = super(key) - v && v.respond_to?(:encrypted?) && v.encrypted? ? v.decrypt : v + v = send(key) + (v.respond_to?(:encrypted?) && v.try(:encrypted?)) ? v.decrypt : v end private @@ -47,7 +47,7 @@ def read_attribute_for_validation(key) # @param [Class] cipher_class # @param [Hash] options def write_encrypted_attribute(attr_name, cipher_class, options) - value = read_attribute(attr_name.to_sym) + value = send(attr_name) return if value.blank? or value.encrypted? cipher = instantiate_cipher(cipher_class, options) @@ -77,5 +77,4 @@ def instantiate_cipher(cipher_class, options) cipher_class.new(options.dup) end end - end