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

Add ignore_period option to .confirm #5677

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
12 changes: 8 additions & 4 deletions lib/devise/models/confirmable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,16 @@ def self.required_fields(klass)
required_methods
end

# Confirm a user by setting it's confirmed_at to actual time. If the user
# is already confirmed, add an error to email field. If the user is invalid
# add errors
# Confirm a user by setting its confirmed_at to the current time. If the user
# is already confirmed, add an error to email field.
#
# Supported options:
# * `:ensure_valid` - When true, validations are run when saving the record (otherwise, validations are only run
# on reconfirmation, to ensure e-mail uniqueness).
# * `:ignore_period` - When true, skips checking whether the confirmation period is expired.
def confirm(args = {})
pending_any_confirmation do
if confirmation_period_expired?
if !args[:ignore_period] && confirmation_period_expired?
self.errors.add(:email, :confirmation_period_expired,
period: Devise::TimeInflector.time_ago_in_words(self.class.confirm_within.ago))
return false
Expand Down
12 changes: 12 additions & 0 deletions test/models/confirmable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,18 @@ def confirm_user_by_token_with_confirmation_sent_at(confirmation_sent_at)
admin.stubs(:valid?).returns(false)
assert_not admin.confirm(ensure_valid: true)
end

test 'should ignore the confirmation period when ignore_period is true' do
swap Devise, confirm_within: 3.days do
user = Timecop.freeze(4.days.ago) do
create_user
end
assert_not user.confirm
assert_not user.confirmed?
assert user.confirm(ignore_period: true)
assert user.confirmed?
end
end
end

class ReconfirmableTest < ActiveSupport::TestCase
Expand Down