lib/encrypted_id.rb in encrypted_id-1.0.0 vs lib/encrypted_id.rb in encrypted_id-1.1.0
- old
+ new
@@ -9,10 +9,11 @@
def encrypted_id(options = {})
extend ClassMethods
include InstanceMethods
cattr_accessor :encrypted_id_key
self.encrypted_id_key = Digest::SHA256.digest(options[:key] || encrypted_id_default_key)
+ self.define_singleton_method(:find_single, lambda { puts "foo" })
end
def self.decrypt(key, id)
c = OpenSSL::Cipher::Cipher.new(CIPHER_NAME).decrypt
c.iv = CIPHER_IV
@@ -27,18 +28,21 @@
(c.update("#{id}") + c.final).unpack('H*')[0]
end
module ClassMethods
def find(*args)
- if has_encrypted_id?
+ scope = args.slice!(0)
+ options = args.slice!(0) || {}
+ if !(scope.is_a? Symbol) && has_encrypted_id? && !options[:no_encrypted_id]
begin
- args[0] = EncryptedId.decrypt(encrypted_id_key, "#{args[0]}")
+ scope = EncryptedId.decrypt(encrypted_id_key, "#{scope}")
rescue OpenSSL::Cipher::CipherError
raise ActiveRecord::RecordNotFound.new("Could not decrypt ID #{args[0]}")
end
end
- super(*args)
+ options.delete(:no_encrypted_id)
+ super(scope, options)
end
def has_encrypted_id?
true
end
@@ -49,9 +53,14 @@
end
module InstanceMethods
def to_param
EncryptedId.encrypt(self.class.encrypted_id_key, self.id)
+ end
+
+ def reload(options = nil)
+ options = (options || {}).merge(:no_encrypted_id => true)
+ super(options)
end
end
end
ActiveRecord::Base.extend EncryptedId