Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeoutable timeout_in config should accept a proc #5524

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/devise/models/timeoutable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def timedout?(last_access)
end

def timeout_in
self.class.timeout_in
config_value = self.class.timeout_in
config_value.is_a?(Proc) ? config_value.call(self) : config_value
end

private
Expand Down
1 change: 1 addition & 0 deletions lib/generators/templates/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
# ==> Configuration for :timeoutable
# The time you want to timeout the user session without activity. After this
# time the user will be asked for credentials again. Default is 30 minutes.
# Accepts a Duration or a proc, e.g. ->(user) { user.admin? ? nil : 30.minutes }
# config.timeout_in = 30.minutes

# ==> Configuration for :lockable
Expand Down
8 changes: 8 additions & 0 deletions test/models/timeoutable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ class TimeoutableTest < ActiveSupport::TestCase
end
end

test 'Devise config option should accept a proc' do
user = new_user
user.instance_eval { def custom_timeout_in; 42.minutes end }
swap Devise, timeout_in: ->(user) { user.custom_timeout_in } do
assert_equal user.timeout_in, 42.minutes
end
end

test 'required_fields should contain the fields that Devise uses' do
assert_equal [], Devise::Models::Timeoutable.required_fields(User)
end
Expand Down
1 change: 1 addition & 0 deletions test/rails_app/config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
# ==> Configuration for :timeoutable
# The time you want to timeout the user session without activity. After this
# time the user will be asked for credentials again. Default is 30 minutes.
# Accepts a Duration or a proc, e.g. ->(user) { user.admin? ? nil : 30.minutes }
# config.timeout_in = 30.minutes

# ==> Configuration for :lockable
Expand Down