Sha256: c43086e19b14a741af44dfac8423ade42829d4823c6f630b76dfbdddf9cedb2b

Contents?: true

Size: 625 Bytes

Versions: 5

Compression:

Stored size: 625 Bytes

Contents

# frozen_string_literal: true

module SolidusPromotions
  class PromotionCodeBatch < Spree::Base
    class CantProcessStartedBatch < StandardError
    end

    belongs_to :promotion
    has_many :promotion_codes, dependent: :destroy

    validates :number_of_codes, numericality: { greater_than: 0 }
    validates :base_code, :number_of_codes, presence: true

    def finished?
      state == "completed"
    end

    def process
      raise CantProcessStartedBatch, "Batch #{id} already started" unless state == "pending"

      update!(state: "processing")
      PromotionCodeBatchJob.perform_later(self)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
solidus_promotions-4.5.1 app/models/solidus_promotions/promotion_code_batch.rb
solidus_promotions-4.5.0 app/models/solidus_promotions/promotion_code_batch.rb
solidus_promotions-4.4.2 app/models/solidus_promotions/promotion_code_batch.rb
solidus_promotions-4.4.1 app/models/solidus_promotions/promotion_code_batch.rb
solidus_promotions-4.4.0 app/models/solidus_promotions/promotion_code_batch.rb