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
33 34 35 |
# File 'app/models/spree/gift_card_batch.rb', line 33 def amount=(amount) self[:amount] = Spree::LocalizedNumber.parse(amount) end |
#create_gift_cards ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'app/models/spree/gift_card_batch.rb', line 47 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
58 59 60 61 62 63 |
# File 'app/models/spree/gift_card_batch.rb', line 58 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
39 40 41 42 43 44 45 |
# File 'app/models/spree/gift_card_batch.rb', line 39 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 |