Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds the possibility to configure the record's primary_key.

  • Loading branch information...
commit 86e992c8e1524d38b142e1bcd34d09d57da5b776 1 parent 7aed570
@julianalucena authored
View
2  .gitignore
@@ -1,5 +1,5 @@
.DS_Store
-.swp
+*.swp
*.log
*.sqlite3
pkg/*
View
12 lib/authlogic/acts_as_authentic/session_maintenance.rb
@@ -57,6 +57,17 @@ def session_class(value = nil)
rw_config(:session_class, value, const)
end
alias_method :session_class=, :session_class
+
+ # This method allows you to change the record's primary_key in the
+ # context of Authlogic.
+ #
+ # * <tt>Default:</tt> :id
+ # * <tt>Accepts:</tt> Symbol
+ def authlogic_record_primary_key(value = nil)
+ rw_config(:authlogic_record_primary_key, value,
+ base_class.primary_key.to_sym)
+ end
+ alias_method :authlogic_record_primary_key=, :authlogic_record_primary_key
end
module Methods
@@ -64,6 +75,7 @@ def self.included(klass)
klass.class_eval do
before_save :get_session_information, :if => :update_sessions?
before_save :maintain_sessions, :if => :update_sessions?
+ authlogic_record_primary_key
end
end
View
4 lib/authlogic/session/cookies.rb
@@ -155,7 +155,7 @@ def cookie_credentials
def persist_by_cookie
persistence_token, record_id = cookie_credentials
if !persistence_token.nil?
- record = record_id.nil? ? search_for_record("find_by_persistence_token", persistence_token) : search_for_record("find_by_#{klass.primary_key}", record_id)
+ record = record_id.nil? ? search_for_record("find_by_persistence_token", persistence_token) : search_for_record("find_by_#{klass.authlogic_record_primary_key}", record_id)
self.unauthorized_record = record if record && record.persistence_token == persistence_token
valid?
else
@@ -165,7 +165,7 @@ def persist_by_cookie
def save_cookie
controller.cookies[cookie_key] = {
- :value => "#{record.persistence_token}::#{record.send(record.class.primary_key)}",
+ :value => "#{record.persistence_token}::#{record.send(record.class.authlogic_record_primary_key)}",
:expires => remember_me_until,
:secure => secure,
:httponly => httponly,
View
4 lib/authlogic/session/session.rb
@@ -36,7 +36,7 @@ def persist_by_session
# This is done for performance reasons and to save on queries.
record = record_id.nil? ?
search_for_record("find_by_persistence_token", persistence_token) :
- search_for_record("find_by_#{klass.primary_key}", record_id)
+ search_for_record("find_by_#{klass.authlogic_record_primary_key}", record_id)
self.unauthorized_record = record if record && record.persistence_token == persistence_token
valid?
else
@@ -45,7 +45,7 @@ def persist_by_session
end
def session_credentials
- [controller.session[session_key], controller.session["#{session_key}_#{klass.primary_key}"]].compact
+ [controller.session[session_key], controller.session["#{session_key}_#{klass.authlogic_record_primary_key}"]].compact
end
def session_key
View
8 test/acts_as_authentic_test/session_maintenance_test.rb
@@ -10,6 +10,14 @@ def test_maintain_sessions_config
assert User.maintain_sessions
end
+ def test_authlogic_record_primary_key_config
+ assert_equal User.authlogic_record_primary_key, :id
+ User.authlogic_record_primary_key = :login
+ assert_equal User.authlogic_record_primary_key, :login
+ User.authlogic_record_primary_key = :id
+ assert_equal User.authlogic_record_primary_key, :id
+ end
+
def test_login_after_create
assert User.create(:login => "awesome", :password => "saweet", :password_confirmation => "saweet", :email => "awesome@awesome.com")
assert UserSession.find
Please sign in to comment.
Something went wrong with that request. Please try again.