Class: Spree::GiftCardBatch

Inherits:
Object
  • Object
show all
Extended by:
DisplayMoney
Includes:
SingleStoreResource
Defined in:
app/models/spree/gift_card_batch.rb

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Instance Method Details

#create_gift_cardsObject



41
42
43
44
45
46
47
48
49
50
# File 'app/models/spree/gift_card_batch.rb', line 41

def create_gift_cards
  @gift_cards_to_insert = []

  Spree::GiftCard.transaction do
    (codes_count - gift_cards.count).times do
      @gift_cards_to_insert << gift_card_hash
    end
    Spree::GiftCard.insert_all @gift_cards_to_insert if @gift_cards_to_insert.any?
  end
end

#generate_codeObject



52
53
54
55
56
57
# File 'app/models/spree/gift_card_batch.rb', line 52

def generate_code
  loop do
    code = "#{prefix.downcase}#{SecureRandom.hex(3).downcase}"
    break code unless Spree::GiftCard.exists?(code: code) || @gift_cards_to_insert.detect { |gc| gc[:code] == code }
  end
end

#generate_gift_cardsObject



33
34
35
36
37
38
39
# File 'app/models/spree/gift_card_batch.rb', line 33

def generate_gift_cards
  if codes_count < Spree::Config[:gift_card_batch_web_limit].to_i
    create_gift_cards
  else
    Spree::GiftCards::BulkGenerateJob.perform_later(id)
  end
end