Sha256: a9696c94b401ed3856b720100746078f6978e895ec5bc936110c6b0e93294138

Contents?: true

Size: 981 Bytes

Versions: 42

Compression:

Stored size: 981 Bytes

Contents

# frozen_string_literal: true

class Spree::PromotionCode < Spree::Base
  belongs_to :promotion, inverse_of: :codes
  belongs_to :promotion_code_batch, class_name: "Spree::PromotionCodeBatch"
  has_many :adjustments

  validates :value, presence: true, uniqueness: { allow_blank: true }
  validates :promotion, presence: true

  before_save :normalize_code

  self.whitelisted_ransackable_attributes = ['value']

  # Whether the promotion code has exceeded its usage restrictions
  #
  # @return true or false
  def usage_limit_exceeded?
    if usage_limit
      usage_count >= usage_limit
    end
  end

  # Number of times the code has been used overall
  #
  # @return [Integer] usage count
  def usage_count
    adjustments.eligible.
      joins(:order).
      merge(Spree::Order.complete).
      distinct.
      count(:order_id)
  end

  def usage_limit
    promotion.per_code_usage_limit
  end

  private

  def normalize_code
    self.value = value.downcase.strip
  end
end

Version data entries

42 entries across 42 versions & 2 rubygems

Version Path
solidus_core-2.9.6 app/models/spree/promotion_code.rb
solidus_core-2.8.6 app/models/spree/promotion_code.rb
solidus_core-2.9.5 app/models/spree/promotion_code.rb
solidus_core-2.9.4 app/models/spree/promotion_code.rb
solidus_core-2.6.6 app/models/spree/promotion_code.rb
solidus_core-2.7.4 app/models/spree/promotion_code.rb
solidus_core-2.8.5 app/models/spree/promotion_code.rb
solidus_core-2.9.3 app/models/spree/promotion_code.rb
solidus_core-2.9.2 app/models/spree/promotion_code.rb
solidus_core-2.7.3 app/models/spree/promotion_code.rb
solidus_core-2.6.5 app/models/spree/promotion_code.rb
solidus_core-2.9.1 app/models/spree/promotion_code.rb
solidus_core-2.9.0 app/models/spree/promotion_code.rb
solidus_core-2.9.0.rc.1 app/models/spree/promotion_code.rb
solidus_core-2.8.4 app/models/spree/promotion_code.rb
solidus_core-2.8.3 app/models/spree/promotion_code.rb
solidus_core-2.7.2 app/models/spree/promotion_code.rb
solidus_core-2.6.4 app/models/spree/promotion_code.rb
solidus_core-2.8.2 app/models/spree/promotion_code.rb
solidus_core-2.8.1 app/models/spree/promotion_code.rb