-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1820 from tvdeyen/order-number-generator
Add a configurable order number generator
- Loading branch information
Showing
5 changed files
with
141 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module Spree | ||
# Generates order numbers | ||
# | ||
# In order to change the way your order numbers get generated you can either | ||
# set your own instance of this class in your stores configuration with different options: | ||
# | ||
# Spree::Config.order_number_generator = Spree::Order::NumberGenerator.new( | ||
# prefix: 'B', | ||
# lenght: 8, | ||
# letters: false | ||
# ) | ||
# | ||
# or create your own class: | ||
# | ||
# Spree::Config.order_number_generator = My::OrderNumberGenerator.new | ||
# | ||
class Order::NumberGenerator | ||
attr_reader :letters, :prefix | ||
|
||
def initialize(options = {}) | ||
@length = options[:length] || Spree::Order::ORDER_NUMBER_LENGTH | ||
@letters = options[:letters] || Spree::Order::ORDER_NUMBER_LETTERS | ||
@prefix = options[:prefix] || Spree::Order::ORDER_NUMBER_PREFIX | ||
end | ||
|
||
def generate | ||
possible = (0..9).to_a | ||
possible += ('A'..'Z').to_a if letters | ||
|
||
loop do | ||
# Make a random number. | ||
random = "#{prefix}#{(0...@length).map { possible.sample }.join}" | ||
# Use the random number if no other order exists with it. | ||
if Spree::Order.exists?(number: random) | ||
# If over half of all possible options are taken add another digit. | ||
@length += 1 if Spree::Order.count > (10**@length / 2) | ||
else | ||
break random | ||
end | ||
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,45 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Spree::Order::NumberGenerator do | ||
subject { described_class.new.generate } | ||
|
||
it { is_expected.to be_a(String) } | ||
|
||
describe 'length' do | ||
let(:default_length) do | ||
Spree::Order::ORDER_NUMBER_LENGTH + Spree::Order::ORDER_NUMBER_PREFIX.length | ||
end | ||
|
||
it { expect(subject.length).to eq default_length } | ||
|
||
context "when length option is 5" do | ||
let(:option_length) { 5 + Spree::Order::ORDER_NUMBER_PREFIX.length } | ||
|
||
subject { described_class.new(length: 5).generate } | ||
|
||
it "should be 5 plus default prefix length" do | ||
expect(subject.length).to eq option_length | ||
end | ||
end | ||
end | ||
|
||
context "when letters option is true" do | ||
subject { described_class.new(letters: true).generate } | ||
|
||
it "generates order number including letters" do | ||
is_expected.to match /[A-Z]/ | ||
end | ||
end | ||
|
||
describe 'prefix' do | ||
it { is_expected.to match /^#{Spree::Order::ORDER_NUMBER_PREFIX}/ } | ||
|
||
context "when prefix option is 'P'" do | ||
subject { described_class.new(prefix: 'P').generate } | ||
|
||
it "generates order number prefixed with 'P'" do | ||
is_expected.to match /^P/ | ||
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