Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unify token creation in separate generator.
Add timestamp to token. Ensure uniqueness on database level. Conflicts: core/app/models/spree/order.rb
- Loading branch information
Showing
6 changed files
with
56 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Spree | ||
module Core | ||
module TokenGenerator | ||
def generate_guest_token(model_class = Spree::Order) | ||
loop do | ||
token = "#{random_token}#{unique_ending}" | ||
break token unless model_class.exists?(guest_token: token) | ||
end | ||
end | ||
|
||
private | ||
|
||
def random_token | ||
SecureRandom.urlsafe_base64(nil, false) | ||
end | ||
|
||
def unique_ending | ||
(Time.now.to_f * 1000).to_i | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'spec_helper' | ||
|
||
describe Spree::Core::TokenGenerator do | ||
class DummyClass | ||
include Spree::Core::TokenGenerator | ||
|
||
attr_reader :created_at | ||
|
||
def initialize | ||
@created_at = Time.now.to_i | ||
end | ||
end | ||
|
||
let(:dummy_class_instance) { DummyClass.new } | ||
|
||
describe 'generate_guest_token' do | ||
let(:generated_token) { dummy_class_instance.generate_guest_token } | ||
|
||
it 'generates random token with timestamp' do | ||
expect(generated_token.size).to eq 35 | ||
expect(generated_token).to include dummy_class_instance.created_at.to_s | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters