1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

timeout_in option can be a Proc object

This commit is contained in:
lest 2011-11-24 21:42:58 +03:00
parent 5909d6a0c5
commit 426223dda0
3 changed files with 30 additions and 6 deletions

View file

@ -2,6 +2,7 @@
* enhancements * enhancements
* Add support for rails 3.1 new mass assignment conventions (by github.com/kirs) * Add support for rails 3.1 new mass assignment conventions (by github.com/kirs)
* timeout_in option can be a Proc object (by github.com/lest)
* bug fix * bug fix
* OmniAuth error message now shows the proper option (:strategy_class instead of :klass) * OmniAuth error message now shows the proper option (:strategy_class instead of :klass)

View file

@ -24,7 +24,10 @@ module Devise
def timedout?(last_access) def timedout?(last_access)
return false if remember_exists_and_not_expired? return false if remember_exists_and_not_expired?
last_access && last_access <= self.class.timeout_in.ago timeout_in = self.class.timeout_in
timeout_in = timeout_in.call(self) if timeout_in.respond_to?(:call)
!timeout_in.nil? && last_access && last_access <= timeout_in.ago
end end
private private

View file

@ -14,6 +14,26 @@ class TimeoutableTest < ActiveSupport::TestCase
assert_not new_user.timedout?(nil) assert_not new_user.timedout?(nil)
end end
test 'should accept timeout_in proc and provide user as argument' do
user = new_user
timeout_in = proc do |obj|
assert_equal user, obj
10.minutes
end
swap Devise, :timeout_in => timeout_in do
assert user.timedout?(12.minutes.ago)
assert_not user.timedout?(8.minutes.ago)
end
end
test 'should not be expired when timeout_in proc returns nil' do
swap Devise, :timeout_in => proc { nil } do
assert_not new_user.timedout?(10.hours.ago)
end
end
test 'fallback to Devise config option' do test 'fallback to Devise config option' do
swap Devise, :timeout_in => 1.minute do swap Devise, :timeout_in => 1.minute do
user = new_user user = new_user