From 697ffff07289237d4f8dd7acef3c082dc15d5fb4 Mon Sep 17 00:00:00 2001 From: Jeremy Wadsack Date: Wed, 11 Oct 2017 14:28:51 -0700 Subject: [PATCH] Model should set authentication token value before validation rather than before save --- .../acts_as_token_authenticatable.rb | 2 +- .../acts_as_token_authenticatable_spec.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/simple_token_authentication/acts_as_token_authenticatable.rb b/lib/simple_token_authentication/acts_as_token_authenticatable.rb index 728cbeda..6f252125 100644 --- a/lib/simple_token_authentication/acts_as_token_authenticatable.rb +++ b/lib/simple_token_authentication/acts_as_token_authenticatable.rb @@ -41,7 +41,7 @@ def token_generator module ClassMethods def acts_as_token_authenticatable(options = {}) - before_save :ensure_authentication_token + before_validation :ensure_authentication_token end end end diff --git a/spec/lib/simple_token_authentication/acts_as_token_authenticatable_spec.rb b/spec/lib/simple_token_authentication/acts_as_token_authenticatable_spec.rb index 3579f49b..dee12572 100644 --- a/spec/lib/simple_token_authentication/acts_as_token_authenticatable_spec.rb +++ b/spec/lib/simple_token_authentication/acts_as_token_authenticatable_spec.rb @@ -33,20 +33,20 @@ def generate_token end end - describe 'which supports the :before_save hook' do + describe 'which supports the :before_validation hook' do context 'when it acts as token authenticatable' do it 'ensures its instances have an authentication token before being saved (1)', rspec_3_error: true, public: true do some_class = @subjects.first - expect(some_class).to receive(:before_save).with(:ensure_authentication_token) + expect(some_class).to receive(:before_validation).with(:ensure_authentication_token) some_class.acts_as_token_authenticatable end it 'ensures its instances have an authentication token before being saved (2)', rspec_3_error: true, public: true do some_child_class = @subjects.last - expect(some_child_class).to receive(:before_save).with(:ensure_authentication_token) + expect(some_child_class).to receive(:before_validation).with(:ensure_authentication_token) some_child_class.acts_as_token_authenticatable end end