Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor klass_name

  • Loading branch information...
commit 8b383b57f2ea30aae27ae73c3651084def12c4e5 1 parent 380761d
@binarylogic authored
View
1  Gemfile
@@ -5,6 +5,7 @@ gem 'activerecord', '>= 3.0.7'
group :test do
gem 'bcrypt-ruby'
gem 'jeweler'
+ gem 'rake'
gem 'ruby-debug19'
gem 'sqlite3'
end
View
31 Gemfile.lock
@@ -1,30 +1,30 @@
GEM
remote: http://rubygems.org/
specs:
- activemodel (3.0.7)
- activesupport (= 3.0.7)
+ activemodel (3.0.10)
+ activesupport (= 3.0.10)
builder (~> 2.1.2)
i18n (~> 0.5.0)
- activerecord (3.0.7)
- activemodel (= 3.0.7)
- activesupport (= 3.0.7)
- arel (~> 2.0.2)
+ activerecord (3.0.10)
+ activemodel (= 3.0.10)
+ activesupport (= 3.0.10)
+ arel (~> 2.0.10)
tzinfo (~> 0.3.23)
- activesupport (3.0.7)
+ activesupport (3.0.10)
archive-tar-minitar (0.5.2)
- arel (2.0.9)
- bcrypt-ruby (2.1.4)
+ arel (2.0.10)
+ bcrypt-ruby (3.0.0)
builder (2.1.2)
- columnize (0.3.2)
+ columnize (0.3.4)
git (1.2.5)
i18n (0.5.0)
- jeweler (1.5.2)
- bundler (~> 1.0.0)
+ jeweler (1.6.4)
+ bundler (~> 1.0)
git (>= 1.2.5)
rake
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
- rake (0.8.7)
+ rake (0.9.2)
ruby-debug-base19 (0.11.25)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
@@ -35,8 +35,8 @@ GEM
ruby-debug-base19 (>= 0.11.19)
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
- sqlite3 (1.3.3)
- tzinfo (0.3.27)
+ sqlite3 (1.3.4)
+ tzinfo (0.3.29)
PLATFORMS
ruby
@@ -45,5 +45,6 @@ DEPENDENCIES
activerecord (>= 3.0.7)
bcrypt-ruby
jeweler
+ rake
ruby-debug19
sqlite3
View
31 lib/authlogic/session/klass.rb
@@ -6,13 +6,13 @@ def self.included(klass)
klass.class_eval do
extend Config
include InstanceMethods
-
+
class << self
attr_accessor :configured_klass_methods
end
end
end
-
+
module Config
# Lets you change which model to use for authentication.
#
@@ -23,31 +23,22 @@ def authenticate_with(klass)
@klass = klass
end
alias_method :authenticate_with=, :authenticate_with
-
+
# The name of the class that this session is authenticating with. For example, the UserSession class will
# authenticate with the User class unless you specify otherwise in your configuration. See authenticate_with
# for information on how to change this value.
def klass
- @klass ||=
- if klass_name
- klass_name.constantize
- else
- nil
- end
- end
-
- # Same as klass, just returns a string instead of the actual constant.
- def klass_name
- @klass_name ||= guessed_klass_name
+ @klass ||= klass_name ? klass_name.constantize : nil
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
+ def klass_name
+ return @klass_name if defined?(@klass_name)
+ @klass_name = name.scan(/(.*)Session/)[0]
+ @klass_name = klass_name ? klass_name[0] : nil
end
end
-
+
module InstanceMethods
# Creating an alias method for the "record" method based on the klass name, so that we can do:
#
@@ -63,7 +54,7 @@ def initialize(*args)
end
super
end
-
+
private
def klass
self.class.klass
View
1  test/libs/user_session.rb
@@ -2,5 +2,4 @@ class UserSession < Authlogic::Session::Base
end
class BackOfficeUserSession < Authlogic::Session::Base
- authenticate_with User
end
View
36 test/session_test/cookies_test.rb
@@ -6,35 +6,35 @@ class ConfiTest < ActiveSupport::TestCase
def test_cookie_key
UserSession.cookie_key = "my_cookie_key"
assert_equal "my_cookie_key", UserSession.cookie_key
-
+
UserSession.cookie_key "user_credentials"
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
session = UserSession.new
assert_equal true, session.remember_me
-
+
UserSession.remember_me false
assert_equal false, UserSession.remember_me
session = UserSession.new
assert_equal false, session.remember_me
end
-
+
def test_remember_me_for
UserSession.remember_me_for = 3.years
assert_equal 3.years, UserSession.remember_me_for
session = UserSession.new
session.remember_me = true
assert_equal 3.years, session.remember_me_for
-
+
UserSession.remember_me_for 3.months
assert_equal 3.months, UserSession.remember_me_for
session = UserSession.new
@@ -66,48 +66,48 @@ def test_httponly
assert_equal false, session.httponly
end
end
-
+
class InstanceMethodsTest < ActiveSupport::TestCase
def test_credentials
session = UserSession.new
session.credentials = {:remember_me => true}
assert_equal true, session.remember_me
end
-
+
def test_remember_me
session = UserSession.new
assert_equal false, session.remember_me
assert !session.remember_me?
-
+
session.remember_me = false
assert_equal false, session.remember_me
assert !session.remember_me?
-
+
session.remember_me = true
assert_equal true, session.remember_me
assert session.remember_me?
-
+
session.remember_me = nil
assert_nil session.remember_me
assert !session.remember_me?
-
+
session.remember_me = "1"
assert_equal "1", session.remember_me
assert session.remember_me?
-
+
session.remember_me = "true"
assert_equal "true", session.remember_me
assert session.remember_me?
end
-
+
def test_remember_me_until
session = UserSession.new
assert_nil session.remember_me_until
-
+
session.remember_me = true
assert 3.months.from_now <= session.remember_me_until
end
-
+
def test_persist_persist_by_cookie
ben = users(:ben)
assert !UserSession.find
@@ -115,14 +115,14 @@ def test_persist_persist_by_cookie
assert session = UserSession.find
assert_equal ben, session.record
end
-
+
def test_after_save_save_cookie
ben = users(:ben)
session = UserSession.new(ben)
assert session.save
assert_equal "#{ben.persistence_token}::#{ben.id}", controller.cookies["user_credentials"]
end
-
+
def test_after_destroy_destroy_cookie
ben = users(:ben)
set_cookie_for(ben)
View
14 test/session_test/klass_test.rb
@@ -7,12 +7,12 @@ def test_authenticate_with
UserSession.authenticate_with = Employee
assert_equal "Employee", UserSession.klass_name
assert_equal Employee, UserSession.klass
-
+
UserSession.authenticate_with User
assert_equal "User", UserSession.klass_name
assert_equal User, UserSession.klass
end
-
+
def test_klass
assert_equal User, UserSession.klass
end
@@ -20,13 +20,13 @@ 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
+
+ def test_klass_name_uses_custom_name
+ assert_equal "User", UserSession.klass_name
+ assert_equal "BackOfficeUser", BackOfficeUserSession.klass_name
end
end
-
+
class InstanceMethodsTest < ActiveSupport::TestCase
def test_record_method
ben = users(:ben)
Please sign in to comment.
Something went wrong with that request. Please try again.