Class: Spree::GiftCardBatch
- Inherits:
-
Object
- Object
- Spree::GiftCardBatch
- Extended by:
- DisplayMoney
- Includes:
- SingleStoreResource
- Defined in:
- app/models/spree/gift_card_batch.rb
Instance Method Summary collapse
- #amount=(amount) ⇒ Object
- #create_gift_cards ⇒ Object
- #generate_code ⇒ Object
- #generate_gift_cards ⇒ Object
Methods included from DisplayMoney
Instance Method Details
#amount=(amount) ⇒ Object
35 36 37 |
# File 'app/models/spree/gift_card_batch.rb', line 35 def amount=(amount) self[:amount] = Spree::LocalizedNumber.parse(amount) end |
#create_gift_cards ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'app/models/spree/gift_card_batch.rb', line 49 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_code ⇒ Object
60 61 62 63 64 65 |
# File 'app/models/spree/gift_card_batch.rb', line 60 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_cards ⇒ Object
41 42 43 44 45 46 47 |
# File 'app/models/spree/gift_card_batch.rb', line 41 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 |