Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Adds the possibility to configure the record's primary_key. #338

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+26 −6
Split
View
@@ -1,5 +1,5 @@
.DS_Store
-.swp
+*.swp
*.log
*.sqlite3
pkg/*
@@ -57,13 +57,25 @@ 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
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
@@ -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,
@@ -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
@@ -9,7 +9,7 @@ def authenticate_with_http_basic(&block)
def cookies
new_cookies = MockCookieJar.new
super.each do |key, value|
- new_cookies[key] = value[:value]
+ new_cookies[key] = { :value => value[:value] }
end
new_cookies
end
@@ -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