Moving perishable inclusion to active_record devise, to include just once. Also refactor confirm! method.
This commit is contained in:
parent
7f91651d0c
commit
417f273a81
|
@ -25,6 +25,7 @@ module Devise
|
|||
def devise(*options)
|
||||
options = [:confirmable, :recoverable, :validatable] if options.include?(:all)
|
||||
options |= [:authenticable]
|
||||
options |= [:perishable] if options.include?(:confirmable) || options.include?(:recoverable)
|
||||
|
||||
options.each do |m|
|
||||
devise_modules << m.to_sym
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
require 'devise/models/perishable'
|
||||
|
||||
module Devise
|
||||
module Models
|
||||
|
||||
|
@ -25,12 +23,13 @@ module Devise
|
|||
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
include ::Devise::Models::Perishable
|
||||
extend ClassMethods
|
||||
|
||||
after_create :send_confirmation_instructions
|
||||
before_update :reset_confirmation, :if => :email_changed?
|
||||
after_update :send_confirmation_instructions, :if => :email_changed?
|
||||
|
||||
before_create :reset_perishable_token
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -38,11 +37,8 @@ module Devise
|
|||
# is already confirmed, add en error to email field
|
||||
#
|
||||
def confirm!
|
||||
unless confirmed?
|
||||
unless_confirmed do
|
||||
update_attribute(:confirmed_at, Time.now)
|
||||
else
|
||||
errors.add(:email, :already_confirmed, :default => 'already confirmed')
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -77,6 +73,18 @@ module Devise
|
|||
self.confirmed_at = nil
|
||||
end
|
||||
|
||||
# Checks whether the record is confirmed or not, yielding to the block if
|
||||
# it's already confirmed, otherwise adds an error to email.
|
||||
#
|
||||
def unless_confirmed
|
||||
unless confirmed?
|
||||
yield
|
||||
else
|
||||
errors.add(:email, :already_confirmed, :default => 'already confirmed')
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Attempt to find a user by it's email. If a record is found, send new
|
||||
# confirmation instructions to it. If not user is found, returns a new user
|
||||
|
|
|
@ -21,7 +21,6 @@ module Devise
|
|||
def self.included(base)
|
||||
base.class_eval do
|
||||
extend ClassMethods
|
||||
before_create :reset_perishable_token
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
require 'devise/models/perishable'
|
||||
|
||||
module Devise
|
||||
module Models
|
||||
|
||||
|
@ -17,7 +15,6 @@ module Devise
|
|||
module Recoverable
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
include ::Devise::Models::Perishable
|
||||
extend ClassMethods
|
||||
end
|
||||
end
|
||||
|
|
|
@ -52,7 +52,7 @@ class PerishableTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
test 'should generate a sha1 hash for perishable token' do
|
||||
ActiveSupport::SecureRandom.expects(:base64).with(15).times(3).returns('perishable token')
|
||||
ActiveSupport::SecureRandom.expects(:base64).with(15).twice.returns('perishable token')
|
||||
assert_equal 'perishable token', create_user.perishable_token
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue