Skip to content
Browse files

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
  • Loading branch information...
1 parent f80cecc commit dcada8fe7568561e202ad66b8f2088a8b5c56ce5 @promisedlandt promisedlandt committed
View
4 lib/devise.rb
@@ -105,8 +105,8 @@ module Strategies
@@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
34 lib/devise/models/confirmable.rb
@@ -19,7 +19,7 @@ module Models
# 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 @@ def confirmation_period_valid?
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 @@ def find_by_unconfirmed_email_with_errors(attributes = {})
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
4 lib/generators/templates/devise.rb
@@ -95,10 +95,10 @@
# 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
4 test/integration/confirmable_test.rb
@@ -51,7 +51,7 @@ def resend_confirmation
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 @@ def resend_confirmation
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
4 test/models/confirmable_test.rb
@@ -249,13 +249,13 @@ def confirm_user_by_token_with_confirmation_sent_at(confirmation_sent_at)
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

0 comments on commit dcada8f

Please sign in to comment.
Something went wrong with that request. Please try again.