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
38 39 40 |
# File 'app/models/spree/gift_card_batch.rb', line 38 def amount=(amount) self[:amount] = Spree::LocalizedNumber.parse(amount) end |
#create_gift_cards ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'app/models/spree/gift_card_batch.rb', line 52 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
63 64 65 66 67 68 |
# File 'app/models/spree/gift_card_batch.rb', line 63 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
44 45 46 47 48 49 50 |
# File 'app/models/spree/gift_card_batch.rb', line 44 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 |