Skip to content

Commit

Permalink
By default, the cookie key should be guessed from the session klass n…
Browse files Browse the repository at this point in the history
…ame instead of the klass name. So that we don't have to change cookie keys if we have multiple session models authenticating with the same model.
  • Loading branch information
slainer68 authored and binarylogic committed Nov 12, 2009
1 parent 233bbc3 commit 34240ff
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/authlogic/session/cookies.rb
Expand Up @@ -26,7 +26,7 @@ module Config
# * <tt>Default:</tt> "#{klass_name.underscore}_credentials"
# * <tt>Accepts:</tt> String
def cookie_key(value = nil)
rw_config(:cookie_key, value, "#{klass_name.underscore}_credentials")
rw_config(:cookie_key, value, "#{guessed_klass_name.underscore}_credentials")
end
alias_method :cookie_key=, :cookie_key

Expand Down
11 changes: 7 additions & 4 deletions lib/authlogic/session/klass.rb
Expand Up @@ -38,10 +38,13 @@ def klass

# Same as klass, just returns a string instead of the actual constant.
def klass_name
@klass_name ||=
if guessed_name = name.scan(/(.*)Session/)[0]
@klass_name = guessed_name[0]
end
@klass_name ||= guessed_klass_name
end

# The string of the model name class guessed from the actual session class name.
def guessed_klass_name
guessed_name = name.scan(/(.*)Session/)[0]
guessed_name[0] if guessed_name
end
end

Expand Down
6 changes: 5 additions & 1 deletion test/libs/user_session.rb
@@ -1,2 +1,6 @@
class UserSession < Authlogic::Session::Base
end
end

class BackOfficeUserSession < Authlogic::Session::Base
authenticate_with User
end
5 changes: 5 additions & 0 deletions test/session_test/cookies_test.rb
Expand Up @@ -11,6 +11,11 @@ def test_cookie_key
assert_equal "user_credentials", UserSession.cookie_key
end

def test_default_cookie_key
assert_equal "user_credentials", UserSession.cookie_key
assert_equal "back_office_user_credentials", BackOfficeUserSession.cookie_key
end

def test_remember_me
UserSession.remember_me = true
assert_equal true, UserSession.remember_me
Expand Down
5 changes: 5 additions & 0 deletions test/session_test/klass_test.rb
Expand Up @@ -20,6 +20,11 @@ def test_klass
def test_klass_name
assert_equal "User", UserSession.klass_name
end

def test_guessed_klass_name
assert_equal "User", UserSession.guessed_klass_name
assert_equal "BackOfficeUser", BackOfficeUserSession.guessed_klass_name
end
end

class InstanceMethodsTest < ActiveSupport::TestCase
Expand Down

0 comments on commit 34240ff

Please sign in to comment.