Sha256: b8c05c864e03f62bebe313756b618568db583677886768792f0a49c9718e4c94

Contents?: true

Size: 1.37 KB

Versions: 26

Compression:

Stored size: 1.37 KB

Contents

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

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

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

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

    def applicable?(_promotable)
      raise NotImplementedError, "applicable? should be implemented in a sub-class of Spree::PromotionRule"
    end

    def eligible?(_promotable, _options = {})
      raise NotImplementedError, "eligible? should be implemented in a sub-class of Spree::PromotionRule"
    end

    # This states if a promotion can be applied to the specified line item
    # It is true by default, but can be overridden by promotion rules to provide conditions
    def actionable?(_line_item)
      true
    end

    def eligibility_errors
      @eligibility_errors ||= ActiveModel::Errors.new(self)
    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

    def eligibility_error_message(key, options = {})
      Spree.t(key, Hash[scope: [:eligibility_errors, :messages]].merge(options))
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
solidus_core-2.2.2 app/models/spree/promotion_rule.rb
solidus_core-2.1.1 app/models/spree/promotion_rule.rb
solidus_core-2.0.3 app/models/spree/promotion_rule.rb
solidus_core-1.4.2 app/models/spree/promotion_rule.rb
solidus_core-1.3.2 app/models/spree/promotion_rule.rb
solidus_core-2.0.2 app/models/spree/promotion_rule.rb
solidus_core-1.4.1 app/models/spree/promotion_rule.rb
solidus_core-2.2.1 app/models/spree/promotion_rule.rb
solidus_core-2.2.0 app/models/spree/promotion_rule.rb
solidus_core-2.2.0.rc1 app/models/spree/promotion_rule.rb
solidus_core-2.2.0.beta1 app/models/spree/promotion_rule.rb
solidus_core-2.1.0 app/models/spree/promotion_rule.rb
solidus_core-2.1.0.rc1 app/models/spree/promotion_rule.rb
solidus_core-2.0.1 app/models/spree/promotion_rule.rb
solidus_core-2.1.0.beta1 app/models/spree/promotion_rule.rb
solidus_core-2.0.0 app/models/spree/promotion_rule.rb
solidus_core-1.4.0 app/models/spree/promotion_rule.rb
solidus_core-2.0.0.rc1 app/models/spree/promotion_rule.rb
solidus_core-1.4.0.rc1 app/models/spree/promotion_rule.rb
solidus_core-2.0.0.beta1 app/models/spree/promotion_rule.rb