Class: Spree::PromotionCode::BatchBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/promotion_code/batch_builder.rb

Constant Summary

DEFAULT_OPTIONS =
{
  random_code_length: 6,
  batch_size: 1000,
  sample_characters: ('a'..'z').to_a + (2..9).to_a.map(&:to_s)
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(promotion_code_batch, options = {}) ⇒ BatchBuilder

Returns a new instance of BatchBuilder



27
28
29
30
31
# File 'app/models/spree/promotion_code/batch_builder.rb', line 27

def initialize(promotion_code_batch, options = {})
  @promotion_code_batch = promotion_code_batch
  options.assert_valid_keys(*DEFAULT_OPTIONS.keys)
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options



4
5
6
# File 'app/models/spree/promotion_code/batch_builder.rb', line 4

def options
  @options
end

#promotion_code_batchObject (readonly)

Returns the value of attribute promotion_code_batch



4
5
6
# File 'app/models/spree/promotion_code/batch_builder.rb', line 4

def promotion_code_batch
  @promotion_code_batch
end

Instance Method Details

#build_promotion_codesObject



33
34
35
36
37
38
39
40
41
42
# File 'app/models/spree/promotion_code/batch_builder.rb', line 33

def build_promotion_codes
  generate_random_codes
  promotion_code_batch.update!(state: "completed")
rescue StandardError => e
  promotion_code_batch.update!(
    error: e.inspect,
    state: "failed"
  )
  raise e
end