Sha256: 94751ced15ae49a2cdcb4183b59721e77c107f2abd6f43c8a19abba75aea8266

Contents?: true

Size: 868 Bytes

Versions: 28

Compression:

Stored size: 868 Bytes

Contents

class Spree::PromotionCode < Spree::Base
  belongs_to :promotion, inverse_of: :codes
  has_many :adjustments

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

  before_save :downcase_value

  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 downcase_value
    self.value = value.downcase
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
solidus_core-2.1.1 app/models/spree/promotion_code.rb
solidus_core-2.0.3 app/models/spree/promotion_code.rb
solidus_core-1.4.2 app/models/spree/promotion_code.rb
solidus_core-1.3.2 app/models/spree/promotion_code.rb
solidus_core-1.2.3 app/models/spree/promotion_code.rb
solidus_core-2.0.2 app/models/spree/promotion_code.rb
solidus_core-1.4.1 app/models/spree/promotion_code.rb
solidus_core-2.1.0 app/models/spree/promotion_code.rb
solidus_core-2.1.0.rc1 app/models/spree/promotion_code.rb
solidus_core-2.0.1 app/models/spree/promotion_code.rb
solidus_core-2.1.0.beta1 app/models/spree/promotion_code.rb
solidus_core-2.0.0 app/models/spree/promotion_code.rb
solidus_core-1.4.0 app/models/spree/promotion_code.rb
solidus_core-2.0.0.rc1 app/models/spree/promotion_code.rb
solidus_core-1.4.0.rc1 app/models/spree/promotion_code.rb
solidus_core-2.0.0.beta1 app/models/spree/promotion_code.rb
solidus_core-1.4.0.beta1 app/models/spree/promotion_code.rb
solidus_core-1.3.1 app/models/spree/promotion_code.rb
solidus_core-1.3.0 app/models/spree/promotion_code.rb
solidus_core-1.3.0.rc2 app/models/spree/promotion_code.rb