Sha256: 050b59edd52663ea8c1d93463248df5483db4c4feb4deedbba4f2087f4dd7c59

Contents?: true

Size: 899 Bytes

Versions: 14

Compression:

Stored size: 899 Bytes

Contents

# Base class for all promotion rules
module Spree
  class PromotionRule < Spree::Base
    belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_rules

    scope :of_type, ->(t) { where(type: t) }

    validate :promotion, presence: true
    validate :unique_per_promotion, on: :create

    def self.for(promotable)
      all.select { |rule| rule.applicable?(promotable) }
    end

    def applicable?(promotable)
      raise 'applicable? should be implemented in a sub-class of Spree::PromotionRule'
    end

    def eligible?(promotable, options = {})
      raise 'eligible? should be implemented in a sub-class of Spree::PromotionRule'
    end

    private
    def unique_per_promotion
      if Spree::PromotionRule.exists?(promotion_id: promotion_id, type: self.class.name)
        errors[:base] << "Promotion already contains this rule type"
      end
    end

  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
spree_core-2.3.13 app/models/spree/promotion_rule.rb
spree_core-2.3.12 app/models/spree/promotion_rule.rb
spree_core-2.3.11 app/models/spree/promotion_rule.rb
spree_core-2.3.10 app/models/spree/promotion_rule.rb
spree_core-2.3.9 app/models/spree/promotion_rule.rb
spree_core-2.3.8 app/models/spree/promotion_rule.rb
spree_core-2.3.7 app/models/spree/promotion_rule.rb
spree_core-2.3.6 app/models/spree/promotion_rule.rb
spree_core-2.3.5 app/models/spree/promotion_rule.rb
spree_core-2.3.4 app/models/spree/promotion_rule.rb
spree_core-2.3.3 app/models/spree/promotion_rule.rb
spree_core-2.3.2 app/models/spree/promotion_rule.rb
spree_core-2.3.1 app/models/spree/promotion_rule.rb
spree_core-2.3.0 app/models/spree/promotion_rule.rb