1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Refactoring confirmable and recoverable to use the same default find method by perishable token.

This commit is contained in:
Carlos A. da Silva 2009-10-07 22:59:55 -03:00
parent cf1ea9ab86
commit ba7e74e1a1
7 changed files with 21 additions and 26 deletions

View file

@ -4,8 +4,8 @@ module Devise
def self.included(base)
base.class_eval do
extend ClassMethods
include ::Devise::Perishable
extend ClassMethods
after_create :send_confirmation_instructions
end
@ -52,12 +52,8 @@ module Devise
# If the user is already confirmed, create an error for the user
#
def find_and_confirm(perishable_token)
confirmable = find_or_initialize_by_perishable_token(perishable_token)
unless confirmable.new_record?
confirmable.confirm!
else
confirmable.errors.add(:perishable_token, :invalid, :default => "invalid confirmation")
end
confirmable = find_or_initialize_with_error_by_perishable_token(perishable_token)
confirmable.confirm! unless confirmable.new_record?
confirmable
end
end

View file

@ -3,6 +3,8 @@ module Devise
def self.included(base)
base.class_eval do
extend ClassMethods
before_create :reset_perishable_token
end
end
@ -18,5 +20,19 @@ module Devise
def reset_perishable_token!
reset_perishable_token and save(false)
end
module ClassMethods
# Attempt to find a user by and incoming perishable_token. If no user is
# found, initialize a new one and adds an :invalid error to perishable_token
#
def find_or_initialize_with_error_by_perishable_token(perishable_token)
perishable = find_or_initialize_by_perishable_token(perishable_token)
if perishable.new_record?
perishable.errors.add(:perishable_token, :invalid, :default => "invalid confirmation")
end
perishable
end
end
end
end

View file

@ -53,12 +53,8 @@ module Devise
# Options must contain perishable_token, password and confirmation
#
def reset_password(options={})
recoverable = find_or_initialize_by_perishable_token(options[:perishable_token])
unless recoverable.new_record?
recoverable.reset_password!(options[:password], options[:password_confirmation])
else
recoverable.errors.add(:perishable_token, :invalid, :default => "invalid confirmation")
end
recoverable = find_or_initialize_with_error_by_perishable_token(options[:perishable_token])
recoverable.reset_password!(options[:password], options[:password_confirmation]) unless recoverable.new_record?
recoverable
end
end

View file

@ -3,8 +3,6 @@ require 'test_helper'
class ConfirmableTest < ActiveSupport::TestCase
def setup
# Todo: refactor this!
User.send :include, ::Devise::Confirmable unless User.included_modules.include?(::Devise::Confirmable)
setup_mailer
end
@ -91,4 +89,3 @@ class ConfirmableTest < ActiveSupport::TestCase
end
end
end

View file

@ -2,10 +2,6 @@ require 'test_helper'
class PerishableTest < ActiveSupport::TestCase
def setup
User.send :include, ::Devise::Perishable unless User.included_modules.include?(::Devise::Perishable)
end
test 'should not have perishable token accessible' do
assert_not field_accessible?(:perishable_token)
end

View file

@ -3,7 +3,6 @@ require 'test_helper'
class RecoverableTest < ActiveSupport::TestCase
def setup
User.send :include, ::Devise::Recoverable unless User.included_modules.include?(::Devise::Recoverable)
@user = create_user
setup_mailer
end

View file

@ -2,10 +2,6 @@ require 'test_helper'
class ValidatableTest < ActiveSupport::TestCase
def setup
User.send :include, ::Devise::Validatable unless User.included_modules.include?(::Devise::Validatable)
end
test 'should require email to be set' do
user = new_user(:email => nil)
assert user.invalid?
@ -101,4 +97,3 @@ class ValidatableTest < ActiveSupport::TestCase
assert_not user.errors[:password].to_a.include?('is too short (minimum is 6 characters)')
end
end