Permalink
Browse files

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

… proc
  • Loading branch information...
1 parent c8c471a commit 37dad2172b9d1f3a35132a384103a28fd41acfca @lest lest committed Nov 25, 2011
Showing with 13 additions and 18 deletions.
  1. +1 −1 CHANGELOG.rdoc
  2. +4 −3 lib/devise/models/timeoutable.rb
  3. +8 −14 test/models/timeoutable_test.rb
View
@@ -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)
@@ -24,12 +24,13 @@ module Timeoutable
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?
@@ -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

0 comments on commit 37dad21

Please sign in to comment.