Skip to content

Commit

Permalink
add destroyed? & move to_key/persisted? into ARTrickery
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieg Zaharia committed May 2, 2013
1 parent 51416fe commit 36e38a0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
13 changes: 13 additions & 0 deletions lib/authlogic/session/active_record_trickery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,20 @@ module InstanceMethods
def new_record?
new_session?
end

def persisted?
!(new_record? || destroyed?)
end

# Don't use this yourself, this is to just trick some of the helpers since this is the method it calls.
def destroyed?
record.nil?
end

def to_key
new_record? ? nil : record.to_key
end

# For rails >= 3.0
def to_model
self
Expand Down
10 changes: 1 addition & 9 deletions lib/authlogic/session/foundation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,7 @@ def credentials=(values)
def inspect
"#<#{self.class.name}: #{credentials.blank? ? "no credentials provided" : credentials.inspect}>"
end

def persisted?
!(new_record? || destroyed?)
end

def to_key
new_record? ? nil : [ self.send(self.class.primary_key) ]
end


private
def build_key(last_part)
last_part
Expand Down
29 changes: 29 additions & 0 deletions test/session_test/active_record_trickery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,36 @@ def test_new_record
session = UserSession.new
assert session.new_record?
end

def test_to_key
ben = users(:ben)
session = UserSession.new(ben)
assert_nil session.to_key

session.save
assert_not_nil session.to_key
assert_equal ben.to_key, session.to_key
end

def test_persisted
session = UserSession.new(users(:ben))
assert ! session.persisted?

session.save
assert session.persisted?

session.destroy
assert ! session.persisted?
end

def test_destroyed?
session = UserSession.create(users(:ben))
assert ! session.destroyed?

session.destroy
assert session.destroyed?
end

def test_to_model
session = UserSession.new
assert_equal session, session.to_model
Expand Down

0 comments on commit 36e38a0

Please sign in to comment.