Refactor according to line notes from josevalim

- rename reset_password_within to confirm_within
- confirmation_period_valid? is back and memoized
- fix hash syntax to hashrocket
This commit is contained in:
Nils Landt 2012-07-22 14:02:27 +02:00
parent f80cecc864
commit dcada8fe75
5 changed files with 34 additions and 16 deletions

View File

@ -105,8 +105,8 @@ module Devise
@@allow_unconfirmed_access_for = 0.days
# Time interval the confirmation token is valid. nil = unlimited
mattr_accessor :expire_confirmation_token_after
@@expire_confirmation_token_after = nil
mattr_accessor :confirm_within
@@confirm_within = nil
# Defines which key will be used when confirming an account.
mattr_accessor :confirmation_keys

View File

@ -19,7 +19,7 @@ module Devise
# db field to be setup (t.reconfirmable in migrations). Until confirmed new email is
# stored in unconfirmed email column, and copied to email column on successful
# confirmation.
# * +expire_confirmation_token_after+: the time before a sent confirmation token becomes invalid.
# * +confirm_within+: the time before a sent confirmation token becomes invalid.
# You can use this to force the user to confirm within a set period of time.
#
# == Examples
@ -158,19 +158,37 @@ module Devise
confirmation_sent_at && confirmation_sent_at.utc >= self.class.allow_unconfirmed_access_for.ago
end
# Checks if the user confirmation happens before the token becomes invalid
# Examples:
#
# # confirm_within = 3.days and confirmation_sent_at = 2.days.ago
# confirmation_period_expired? # returns false
#
# # confirm_within = 3.days and confirmation_sent_at = 4.days.ago
# confirmation_period_expired? # returns true
#
# # confirm_within = nil
# confirmation_period_expired? # will always return false
#
def confirmation_period_expired?
if @confirmation_period_expired.nil?
@confirmation_period_expired = self.class.confirm_within && (Time.now > self.confirmation_sent_at + self.class.confirm_within )
@confirmation_period_expired
else
@confirmation_period_expired
end
end
# Checks whether the record requires any confirmation.
def pending_any_confirmation
@confirmation_period_expired = if @confirmation_period_expired.nil?
self.class.expire_confirmation_token_after && (Time.now > self.confirmation_sent_at + self.class.expire_confirmation_token_after )
else
@confirmation_period_expired
end
@confirmation_period_expired = confirmation_period_expired?
if (!confirmed? || pending_reconfirmation?) && !@confirmation_period_expired
yield
else
if @confirmation_period_expired
self.errors.add(:email, :confirmation_period_expired, period: time_ago_in_words(self.class.expire_confirmation_token_after.ago))
self.errors.add(:email, :confirmation_period_expired, :period => time_ago_in_words(self.class.confirm_within.ago))
else
self.errors.add(:email, :already_confirmed)
end
@ -247,7 +265,7 @@ module Devise
find_or_initialize_with_errors(unconfirmed_required_attributes, unconfirmed_attributes, :not_found)
end
Devise::Models.config(self, :allow_unconfirmed_access_for, :confirmation_keys, :reconfirmable, :expire_confirmation_token_after)
Devise::Models.config(self, :allow_unconfirmed_access_for, :confirmation_keys, :reconfirmable, :confirm_within)
end
end
end

View File

@ -95,10 +95,10 @@ Devise.setup do |config|
# A period that the user is allowed to confirm their account before their token
# becomes invalid. For example, if set to 3.days, the user can confirm their account
# within 3 days after the mail was sent, but on the fourth day their account can't be
# confirmed with the token any more
# confirmed with the token any more.
# Default is nil, meaning there is no restriction on how long a user can take before
# confirming their account.
# config.expire_confirmation_token_after = 3.days
# config.confirm_within = 3.days
# If true, requires any email changes to be confirmed (exactly the same way as
# initial account confirmation) to be applied. Requires additional unconfirmed_email

View File

@ -51,7 +51,7 @@ class ConfirmationTest < ActionController::IntegrationTest
end
test 'user with valid confirmation token should not be able to confirm an account after the token has expired' do
swap Devise, :expire_confirmation_token_after => 3.days do
swap Devise, :confirm_within => 3.days do
user = create_user(:confirm => false, :confirmation_sent_at => 4.days.ago)
assert_not user.confirmed?
visit_user_confirmation_with_token(user.confirmation_token)
@ -63,7 +63,7 @@ class ConfirmationTest < ActionController::IntegrationTest
end
test 'user with valid confirmation token should be able to confirm an account before the token has expired' do
swap Devise, :expire_confirmation_token_after => 3.days do
swap Devise, :confirm_within => 3.days do
user = create_user(:confirm => false, :confirmation_sent_at => 2.days.ago)
assert_not user.confirmed?
visit_user_confirmation_with_token(user.confirmation_token)

View File

@ -249,13 +249,13 @@ class ConfirmableTest < ActiveSupport::TestCase
end
test 'should accept confirmation email token after 2 days when expiration is set to 3 days' do
swap Devise, :expire_confirmation_token_after => 3.days do
swap Devise, :confirm_within => 3.days do
assert confirm_user_by_token_with_confirmation_sent_at(2.days.ago)
end
end
test 'should not accept confirmation email token after 4 days when expiration is set to 3 days' do
swap Devise, :expire_confirmation_token_after => 3.days do
swap Devise, :confirm_within => 3.days do
assert_not confirm_user_by_token_with_confirmation_sent_at(4.days.ago)
end
end