Class: Spree::PromotionCode::BatchBuilder

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

Constant Summary collapse

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.



13
14
15
16
17
# File 'app/models/spree/promotion_code/batch_builder.rb', line 13

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



19
20
21
22
23
24
25
26
27
28
# File 'app/models/spree/promotion_code/batch_builder.rb', line 19

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