Skip to content

Commit

Permalink
Merge pull request binarylogic#292 from iamvery/fix-authentic-scopes
Browse files Browse the repository at this point in the history
Lambda wrapped authentic #logged_in & #logged_out scopes
  • Loading branch information
binarylogic committed Jan 6, 2012
2 parents ec06ca7 + b3b5997 commit d52ea3b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/authlogic/acts_as_authentic/logged_in_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def self.included(klass)

klass.class_eval do
include InstanceMethods
scope :logged_in, where("last_request_at > ?", logged_in_timeout.seconds.ago)
scope :logged_out, where("last_request_at is NULL or last_request_at <= ?", logged_in_timeout.seconds.ago)
scope :logged_in, lambda{ where("last_request_at > ?", logged_in_timeout.seconds.ago) }
scope :logged_out, lambda{ where("last_request_at is NULL or last_request_at <= ?", logged_in_timeout.seconds.ago) }
end
end

Expand Down
14 changes: 14 additions & 0 deletions test/acts_as_authentic_test/logged_in_status_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module ActsAsAuthenticTest
class LoggedInStatusTest < ActiveSupport::TestCase
ERROR_MSG = 'Multiple calls to %s should result in different relations'

def test_logged_in_timeout_config
assert_equal 10.minutes.to_i, User.logged_in_timeout
assert_equal 10.minutes.to_i, Employee.logged_in_timeout
Expand All @@ -13,12 +15,24 @@ def test_logged_in_timeout_config
end

def test_named_scope_logged_in
# Testing that the scope returned differs, because the time it was called should be
# slightly different. This is an attempt to make sure the scope is lambda wrapped
# so that it is re-evaluated every time its called. My biggest concern is that the
# test happens so fast that the test fails... I just don't know a better way to test it!
assert User.logged_in.where_values != User.logged_in.where_values, ERROR_MSG % '#logged_in'

assert_equal 0, User.logged_in.count
User.first.update_attribute(:last_request_at, Time.now)
assert_equal 1, User.logged_in.count
end

def test_named_scope_logged_out
# Testing that the scope returned differs, because the time it was called should be
# slightly different. This is an attempt to make sure the scope is lambda wrapped
# so that it is re-evaluated every time its called. My biggest concern is that the
# test happens so fast that the test fails... I just don't know a better way to test it!
assert User.logged_in.where_values != User.logged_out.where_values, ERROR_MSG % '#logged_out'

assert_equal 2, User.logged_out.count
User.first.update_attribute(:last_request_at, Time.now)
assert_equal 1, User.logged_out.count
Expand Down

0 comments on commit d52ea3b

Please sign in to comment.