Permalink
Browse files

Quack, quack, quack. Use duck typing instead of hardcoding everything,

…closes #1281.
  • Loading branch information...
1 parent 673c707 commit 7396c6911d5daef5a202af854dbe208aa7d57bce @josevalim josevalim committed Aug 29, 2011
Showing with 17 additions and 10 deletions.
  1. +3 −0 CHANGELOG.rdoc
  2. +9 −0 lib/devise/models/authenticatable.rb
  3. +5 −10 lib/devise/rails/warden_compat.rb
View
@@ -1,3 +1,6 @@
+* enhancements
+ * Use serialize_into_session and serialize_from_session in Warden serialize to improve extensibility
+
* bug fix
* Properly deprecate setup_mail
* Fix encoding issues with email regexp
@@ -82,6 +82,15 @@ def authenticatable_salt
module ClassMethods
Devise::Models.config(self, :authentication_keys, :request_keys, :strip_whitespace_keys, :case_insensitive_keys, :http_authenticatable, :params_authenticatable)
+ def serialize_into_session(record)
+ [record.to_key, record.authenticatable_salt]
+ end
+
+ def serialize_from_session(key, salt)
+ record = to_adapter.get(key)
+ record if record && record.authenticatable_salt == salt
+ end
+
def params_authenticatable?(strategy)
params_authenticatable.is_a?(Array) ?
params_authenticatable.include?(strategy) : params_authenticatable
@@ -15,21 +15,16 @@ def cookies
class Warden::SessionSerializer
def serialize(record)
- [record.class.name, record.to_key, record.authenticatable_salt]
+ klass = record.class
+ array = klass.serialize_into_session(record)
+ array.unshift(klass.name)
end
def deserialize(keys)
- if keys.size == 2
- raise "Devise changed how it stores objects in session. If you are seeing this message, " <<
- "you can fix it by changing one character in your secret_token or cleaning up your " <<
- "database sessions if you are using a db store."
- end
-
- klass, id, salt = keys
+ klass, *args = keys
begin
- record = ActiveSupport::Inflector.constantize(klass).to_adapter.get(id)
- record if record && record.authenticatable_salt == salt
+ ActiveSupport::Inflector.constantize(klass).serialize_from_session(*args)
rescue NameError => e
if e.message =~ /uninitialized constant/
Rails.logger.debug "[Devise] Trying to deserialize invalid class #{klass}"

0 comments on commit 7396c69

Please sign in to comment.