Class: Spree::NumberGenerators::Random

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/number_generators/random.rb

Overview

Pre-6.0 behaviour, kept as an opt-in: prefix + a fixed-width run of random digits. Merchants choose this when they would rather not tell a repeat customer how many orders the store took in between their two purchases, and accept numbers that are harder to read back over a phone.

Constant Summary collapse

DEFAULT_LENGTH =
9

Instance Method Summary collapse

Constructor Details

#initialize(length: DEFAULT_LENGTH) ⇒ Random

Returns a new instance of Random.

Parameters:

  • length (Integer) (defaults to: DEFAULT_LENGTH) —

    digits after the prefix



12
13
14
# File 'app/models/spree/number_generators/random.rb', line 12

def initialize(length: DEFAULT_LENGTH)
  @length = length
end

Instance Method Details

#generate(record) ⇒ Object



16
17
18
19
20
# File 'app/models/spree/number_generators/random.rb', line 16

def generate(record)
  digits = SecureRandom.random_number(10**@length).to_s.rjust(@length, '0')

  "#{prefix_for(record)}#{digits}#{suffix_for(record)}"
end