implement dynamic value for timeout_in as a model method instead of a proc

This commit is contained in:
lest 2011-11-25 10:57:36 +03:00
parent c8c471a128
commit 37dad2172b
3 changed files with 13 additions and 18 deletions

View File

@ -2,7 +2,7 @@
* enhancements
* 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)
* Add timeout_in method to Timeoutable, it can be overriden in a model (by github.com/lest)
* bug fix
* OmniAuth error message now shows the proper option (:strategy_class instead of :klass)

View File

@ -24,12 +24,13 @@ module Devise
def timedout?(last_access)
return false if remember_exists_and_not_expired?
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
def timeout_in
self.class.timeout_in
end
private
def remember_exists_and_not_expired?

View File

@ -14,24 +14,18 @@ class TimeoutableTest < ActiveSupport::TestCase
assert_not new_user.timedout?(nil)
end
test 'should accept timeout_in proc and provide user as argument' do
test 'should use timeout_in method' do
user = new_user
user.instance_eval { def timeout_in; 10.minutes end }
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
assert user.timedout?(12.minutes.ago)
assert_not user.timedout?(8.minutes.ago)
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
test 'should not be expired when timeout_in method returns nil' do
user = new_user
user.instance_eval { def timeout_in; nil end }
assert_not user.timedout?(10.hours.ago)
end
test 'fallback to Devise config option' do