From 76f7e80455f1f5b5b052c7d8caf519713b016eea Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 23 Dec 2015 09:29:04 +0100 Subject: [PATCH] Fix method that ensures authentication token Until now, `ensure_#{token_filed_name}` method didn't persist new token in database. This closes #4235. --- app/models/concerns/token_authenticatable.rb | 8 ++------ spec/models/concerns/token_authenticatable_spec.rb | 4 ++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/concerns/token_authenticatable.rb b/app/models/concerns/token_authenticatable.rb index 488ff8c31b7..e84c5804cf4 100644 --- a/app/models/concerns/token_authenticatable.rb +++ b/app/models/concerns/token_authenticatable.rb @@ -17,12 +17,8 @@ module TokenAuthenticatable end define_method("ensure_#{token_field}") do - current_token = read_attribute(token_field) - if current_token.blank? - write_attribute(token_field, generate_token_for(token_field)) - else - current_token - end + send("reset_#{token_field}!") if read_attribute(token_field).blank? + read_attribute(token_field) end define_method("reset_#{token_field}!") do diff --git a/spec/models/concerns/token_authenticatable_spec.rb b/spec/models/concerns/token_authenticatable_spec.rb index a9b0b64e5de..07ec3a80e60 100644 --- a/spec/models/concerns/token_authenticatable_spec.rb +++ b/spec/models/concerns/token_authenticatable_spec.rb @@ -35,6 +35,10 @@ describe ApplicationSetting, 'TokenAuthenticatable' do it { is_expected.to be_a String } it { is_expected.to_not be_blank } + + it 'should persist new token' do + expect(subject).to eq described_class.current[token_field] + end end end